]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: provide a better error message when there's no go directive
authorMichael Matloob <matloob@golang.org>
Mon, 11 Mar 2024 17:53:40 +0000 (13:53 -0400)
committerMichael Matloob <matloob@golang.org>
Mon, 11 Mar 2024 18:35:47 +0000 (18:35 +0000)
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 <path> listed in go.work requires go
>= <version>, 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 <adonovan@google.com>
Reviewed-by: Sam Thanawalla <samthanawalla@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>

src/cmd/go/internal/modload/init.go
src/cmd/go/testdata/script/work_implicit_go_requirement.txt [new file with mode: 0644]

index 23db438da1919a06a8291b216f7fcc86e66c09b9..0c73b00022e4e198087f47a6d8607c67b06f9a2b 100644 (file)
@@ -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 (file)
index 0000000..e123a7b
--- /dev/null
@@ -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