Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 913ae23

Browse files
committed
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.
1 parent 8866876 commit 913ae23

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

playground/socket/socket.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ func (p *process) start(body string, opt *Options) error {
369369

370370
// write body to x.go files
371371
a := txtar.Parse([]byte(body))
372-
if len(a.Comment) != 0 {
372+
if strings.TrimSpace(string(a.Comment)) != "" {
373373
a.Files = append(a.Files, txtar.File{Name: "prog.go", Data: a.Comment})
374374
a.Comment = nil
375375
}

0 commit comments

Comments
 (0)