Skip to content

Commit 4b38200

Browse files
hirochachachaianlancetaylor
authored andcommitted
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 <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent c3fa6f4 commit 4b38200

File tree

2 files changed

+62
-4
lines changed

2 files changed

+62
-4
lines changed

src/cmd/go/go_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4389,3 +4389,52 @@ func TestBuildmodePIE(t *testing.T) {
43894389
t.Errorf("got %q; want %q", out, "hello")
43904390
}
43914391
}
4392+
4393+
func TestExecBuildX(t *testing.T) {
4394+
if !canCgo {
4395+
t.Skip("skipping because cgo not enabled")
4396+
}
4397+
4398+
if runtime.GOOS == "plan9" || runtime.GOOS == "windows" {
4399+
t.Skipf("skipping because unix shell is not supported on %s", runtime.GOOS)
4400+
}
4401+
4402+
tg := testgo(t)
4403+
defer tg.cleanup()
4404+
4405+
tg.tempFile("main.go", `package main; import "C"; func main() { print("hello") }`)
4406+
src := tg.path("main.go")
4407+
obj := tg.path("main")
4408+
tg.run("build", "-x", "-o", obj, src)
4409+
sh := tg.path("test.sh")
4410+
err := ioutil.WriteFile(sh, []byte(tg.getStderr()), 0666)
4411+
if err != nil {
4412+
t.Fatal(err)
4413+
}
4414+
4415+
out, err := exec.Command(obj).CombinedOutput()
4416+
if err != nil {
4417+
t.Fatal(err)
4418+
}
4419+
if string(out) != "hello" {
4420+
t.Fatalf("got %q; want %q", out, "hello")
4421+
}
4422+
4423+
err = os.Remove(obj)
4424+
if err != nil {
4425+
t.Fatal(err)
4426+
}
4427+
4428+
out, err = exec.Command("/bin/sh", sh).CombinedOutput()
4429+
if err != nil {
4430+
t.Fatalf("/bin/sh %s: %v\n%s", sh, err, out)
4431+
}
4432+
4433+
out, err = exec.Command(obj).CombinedOutput()
4434+
if err != nil {
4435+
t.Fatal(err)
4436+
}
4437+
if string(out) != "hello" {
4438+
t.Fatalf("got %q; want %q", out, "hello")
4439+
}
4440+
}

src/cmd/go/internal/work/build.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1963,9 +1963,15 @@ func (b *Builder) runOut(dir string, desc string, env []string, cmdargs ...inter
19631963
cmdline := str.StringList(cmdargs...)
19641964
if cfg.BuildN || cfg.BuildX {
19651965
var envcmdline string
1966-
for i := range env {
1967-
envcmdline += env[i]
1968-
envcmdline += " "
1966+
for _, e := range env {
1967+
if j := strings.IndexByte(e, '='); j != -1 {
1968+
if strings.ContainsRune(e[j+1:], '\'') {
1969+
envcmdline += fmt.Sprintf("%s=%q", e[:j], e[j+1:])
1970+
} else {
1971+
envcmdline += fmt.Sprintf("%s='%s'", e[:j], e[j+1:])
1972+
}
1973+
envcmdline += " "
1974+
}
19691975
}
19701976
envcmdline += joinUnambiguously(cmdline)
19711977
b.Showcmd(dir, "%s", envcmdline)
@@ -2416,7 +2422,7 @@ func (gcToolchain) pack(b *Builder, p *load.Package, objDir, afile string, ofile
24162422
}
24172423

24182424
if cfg.BuildN || cfg.BuildX {
2419-
cmdline := str.StringList("pack", "r", absAfile, absOfiles)
2425+
cmdline := str.StringList(base.Tool("pack"), "r", absAfile, absOfiles)
24202426
b.Showcmd(p.Dir, "%s # internal", joinUnambiguously(cmdline))
24212427
}
24222428
if cfg.BuildN {
@@ -3220,6 +3226,9 @@ func (b *Builder) gccSupportsFlag(flag string) bool {
32203226
return b
32213227
}
32223228
if b.flagCache == nil {
3229+
if cfg.BuildN || cfg.BuildX {
3230+
b.Showcmd(b.WorkDir, "touch trivial.c")
3231+
}
32233232
src := filepath.Join(b.WorkDir, "trivial.c")
32243233
if err := ioutil.WriteFile(src, []byte{}, 0666); err != nil {
32253234
return false

0 commit comments

Comments
 (0)