From: Hiroshi Ioka Date: Mon, 14 Aug 2017 14:02:56 +0000 (+0900) Subject: cmd/go: correctly quote environment variables in -x output X-Git-Tag: go1.10beta1~1580 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=4b38200bfd2d4718730e63cc991e7163ed0d7fd9;p=gostls13.git cmd/go: correctly quote environment variables in -x output This fixes the -x output so that when it reports environment variables they are correctly quoted for later execution by the shell. Also fix -x output to use the right path to the pack tool, and note when we are touching a file. Fixes #21427 Change-Id: I323ef4edf9905b08bc26944b94183d8da2fa9675 Reviewed-on: https://go-review.googlesource.com/55350 Reviewed-by: Ian Lance Taylor Run-TryBot: Ian Lance Taylor TryBot-Result: Gobot Gobot --- diff --git a/src/cmd/go/go_test.go b/src/cmd/go/go_test.go index 2006283ea1..56b9b07889 100644 --- a/src/cmd/go/go_test.go +++ b/src/cmd/go/go_test.go @@ -4389,3 +4389,52 @@ func TestBuildmodePIE(t *testing.T) { t.Errorf("got %q; want %q", out, "hello") } } + +func TestExecBuildX(t *testing.T) { + if !canCgo { + t.Skip("skipping because cgo not enabled") + } + + if runtime.GOOS == "plan9" || runtime.GOOS == "windows" { + t.Skipf("skipping because unix shell is not supported on %s", runtime.GOOS) + } + + tg := testgo(t) + defer tg.cleanup() + + tg.tempFile("main.go", `package main; import "C"; func main() { print("hello") }`) + src := tg.path("main.go") + obj := tg.path("main") + tg.run("build", "-x", "-o", obj, src) + sh := tg.path("test.sh") + err := ioutil.WriteFile(sh, []byte(tg.getStderr()), 0666) + if err != nil { + t.Fatal(err) + } + + out, err := exec.Command(obj).CombinedOutput() + if err != nil { + t.Fatal(err) + } + if string(out) != "hello" { + t.Fatalf("got %q; want %q", out, "hello") + } + + err = os.Remove(obj) + if err != nil { + t.Fatal(err) + } + + out, err = exec.Command("/bin/sh", sh).CombinedOutput() + if err != nil { + t.Fatalf("/bin/sh %s: %v\n%s", sh, err, out) + } + + out, err = exec.Command(obj).CombinedOutput() + if err != nil { + t.Fatal(err) + } + if string(out) != "hello" { + t.Fatalf("got %q; want %q", out, "hello") + } +} diff --git a/src/cmd/go/internal/work/build.go b/src/cmd/go/internal/work/build.go index 53afebe8cc..400186307e 100644 --- a/src/cmd/go/internal/work/build.go +++ b/src/cmd/go/internal/work/build.go @@ -1963,9 +1963,15 @@ func (b *Builder) runOut(dir string, desc string, env []string, cmdargs ...inter cmdline := str.StringList(cmdargs...) if cfg.BuildN || cfg.BuildX { var envcmdline string - for i := range env { - envcmdline += env[i] - envcmdline += " " + for _, e := range env { + if j := strings.IndexByte(e, '='); j != -1 { + if strings.ContainsRune(e[j+1:], '\'') { + envcmdline += fmt.Sprintf("%s=%q", e[:j], e[j+1:]) + } else { + envcmdline += fmt.Sprintf("%s='%s'", e[:j], e[j+1:]) + } + envcmdline += " " + } } envcmdline += joinUnambiguously(cmdline) b.Showcmd(dir, "%s", envcmdline) @@ -2416,7 +2422,7 @@ func (gcToolchain) pack(b *Builder, p *load.Package, objDir, afile string, ofile } if cfg.BuildN || cfg.BuildX { - cmdline := str.StringList("pack", "r", absAfile, absOfiles) + cmdline := str.StringList(base.Tool("pack"), "r", absAfile, absOfiles) b.Showcmd(p.Dir, "%s # internal", joinUnambiguously(cmdline)) } if cfg.BuildN { @@ -3220,6 +3226,9 @@ func (b *Builder) gccSupportsFlag(flag string) bool { return b } if b.flagCache == nil { + if cfg.BuildN || cfg.BuildX { + b.Showcmd(b.WorkDir, "touch trivial.c") + } src := filepath.Join(b.WorkDir, "trivial.c") if err := ioutil.WriteFile(src, []byte{}, 0666); err != nil { return false