}
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.
--- /dev/null
+# 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