cmd/go: fix bug in using the workfile flag with tests

Tests do custom flag processing so we must process the workfile flag
after that happens.

Also fix an issue where errors weren't handled properly when the
workfile wasn't absolute (the go command should just exit), and where a
parse error was just dropped.

Fixes #48576

Change-Id: I3a94d8d3a515114b2c4cc0e73f63447df2fc6bc9
Reviewed-on: https://go-review.googlesource.com/c/go/+/366174
Trust: Michael Matloob <matloob@golang.org>
Run-TryBot: Michael Matloob <matloob@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
This commit is contained in:
Michael Matloob 2021-11-19 16:09:52 -05:00
parent e883005d2a
commit 4da06e7b00
3 changed files with 24 additions and 4 deletions

View File

@ -294,7 +294,7 @@ func InitWorkfile() {
workFilePath = findWorkspaceFile(base.Cwd())
default:
if !filepath.IsAbs(cfg.WorkFile) {
base.Errorf("the path provided to -workfile must be an absolute path")
base.Fatalf("the path provided to -workfile must be an absolute path")
}
workFilePath = cfg.WorkFile
}
@ -590,9 +590,8 @@ func ReadWorkFile(path string) (*modfile.WorkFile, error) {
if err != nil {
return nil, err
}
wf, err := modfile.ParseWork(path, workData, nil)
return wf, nil
return modfile.ParseWork(path, workData, nil)
}
// WriteWorkFile cleans and writes out the go.work file to the given path.

View File

@ -619,8 +619,8 @@ var defaultVetFlags = []string{
}
func runTest(ctx context.Context, cmd *base.Command, args []string) {
modload.InitWorkfile()
pkgArgs, testArgs = testFlags(args)
modload.InitWorkfile() // The test command does custom flag processing; initialize workspaces after that.
if cfg.DebugTrace != "" {
var close func() error

View File

@ -0,0 +1,21 @@
! go list -workfile=stop.work a # require absolute path
! stderr panic
! go list -workfile=doesnotexist a
! stderr panic
go list -n -workfile=$GOPATH/src/stop.work a
go build -n -workfile=$GOPATH/src/stop.work a
go test -n -workfile=$GOPATH/src/stop.work a
-- stop.work --
go 1.18
use ./a
-- a/a.go --
package a
-- a/a_test.go --
package a
-- a/go.mod --
module a
go 1.18