Commit 4b38200b authored by Hiroshi Ioka's avatar Hiroshi Ioka Committed by Ian Lance Taylor

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/55350Reviewed-by: 's avatarIan Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent c3fa6f4d
...@@ -4389,3 +4389,52 @@ func TestBuildmodePIE(t *testing.T) { ...@@ -4389,3 +4389,52 @@ func TestBuildmodePIE(t *testing.T) {
t.Errorf("got %q; want %q", out, "hello") 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")
}
}
...@@ -1963,10 +1963,16 @@ func (b *Builder) runOut(dir string, desc string, env []string, cmdargs ...inter ...@@ -1963,10 +1963,16 @@ func (b *Builder) runOut(dir string, desc string, env []string, cmdargs ...inter
cmdline := str.StringList(cmdargs...) cmdline := str.StringList(cmdargs...)
if cfg.BuildN || cfg.BuildX { if cfg.BuildN || cfg.BuildX {
var envcmdline string var envcmdline string
for i := range env { for _, e := range env {
envcmdline += env[i] 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 += " "
} }
}
envcmdline += joinUnambiguously(cmdline) envcmdline += joinUnambiguously(cmdline)
b.Showcmd(dir, "%s", envcmdline) b.Showcmd(dir, "%s", envcmdline)
if cfg.BuildN { if cfg.BuildN {
...@@ -2416,7 +2422,7 @@ func (gcToolchain) pack(b *Builder, p *load.Package, objDir, afile string, ofile ...@@ -2416,7 +2422,7 @@ func (gcToolchain) pack(b *Builder, p *load.Package, objDir, afile string, ofile
} }
if cfg.BuildN || cfg.BuildX { 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)) b.Showcmd(p.Dir, "%s # internal", joinUnambiguously(cmdline))
} }
if cfg.BuildN { if cfg.BuildN {
...@@ -3220,6 +3226,9 @@ func (b *Builder) gccSupportsFlag(flag string) bool { ...@@ -3220,6 +3226,9 @@ func (b *Builder) gccSupportsFlag(flag string) bool {
return b return b
} }
if b.flagCache == nil { if b.flagCache == nil {
if cfg.BuildN || cfg.BuildX {
b.Showcmd(b.WorkDir, "touch trivial.c")
}
src := filepath.Join(b.WorkDir, "trivial.c") src := filepath.Join(b.WorkDir, "trivial.c")
if err := ioutil.WriteFile(src, []byte{}, 0666); err != nil { if err := ioutil.WriteFile(src, []byte{}, 0666); err != nil {
return false return false
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment