]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: fix bug in using the workfile flag with tests
authorMichael Matloob <matloob@golang.org>
Fri, 19 Nov 2021 21:09:52 +0000 (16:09 -0500)
committerMichael Matloob <matloob@golang.org>
Wed, 24 Nov 2021 19:11:35 +0000 (19:11 +0000)
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>
src/cmd/go/internal/modload/init.go
src/cmd/go/internal/test/test.go
src/cmd/go/testdata/script/work_workfile.txt [new file with mode: 0644]

index 30fe446e43e76314184036200768b1368f52e026..943547e71baf341517a5f09d8660dc12e101d199 100644 (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.
index b7bbcb45136a7f4acf806296129b941f47d8cd75..7ea9d4f1f1320ba24c54d3dd1a5a6c81b2de643b 100644 (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
diff --git a/src/cmd/go/testdata/script/work_workfile.txt b/src/cmd/go/testdata/script/work_workfile.txt
new file mode 100644 (file)
index 0000000..b629181
--- /dev/null
@@ -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
\ No newline at end of file