Skip to content

Commit fa71076

Browse files
cmd/go, cmd/cgo: only set TERM=dumb when running the compiler
The clang compiler on some terminals will issue colored error messages, which can confuse tools like cgo. To avoid this we used to set TERM=dumb for all programs started by the go tool. However, that confuses the pprof tool, which doesn't know whether to support fancy editing and colors itself. Instead, change the go tool and the cgo tool to set TERM=dumb where it matters--when invoking the C compiler--rather than in all cases. Updates #26254 Change-Id: I95174f961ac269a50a83f5f9d268219043cba968 Reviewed-on: https://go-review.googlesource.com/122975 Run-TryBot: Ian Lance Taylor <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]>
1 parent f9800a9 commit fa71076

File tree

3 files changed

+14
-8
lines changed

3 files changed

+14
-8
lines changed

src/cmd/cgo/util.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ func run(stdin []byte, argv []string) (stdout, stderr []byte, ok bool) {
5959
var bout, berr bytes.Buffer
6060
p.Stdout = &bout
6161
p.Stderr = &berr
62+
// Disable escape codes in clang error messages.
63+
p.Env = append(os.Environ(), "TERM=dumb")
6264
err := p.Run()
6365
if _, ok := err.(*exec.ExitError); err != nil && !ok {
6466
fatalf("%s", err)

src/cmd/go/internal/envcmd/env.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,6 @@ func MkEnv() []cfg.EnvVar {
6262
{Name: "GOROOT", Value: cfg.GOROOT},
6363
{Name: "GOTMPDIR", Value: os.Getenv("GOTMPDIR")},
6464
{Name: "GOTOOLDIR", Value: base.ToolDir},
65-
66-
// disable escape codes in clang errors
67-
{Name: "TERM", Value: "dumb"},
6865
}
6966

7067
if work.GccgoBin != "" {

src/cmd/go/internal/work/exec.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -990,7 +990,7 @@ func (b *Builder) vet(a *Action) error {
990990
return err
991991
}
992992

993-
var env []string
993+
env := b.cCompilerEnv()
994994
if cfg.BuildToolchainName == "gccgo" {
995995
env = append(env, "GCCGO="+BuildToolchain.compiler())
996996
}
@@ -1863,6 +1863,13 @@ func joinUnambiguously(a []string) string {
18631863
return buf.String()
18641864
}
18651865

1866+
// cCompilerEnv returns environment variables to set when running the
1867+
// C compiler. This is needed to disable escape codes in clang error
1868+
// messages that confuse tools like cgo.
1869+
func (b *Builder) cCompilerEnv() []string {
1870+
return []string{"TERM=dumb"}
1871+
}
1872+
18661873
// mkdir makes the named directory.
18671874
func (b *Builder) Mkdir(dir string) error {
18681875
// Make Mkdir(a.Objdir) a no-op instead of an error when a.Objdir == "".
@@ -2010,7 +2017,7 @@ func (b *Builder) ccompile(a *Action, p *load.Package, outfile string, flags []s
20102017
if !filepath.IsAbs(outfile) {
20112018
outfile = filepath.Join(p.Dir, outfile)
20122019
}
2013-
output, err := b.runOut(filepath.Dir(file), nil, compiler, flags, "-o", outfile, "-c", filepath.Base(file))
2020+
output, err := b.runOut(filepath.Dir(file), b.cCompilerEnv(), compiler, flags, "-o", outfile, "-c", filepath.Base(file))
20142021
if len(output) > 0 {
20152022
// On FreeBSD 11, when we pass -g to clang 3.8 it
20162023
// invokes its internal assembler with -dwarf-version=2.
@@ -2050,7 +2057,7 @@ func (b *Builder) gccld(p *load.Package, objdir, out string, flags []string, obj
20502057
} else {
20512058
cmd = b.GccCmd(p.Dir, objdir)
20522059
}
2053-
return b.run(nil, p.Dir, p.ImportPath, nil, cmd, "-o", out, objs, flags)
2060+
return b.run(nil, p.Dir, p.ImportPath, b.cCompilerEnv(), cmd, "-o", out, objs, flags)
20542061
}
20552062

20562063
// Grab these before main helpfully overwrites them.
@@ -2345,7 +2352,7 @@ func (b *Builder) cgo(a *Action, cgoExe, objdir string, pcCFLAGS, pcLDFLAGS, cgo
23452352
// along to the host linker. At this point in the code, cgoLDFLAGS
23462353
// consists of the original $CGO_LDFLAGS (unchecked) and all the
23472354
// flags put together from source code (checked).
2348-
var cgoenv []string
2355+
cgoenv := b.cCompilerEnv()
23492356
if len(cgoLDFLAGS) > 0 {
23502357
flags := make([]string, len(cgoLDFLAGS))
23512358
for i, f := range cgoLDFLAGS {
@@ -2492,7 +2499,7 @@ func (b *Builder) dynimport(a *Action, p *load.Package, objdir, importGo, cgoExe
24922499
if p.Standard && p.ImportPath == "runtime/cgo" {
24932500
cgoflags = []string{"-dynlinker"} // record path to dynamic linker
24942501
}
2495-
return b.run(a, p.Dir, p.ImportPath, nil, cfg.BuildToolexec, cgoExe, "-dynpackage", p.Name, "-dynimport", dynobj, "-dynout", importGo, cgoflags)
2502+
return b.run(a, p.Dir, p.ImportPath, b.cCompilerEnv(), cfg.BuildToolexec, cgoExe, "-dynpackage", p.Name, "-dynimport", dynobj, "-dynout", importGo, cgoflags)
24962503
}
24972504

24982505
// Run SWIG on all SWIG input files.

0 commit comments

Comments
 (0)