Skip to content

Commit

Permalink
cmd/go: provide a better error message when there's no go directive
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
matloob committed Mar 11, 2024
1 parent 3a41bfa commit edbb5a1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/cmd/go/internal/modload/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
18 changes: 18 additions & 0 deletions src/cmd/go/testdata/script/work_implicit_go_requirement.txt
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit edbb5a1

Please sign in to comment.