From 5dcc04aeacdaa78cc431e3f8b2119d2f351685b5 Mon Sep 17 00:00:00 2001 From: Michael Matloob Date: Tue, 23 May 2023 16:13:13 -0400 Subject: [PATCH] cmd/go/internal/modload: make it clear -mod can't be set in some cases We check that -mod can't be set to mod in workspace mode, but then we set BuildMod to mod for go work sync below. Make it clear that that's okay because we can't pass -mod=mod to go work sync (or the other go mod commands that can run in workspace mode that set mod=mod: go mod graph, go mod verify, and go mod why). Change-Id: Idfe6fea6a420211886e4f838e050be4bf7d1b71d Reviewed-on: https://go-review.googlesource.com/c/go/+/497617 Reviewed-by: Ian Lance Taylor Reviewed-by: Bryan Mills LUCI-TryBot-Result: Go LUCI --- src/cmd/go/internal/modload/init.go | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/cmd/go/internal/modload/init.go b/src/cmd/go/internal/modload/init.go index f4f4a68254..23db438da1 100644 --- a/src/cmd/go/internal/modload/init.go +++ b/src/cmd/go/internal/modload/init.go @@ -1343,9 +1343,16 @@ func appendGoAndToolchainRoots(roots []module.Version, goVersion, toolchain stri func setDefaultBuildMod() { if cfg.BuildModExplicit { if inWorkspaceMode() && cfg.BuildMod != "readonly" && cfg.BuildMod != "vendor" { - base.Fatalf("go: -mod may only be set to readonly or vendor when in workspace mode, but it is set to %q"+ - "\n\tRemove the -mod flag to use the default readonly value, "+ - "\n\tor set GOWORK=off to disable workspace mode.", cfg.BuildMod) + switch cfg.CmdName { + case "work sync", "mod graph", "mod verify", "mod why": + // These commands run with BuildMod set to mod, but they don't take the + // -mod flag, so we should never get here. + panic("in workspace mode and -mod was set explicitly, but command doesn't support setting -mod") + default: + base.Fatalf("go: -mod may only be set to readonly or vendor when in workspace mode, but it is set to %q"+ + "\n\tRemove the -mod flag to use the default readonly value, "+ + "\n\tor set GOWORK=off to disable workspace mode.", cfg.BuildMod) + } } // Don't override an explicit '-mod=' argument. return -- 2.48.1