Skip to content

Commit 178307c

Browse files
committed
cmd/go: address review comments
Address review comments from earlier CLs. These are changes I was too scared to try to push down into the original CLs (thanks, Git). Change-Id: I0e428fad73d71bd2a7d08178cf2e856de3cef19f Reviewed-on: https://go-review.googlesource.com/36257 Reviewed-by: David Crawshaw <[email protected]>
1 parent 707cadd commit 178307c

File tree

20 files changed

+72
-47
lines changed

20 files changed

+72
-47
lines changed

src/cmd/dist/deps.go

Lines changed: 6 additions & 6 deletions
Large diffs are not rendered by default.

src/cmd/go/internal/base/base.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ package base
88

99
import (
1010
"bytes"
11-
"cmd/go/internal/cfg"
12-
"cmd/go/internal/str"
1311
"errors"
1412
"flag"
1513
"fmt"
@@ -19,6 +17,9 @@ import (
1917
"os/exec"
2018
"strings"
2119
"sync"
20+
21+
"cmd/go/internal/cfg"
22+
"cmd/go/internal/str"
2223
)
2324

2425
// A Command is an implementation of a go command

src/cmd/go/internal/base/flag.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
package base
66

77
import (
8-
"cmd/go/internal/str"
98
"flag"
9+
10+
"cmd/go/internal/cfg"
11+
"cmd/go/internal/str"
1012
)
1113

1214
// A StringsFlag is a command-line flag that interprets its argument
@@ -28,6 +30,6 @@ func (v *StringsFlag) String() string {
2830

2931
// AddBuildFlagsNX adds the -n and -x build flags to the flag set.
3032
func AddBuildFlagsNX(flags *flag.FlagSet) {
31-
flags.BoolVar(&BuildN, "n", false, "")
32-
flags.BoolVar(&BuildX, "x", false, "")
33+
flags.BoolVar(&cfg.BuildN, "n", false, "")
34+
flags.BoolVar(&cfg.BuildX, "x", false, "")
3335
}

src/cmd/go/internal/base/path.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,15 @@ import (
1010
"strings"
1111
)
1212

13-
var Cwd, _ = os.Getwd()
13+
func getwd() string {
14+
wd, err := os.Getwd()
15+
if err != nil {
16+
Fatalf("cannot determine current directory: %v", err)
17+
}
18+
return wd
19+
}
20+
21+
var Cwd = getwd()
1422

1523
// ShortPath returns an absolute or relative name for path, whatever is shorter.
1624
func ShortPath(path string) string {

src/cmd/go/internal/cfg/cfg.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
package cfg
88

99
import (
10-
"flag"
1110
"go/build"
1211
"os"
1312
"path/filepath"
@@ -30,14 +29,18 @@ var (
3029
BuildRace bool // -race flag
3130
BuildToolexec []string // -toolexec flag
3231
BuildToolchainName string
33-
BuildToolchainCompiler string
34-
BuildToolchainLinker string
32+
BuildToolchainCompiler func() string
33+
BuildToolchainLinker func() string
3534
BuildV bool // -v flag
3635
BuildWork bool // -work flag
3736
BuildX bool // -x flag
38-
3937
)
4038

39+
func init() {
40+
BuildToolchainCompiler = func() string { return "missing-compiler" }
41+
BuildToolchainLinker = func() string { return "missing-linker" }
42+
}
43+
4144
// The test coverage mode affects package loading. Sigh.
4245
var TestCoverMode string // -covermode flag
4346

@@ -50,8 +53,10 @@ type EnvVar struct {
5053
// OrigEnv is the original environment of the program at startup.
5154
var OrigEnv []string
5255

53-
// NewEnv is the new environment for running commands.
54-
var NewEnv []EnvVar
56+
// CmdEnv is the new environment for running go tool commands.
57+
// User binaries (during go test or go run) are run with OrigEnv,
58+
// not CmdEnv.
59+
var CmdEnv []EnvVar
5560

5661
// Global build parameters (used during package load)
5762
var (
@@ -61,12 +66,6 @@ var (
6166
Gopath []string
6267
)
6368

64-
// AddBuildFlagsNX adds the -n and -x build flags to the flag set.
65-
func AddBuildFlagsNX(flags *flag.FlagSet) {
66-
flags.BoolVar(&BuildN, "n", false, "")
67-
flags.BoolVar(&BuildX, "x", false, "")
68-
}
69-
7069
var (
7170
GOROOT = filepath.Clean(runtime.GOROOT())
7271
GOBIN = os.Getenv("GOBIN")

src/cmd/go/internal/clean/clean.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5+
// Package clean implements the ``go clean'' command.
56
package clean
67

78
import (

src/cmd/go/internal/doc/doc.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5+
// Package doc implements the ``go doc'' command.
56
package doc
67

78
import (

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
package env
5+
// Package envcmd implements the ``go env'' command.
6+
package envcmd
67

78
import (
89
"fmt"
@@ -103,7 +104,7 @@ func ExtraEnvVars() []cfg.EnvVar {
103104
}
104105

105106
func runEnv(cmd *base.Command, args []string) {
106-
env := cfg.NewEnv
107+
env := cfg.CmdEnv
107108
env = append(env, ExtraEnvVars()...)
108109
if len(args) > 0 {
109110
for _, name := range args {

src/cmd/go/internal/fix/fix.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5+
// Package fix implements the ``go fix'' command.
56
package fix
67

78
import (

src/cmd/go/internal/fmt/fmt.go renamed to src/cmd/go/internal/fmtcmd/fmt.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
package fmt
5+
// Package fmtcmd implements the ``go fmt'' command.
6+
package fmtcmd
67

78
import (
89
"os"
@@ -15,7 +16,7 @@ import (
1516
)
1617

1718
func init() {
18-
cfg.AddBuildFlagsNX(&CmdFmt.Flag)
19+
base.AddBuildFlagsNX(&CmdFmt.Flag)
1920
}
2021

2122
var CmdFmt = &base.Command{

src/cmd/go/internal/generate/generate.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5+
// Package generate implements the ``go generate'' command.
56
package generate
67

78
import (

src/cmd/go/internal/help/help.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
// Package help implements "go help".
5+
// Package help implements the ``go help'' command.
66
package help
77

88
import (
99
"bufio"
1010
"bytes"
1111
"fmt"
12-
"html/template"
1312
"io"
1413
"os"
1514
"strings"
15+
"text/template"
1616
"unicode"
1717
"unicode/utf8"
1818

src/cmd/go/internal/list/list.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,21 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5+
// Package list implements the ``go list'' command.
56
package list
67

78
import (
89
"bufio"
9-
"cmd/go/internal/base"
10-
"cmd/go/internal/cfg"
11-
"cmd/go/internal/load"
12-
"cmd/go/internal/work"
1310
"encoding/json"
1411
"io"
1512
"os"
1613
"strings"
1714
"text/template"
15+
16+
"cmd/go/internal/base"
17+
"cmd/go/internal/cfg"
18+
"cmd/go/internal/load"
19+
"cmd/go/internal/work"
1820
)
1921

2022
var CmdList = &base.Command{

src/cmd/go/internal/load/pkg.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -883,7 +883,7 @@ func (p *Package) load(stk *ImportStack, bp *build.Package, err error) *Package
883883
p.Internal.Target = filepath.Join(cfg.GOROOTpkg, "tool", full)
884884
}
885885
if p.Internal.Target != "" && cfg.BuildContext.GOOS == "windows" {
886-
p.Internal.Target += ".Internal.Exe"
886+
p.Internal.Target += ".exe"
887887
}
888888
} else if p.Internal.Local {
889889
// Local import turned into absolute path.
@@ -1562,10 +1562,10 @@ func isStale(p *Package) (bool, string) {
15621562
// Excluding $GOROOT used to also fix issue 4106, but that's now
15631563
// taken care of above (at least when the installed Go is a released version).
15641564
if p.Root != cfg.GOROOT {
1565-
if olderThan(cfg.BuildToolchainCompiler) {
1565+
if olderThan(cfg.BuildToolchainCompiler()) {
15661566
return true, "newer compiler"
15671567
}
1568-
if p.Internal.Build.IsCommand() && olderThan(cfg.BuildToolchainLinker) {
1568+
if p.Internal.Build.IsCommand() && olderThan(cfg.BuildToolchainLinker()) {
15691569
return true, "newer linker"
15701570
}
15711571
}

src/cmd/go/internal/run/run.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5+
// Package run implements the ``go run'' command.
56
package run
67

78
import (
@@ -66,7 +67,7 @@ func runRun(cmd *base.Command, args []string) {
6667
}
6768
for _, file := range files {
6869
if strings.HasSuffix(file, "_test.go") {
69-
// goFilesPackage is going to assign this to TestGoFiles.
70+
// GoFilesPackage is going to assign this to TestGoFiles.
7071
// Reject since it won't be part of the build.
7172
base.Fatalf("go run: cannot run *_test.go files (%s)", file)
7273
}

src/cmd/go/internal/tool/tool.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5+
// Package tool implements the ``go tool'' command.
56
package tool
67

78
import (

src/cmd/go/internal/version/version.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5+
// Package version implements the ``go version'' command.
56
package version
67

78
import (
8-
"cmd/go/internal/base"
99
"fmt"
1010
"runtime"
11+
12+
"cmd/go/internal/base"
1113
)
1214

1315
var CmdVersion = &base.Command{

src/cmd/go/internal/vet/vet.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5+
// Package vet implements the ``go vet'' command.
56
package vet
67

78
import (

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,8 @@ func (c buildCompiler) Set(value string) error {
180180
return fmt.Errorf("unknown compiler %q", value)
181181
}
182182
cfg.BuildToolchainName = value
183-
cfg.BuildToolchainCompiler = BuildToolchain.compiler()
184-
cfg.BuildToolchainLinker = BuildToolchain.linker()
183+
cfg.BuildToolchainCompiler = BuildToolchain.compiler
184+
cfg.BuildToolchainLinker = BuildToolchain.linker
185185
cfg.BuildContext.Compiler = value
186186
return nil
187187
}
@@ -1317,9 +1317,11 @@ func (b *Builder) build(a *Action) (err error) {
13171317
sfiles = nil
13181318
}
13191319

1320-
cgoExe := base.Tool("cgo")
1320+
var cgoExe string
13211321
if a.cgo != nil && a.cgo.Target != "" {
13221322
cgoExe = a.cgo.Target
1323+
} else {
1324+
cgoExe = base.Tool("cgo")
13231325
}
13241326
outGo, outObj, err := b.cgo(a, cgoExe, obj, pcCFLAGS, pcLDFLAGS, cgofiles, objdirCgofiles, gccfiles, cxxfiles, a.Package.MFiles, a.Package.FFiles)
13251327
if err != nil {

src/cmd/go/main.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ import (
2020
"cmd/go/internal/cfg"
2121
"cmd/go/internal/clean"
2222
"cmd/go/internal/doc"
23-
"cmd/go/internal/env"
23+
"cmd/go/internal/envcmd"
2424
"cmd/go/internal/fix"
25-
fmtcmd "cmd/go/internal/fmt"
25+
"cmd/go/internal/fmtcmd"
2626
"cmd/go/internal/generate"
2727
"cmd/go/internal/get"
2828
"cmd/go/internal/help"
@@ -40,7 +40,7 @@ func init() {
4040
work.CmdBuild,
4141
clean.CmdClean,
4242
doc.CmdDoc,
43-
env.CmdEnv,
43+
envcmd.CmdEnv,
4444
bug.CmdBug,
4545
fix.CmdFix,
4646
fmtcmd.CmdFmt,
@@ -114,8 +114,8 @@ func main() {
114114
// but in practice there might be skew
115115
// This makes sure we all agree.
116116
cfg.OrigEnv = os.Environ()
117-
cfg.NewEnv = env.MkEnv()
118-
for _, env := range cfg.NewEnv {
117+
cfg.CmdEnv = envcmd.MkEnv()
118+
for _, env := range cfg.CmdEnv {
119119
if os.Getenv(env.Name) != env.Value {
120120
os.Setenv(env.Name, env.Value)
121121
}

0 commit comments

Comments
 (0)