From 473d18a8c139ca55266315b75411202ce3dd5f35 Mon Sep 17 00:00:00 2001 From: Alvar Penning Date: Tue, 12 Aug 2025 20:15:37 +0200 Subject: [PATCH] playground/socket: no prog.go from whitespaces Ensure that no empty prog.go file is created for txtar-formatted input with leading whitespace characters, breaking compilation. The internal process.start method creates temporary files based on the user-supplied input. Although not explicitly documented, this input may be txtar-formatted and thereby result in multiple files. If a comment is detected in the txtar input, it will be written to the separate prog.go file. This allows both txtar-formatted data and plain Go source code to be handled. Note that if there are leading whitespace characters in the input, they will be interpreted as a comment and written into the prog.go file. Because the file will then be missing "package main", the compilation will fail. I stumbled upon this error while attempting to create a working Go module demo for present. Thus, creating a txtar input with go.{mod,sum} and a source code file. Turns out, when using present, the run message sent from the browser prepends the body part with "\n\n" characters. When the input is not in the txtar format, this does not matter since the Go compiler ignores the empty lines. When, however, txtar is used, this results in an empty prog.go file, breaking compilation. This change ensures that the comment part after txtar parsing only contains text before getting written into a Go source code file. I decided to make this change within the Go code rather in present's front end because it this affects all kind of usage involving whitespace characters. --- playground/socket/socket.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/playground/socket/socket.go b/playground/socket/socket.go index c7843e59734..99558f7979b 100644 --- a/playground/socket/socket.go +++ b/playground/socket/socket.go @@ -369,7 +369,7 @@ func (p *process) start(body string, opt *Options) error { // write body to x.go files a := txtar.Parse([]byte(body)) - if len(a.Comment) != 0 { + if strings.TrimSpace(string(a.Comment)) != "" { a.Files = append(a.Files, txtar.File{Name: "prog.go", Data: a.Comment}) a.Comment = nil }