diff options
author | Michael Matloob <matloob@golang.org> | 2021-11-18 14:48:26 -0500 |
---|---|---|
committer | Michael Matloob <matloob@golang.org> | 2021-11-22 15:54:11 +0000 |
commit | e73c6c8808da281186a4d8f7107e34e9f7a4a9ee (patch) | |
tree | 3a344b163138e4b8b082858e852ed361ccad5b65 /src/cmd/go | |
parent | b2aa1380d9ad9a46eb98cd2e1ad71fcbccdc2bd1 (diff) | |
download | go-e73c6c8808da281186a4d8f7107e34e9f7a4a9ee.tar.gz go-e73c6c8808da281186a4d8f7107e34e9f7a4a9ee.zip |
cmd/go: fix go work sync when there are zero workspace modules
go work sync panics when there are no workspace modules. This is
because the code that set the pruning mode only did so with modules
present. This change changes pruningForGoVersion to properly return
workspace pruning in workspace mode to prevent that. Another weird
scenario can happen when there are no workspace modules, but the
command-line-arguments module is created by default. Check for that
when iterating over the workspace modules to avoid trying to find the
nonexistant go.mod file for that modules.
Fixes #49591
Change-Id: Iee8bc92a8aaf9c440f88fe4f9ca908a8d461cd36
Reviewed-on: https://go-review.googlesource.com/c/go/+/365234
Trust: Michael Matloob <matloob@golang.org>
Trust: Bryan C. Mills <bcmills@google.com>
Run-TryBot: Michael Matloob <matloob@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
Diffstat (limited to 'src/cmd/go')
-rw-r--r-- | src/cmd/go/internal/modload/modfile.go | 3 | ||||
-rw-r--r-- | src/cmd/go/internal/workcmd/sync.go | 7 | ||||
-rw-r--r-- | src/cmd/go/testdata/script/work_sync_missing_module.txt | 12 |
3 files changed, 22 insertions, 0 deletions
diff --git a/src/cmd/go/internal/modload/modfile.go b/src/cmd/go/internal/modload/modfile.go index 40e6ed787d..7cc2272ea0 100644 --- a/src/cmd/go/internal/modload/modfile.go +++ b/src/cmd/go/internal/modload/modfile.go @@ -124,6 +124,9 @@ const ( ) func pruningForGoVersion(goVersion string) modPruning { + if inWorkspaceMode() { + return workspace + } if semver.Compare("v"+goVersion, ExplicitIndirectVersionV) < 0 { // The go.mod file does not duplicate relevant information about transitive // dependencies, so they cannot be pruned out. diff --git a/src/cmd/go/internal/workcmd/sync.go b/src/cmd/go/internal/workcmd/sync.go index 6f35dc4ff3..5f33e057f6 100644 --- a/src/cmd/go/internal/workcmd/sync.go +++ b/src/cmd/go/internal/workcmd/sync.go @@ -74,6 +74,13 @@ func runSync(ctx context.Context, cmd *base.Command, args []string) { workFilePath := modload.WorkFilePath() // save go.work path because EnterModule clobbers it. for _, m := range mms.Versions() { + if mms.ModRoot(m) == "" && m.Path == "command-line-arguments" { + // This is not a real module. + // TODO(#49228): Remove this special case once the special + // command-line-arguments module is gone. + continue + } + // Use EnterModule to reset the global state in modload to be in // single-module mode using the modroot of m. modload.EnterModule(ctx, mms.ModRoot(m)) diff --git a/src/cmd/go/testdata/script/work_sync_missing_module.txt b/src/cmd/go/testdata/script/work_sync_missing_module.txt new file mode 100644 index 0000000000..0018c733ee --- /dev/null +++ b/src/cmd/go/testdata/script/work_sync_missing_module.txt @@ -0,0 +1,12 @@ +# Ensure go work sync works without any modules in go.work. +go work sync + +# Ensure go work sync works even without a go.mod file. +rm go.mod +go work sync + +-- go.work -- +go 1.18 +-- go.mod -- +go 1.18 +module foo |