From 4da06e7b00ae9965ec7d2f6f131266e44f966754 Mon Sep 17 00:00:00 2001 From: Michael Matloob Date: Fri, 19 Nov 2021 16:09:52 -0500 Subject: [PATCH] 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 Run-TryBot: Michael Matloob TryBot-Result: Go Bot Reviewed-by: Bryan C. Mills --- src/cmd/go/internal/modload/init.go | 5 ++--- src/cmd/go/internal/test/test.go | 2 +- src/cmd/go/testdata/script/work_workfile.txt | 21 ++++++++++++++++++++ 3 files changed, 24 insertions(+), 4 deletions(-) create mode 100644 src/cmd/go/testdata/script/work_workfile.txt diff --git a/src/cmd/go/internal/modload/init.go b/src/cmd/go/internal/modload/init.go index 30fe446e43..943547e71b 100644 --- a/src/cmd/go/internal/modload/init.go +++ b/src/cmd/go/internal/modload/init.go @@ -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. diff --git a/src/cmd/go/internal/test/test.go b/src/cmd/go/internal/test/test.go index b7bbcb4513..7ea9d4f1f1 100644 --- a/src/cmd/go/internal/test/test.go +++ b/src/cmd/go/internal/test/test.go @@ -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 index 0000000000..b62918147e --- /dev/null +++ b/src/cmd/go/testdata/script/work_workfile.txt @@ -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 -- 2.50.0