diff options
author | Philipp Sauter <sauterp@protonmail.com> | 2020-11-08 22:12:38 +0100 |
---|---|---|
committer | Bryan C. Mills <bcmills@google.com> | 2021-05-05 01:28:34 +0000 |
commit | 4df662fb373480b5055e645120558bb536fae42c (patch) | |
tree | 56e29e105f1f96e348060959b03ab4d49ea959b0 /src/cmd/go/internal/test/testflag.go | |
parent | bb5e45219af69135c389c6b68f9a67207bb32e05 (diff) | |
download | go-4df662fb373480b5055e645120558bb536fae42c.tar.gz go-4df662fb373480b5055e645120558bb536fae42c.zip |
cmd/go: don't crash when running "go version" in deleted directory
If the go command is executed on Linux in a deleted directory,
it fails. This behavior is reasonable for commands which depend on
the CWD, but it's unexpected for commands like `go version`.
This change delays initialization of a global CWD variable.
Fixed #34499
Change-Id: I7302fb84a3b7f5f149a123d277abd5b9b5bc95b2
Reviewed-on: https://go-review.googlesource.com/c/go/+/268261
Reviewed-by: Bryan C. Mills <bcmills@google.com>
Trust: Bryan C. Mills <bcmills@google.com>
Trust: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Bryan C. Mills <bcmills@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Diffstat (limited to 'src/cmd/go/internal/test/testflag.go')
-rw-r--r-- | src/cmd/go/internal/test/testflag.go | 25 |
1 files changed, 16 insertions, 9 deletions
diff --git a/src/cmd/go/internal/test/testflag.go b/src/cmd/go/internal/test/testflag.go index 6ed96a36d0..08f1efa2c0 100644 --- a/src/cmd/go/internal/test/testflag.go +++ b/src/cmd/go/internal/test/testflag.go @@ -62,7 +62,7 @@ func init() { cf.String("memprofilerate", "", "") cf.StringVar(&testMutexProfile, "mutexprofile", "", "") cf.String("mutexprofilefraction", "", "") - cf.Var(outputdirFlag{&testOutputDir}, "outputdir", "") + cf.Var(&testOutputDir, "outputdir", "") cf.Int("parallel", 0, "") cf.String("run", "", "") cf.Bool("short", false, "") @@ -71,7 +71,7 @@ func init() { cf.BoolVar(&testV, "v", false, "") cf.Var(&testShuffle, "shuffle", "") - for name, _ := range passFlagToTest { + for name := range passFlagToTest { cf.Var(cf.Lookup(name).Value, "test."+name, "") } } @@ -128,19 +128,26 @@ func (f stringFlag) Set(value string) error { // outputdirFlag implements the -outputdir flag. // It interprets an empty value as the working directory of the 'go' command. type outputdirFlag struct { - resolved *string + abs string } -func (f outputdirFlag) String() string { return *f.resolved } -func (f outputdirFlag) Set(value string) (err error) { +func (f *outputdirFlag) String() string { + return f.abs +} +func (f *outputdirFlag) Set(value string) (err error) { if value == "" { - // The empty string implies the working directory of the 'go' command. - *f.resolved = base.Cwd + f.abs = "" } else { - *f.resolved, err = filepath.Abs(value) + f.abs, err = filepath.Abs(value) } return err } +func (f *outputdirFlag) getAbs() string { + if f.abs == "" { + return base.Cwd() + } + return f.abs +} // vetFlag implements the special parsing logic for the -vet flag: // a comma-separated list, with a distinguished value "off" and @@ -404,7 +411,7 @@ func testFlags(args []string) (packageNames, passToTest []string) { // command. Set it explicitly if it is needed due to some other flag that // requests output. if testProfile() != "" && !outputDirSet { - injectedFlags = append(injectedFlags, "-test.outputdir="+testOutputDir) + injectedFlags = append(injectedFlags, "-test.outputdir="+testOutputDir.getAbs()) } // If the user is explicitly passing -help or -h, show output |