Skip to content

Commit 8db9634

Browse files
kortschakbradfitz
authored andcommitted
playground/socket: handle multi-file present play snippets
Fixes golang/go#35242 Change-Id: I9621aa0843026ab6331499bcd0ad5ba1e4a21ca5 Reviewed-on: https://go-review.googlesource.com/c/tools/+/204237 Run-TryBot: Brad Fitzpatrick <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]>
1 parent 5ae4576 commit 8db9634

File tree

1 file changed

+34
-37
lines changed

1 file changed

+34
-37
lines changed

playground/socket/socket.go

+34-37
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ import (
2929
"os/exec"
3030
"path/filepath"
3131
"runtime"
32-
"strconv"
3332
"strings"
3433
"time"
3534
"unicode/utf8"
3635

3736
"golang.org/x/net/websocket"
37+
"golang.org/x/tools/txtar"
3838
)
3939

4040
// RunScripts specifies whether the socket handler should execute shell scripts
@@ -162,7 +162,7 @@ type process struct {
162162
out chan<- *Message
163163
done chan struct{} // closed when wait completes
164164
run *exec.Cmd
165-
bin string
165+
path string
166166
}
167167

168168
// startProcess builds and runs the given program, sending its output
@@ -203,8 +203,8 @@ func startProcess(id, body string, dest chan<- *Message, opt *Options) *process
203203
// end sends an "end" message to the client, containing the process id and the
204204
// given error value. It also removes the binary, if present.
205205
func (p *process) end(err error) {
206-
if p.bin != "" {
207-
defer os.Remove(p.bin)
206+
if p.path != "" {
207+
defer os.RemoveAll(p.path)
208208
}
209209
m := &Message{Kind: "end"}
210210
if err != nil {
@@ -355,22 +355,37 @@ func (p *process) start(body string, opt *Options) error {
355355
// (rather than the go tool process).
356356
// This makes Kill work.
357357

358-
bin := filepath.Join(tmpdir, "compile"+strconv.Itoa(<-uniq))
359-
src := bin + ".go"
358+
path, err := ioutil.TempDir("", "present-")
359+
if err != nil {
360+
return err
361+
}
362+
defer os.RemoveAll(path)
363+
364+
out := "prog"
360365
if runtime.GOOS == "windows" {
361-
bin += ".exe"
366+
out = "prog.exe"
362367
}
368+
bin := filepath.Join(path, out)
363369

364-
// write body to x.go
365-
defer os.Remove(src)
366-
err := ioutil.WriteFile(src, []byte(body), 0666)
367-
if err != nil {
368-
return err
370+
// write body to x.go files
371+
a := txtar.Parse([]byte(body))
372+
if len(a.Comment) != 0 {
373+
a.Files = append(a.Files, txtar.File{Name: "prog.go", Data: a.Comment})
374+
a.Comment = nil
375+
}
376+
hasModfile := false
377+
for _, f := range a.Files {
378+
err = ioutil.WriteFile(filepath.Join(path, f.Name), f.Data, 0666)
379+
if err != nil {
380+
return err
381+
}
382+
if f.Name == "go.mod" {
383+
hasModfile = true
384+
}
369385
}
370386

371387
// build x.go, creating x
372-
p.bin = bin // to be removed by p.end
373-
dir, file := filepath.Split(src)
388+
p.path = path // to be removed by p.end
374389
args := []string{"go", "build", "-tags", "OMIT"}
375390
if opt != nil && opt.Race {
376391
p.out <- &Message{
@@ -379,8 +394,11 @@ func (p *process) start(body string, opt *Options) error {
379394
}
380395
args = append(args, "-race")
381396
}
382-
args = append(args, "-o", bin, file)
383-
cmd := p.cmd(dir, args...)
397+
args = append(args, "-o", bin)
398+
cmd := p.cmd(path, args...)
399+
if !hasModfile {
400+
cmd.Env = append(cmd.Env, "GO111MODULE=off")
401+
}
384402
cmd.Stdout = cmd.Stderr // send compiler output to stderr
385403
if err := cmd.Run(); err != nil {
386404
return err
@@ -501,24 +519,3 @@ func safeString(b []byte) string {
501519
}
502520
return buf.String()
503521
}
504-
505-
var tmpdir string
506-
507-
func init() {
508-
// find real path to temporary directory
509-
var err error
510-
tmpdir, err = filepath.EvalSymlinks(os.TempDir())
511-
if err != nil {
512-
log.Fatal(err)
513-
}
514-
}
515-
516-
var uniq = make(chan int) // a source of numbers for naming temporary files
517-
518-
func init() {
519-
go func() {
520-
for i := 0; ; i++ {
521-
uniq <- i
522-
}
523-
}()
524-
}

0 commit comments

Comments
 (0)