]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: fix go work sync when there are zero workspace modules
authorMichael Matloob <matloob@golang.org>
Thu, 18 Nov 2021 19:48:26 +0000 (14:48 -0500)
committerMichael Matloob <matloob@golang.org>
Mon, 22 Nov 2021 15:54:11 +0000 (15:54 +0000)
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>
src/cmd/go/internal/modload/modfile.go
src/cmd/go/internal/workcmd/sync.go
src/cmd/go/testdata/script/work_sync_missing_module.txt [new file with mode: 0644]

index 40e6ed787da8b763becca345c3030206395b3854..7cc2272ea03cb4ffdb88c786d0e4d7a347648883 100644 (file)
@@ -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.
index 6f35dc4ff3a659e24679656e17bd1f7c660a2100..5f33e057f6d58d79e236fd6e76b2879bded0b2d1 100644 (file)
@@ -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 (file)
index 0000000..0018c73
--- /dev/null
@@ -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