From: Michael Matloob Date: Mon, 11 Mar 2024 17:53:40 +0000 (-0400) Subject: cmd/go: provide a better error message when there's no go directive X-Git-Tag: go1.23rc1~920 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=edbb5a13b4d89adcfdce3704615e2d33b5991794;p=gostls13.git cmd/go: provide a better error message when there's no go directive On Go 1.21+ it's an error for a workspace to contain a module with a version newer than the workspace's stated go version. If the workspace doesn't explicitly have a go version it's explicitly 1.18. So if a workspace without a go directive contains a module whose go directive is newer on it's always an error for 1.21+. In the error, before this CL the error would read "module listed in go.work requires go >= , but go.work lists go 1.18". After this change the second clause would read "but go.work implicitly requires go 1.18. Fixes #66207 Change-Id: I44680880162a82e5cee9cfc8655d6774add6f762 Reviewed-on: https://go-review.googlesource.com/c/go/+/570735 Reviewed-by: Alan Donovan Reviewed-by: Sam Thanawalla LUCI-TryBot-Result: Go LUCI --- diff --git a/src/cmd/go/internal/modload/init.go b/src/cmd/go/internal/modload/init.go index 23db438da1..0c73b00022 100644 --- a/src/cmd/go/internal/modload/init.go +++ b/src/cmd/go/internal/modload/init.go @@ -1001,8 +1001,14 @@ func loadModFile(ctx context.Context, opts *PackageOpts) (*Requirements, error) } func errWorkTooOld(gomod string, wf *modfile.WorkFile, goVers string) error { - return fmt.Errorf("module %s listed in go.work file requires go >= %s, but go.work lists go %s; to update it:\n\tgo work use", - base.ShortPath(filepath.Dir(gomod)), goVers, gover.FromGoWork(wf)) + verb := "lists" + if wf == nil || wf.Go == nil { + // A go.work file implicitly requires go1.18 + // even when it doesn't list any version. + verb = "implicitly requires" + } + return fmt.Errorf("module %s listed in go.work file requires go >= %s, but go.work %s go %s; to update it:\n\tgo work use", + base.ShortPath(filepath.Dir(gomod)), goVers, verb, gover.FromGoWork(wf)) } // CreateModFile initializes a new module by creating a go.mod file. diff --git a/src/cmd/go/testdata/script/work_implicit_go_requirement.txt b/src/cmd/go/testdata/script/work_implicit_go_requirement.txt new file mode 100644 index 0000000000..e123a7b01a --- /dev/null +++ b/src/cmd/go/testdata/script/work_implicit_go_requirement.txt @@ -0,0 +1,18 @@ +# Issue 66207: provide a better error message when there's no +# go directive in a go.work file so 1.18 is implicitly required. + +! go list +stderr 'go: module . listed in go.work file requires go >= 1.21, but go.work implicitly requires go 1.18; to update it:\s+go work use' + +go work use +go list +stdout foo + +-- go.work -- +use . +-- go.mod -- +module foo + +go 1.21 +-- foo.go -- +package foo