]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: clean up handling of go mod tidy -go=v during modload
authorRuss Cox <rsc@golang.org>
Wed, 31 May 2023 13:38:29 +0000 (09:38 -0400)
committerGopher Robot <gobot@golang.org>
Wed, 31 May 2023 15:21:15 +0000 (15:21 +0000)
Slight cleanup of the special case for "mod tidy" during modload
of a module with no go version. Now it's clearer what is special.

For #57001.

Change-Id: I4a3ad0a948ccb5c62365f2a7f7f7de5582cc57c9
Reviewed-on: https://go-review.googlesource.com/c/go/+/499535
Reviewed-by: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Auto-Submit: Russ Cox <rsc@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>

src/cmd/go/internal/modload/init.go

index 2e833a979a51e905ca7f0dc2b6850dbe64ad9fd9..1a2a516e728f40e70500524b51a2ad93c6876777 100644 (file)
@@ -849,25 +849,24 @@ func loadModFile(ctx context.Context, opts *PackageOpts) *Requirements {
                // cfg.CmdName directly here.
                if cfg.BuildMod == "mod" && cfg.CmdName != "mod graph" && cfg.CmdName != "mod why" {
                        // go line is missing from go.mod; add one there and add to derived requirements.
-                       addGoStmt(MainModules.ModFile(mainModule), mainModule, gover.Local())
-                       if cfg.CmdName != "mod tidy" {
-                               // We want to add the "go" line to the module load in general,
-                               // if we do it in "mod tidy", then go mod tidy -go=older for some older version
-                               // when we are in a module with no go line will see gover.Local() in the
-                               // requirement graph and then report that -go=older is invalid.
-                               // go test -run=Script/mod_tidy_version will fail without the tidy exclusion.
-                               rs = overrideRoots(ctx, rs, []module.Version{{Path: "go", Version: gover.Local()}})
+                       v := gover.Local()
+                       if opts != nil && opts.TidyGo {
+                               v = opts.GoVersion
                        }
+                       addGoStmt(MainModules.ModFile(mainModule), mainModule, v)
+                       rs = overrideRoots(ctx, rs, []module.Version{{Path: "go", Version: v}})
 
                        // We need to add a 'go' version to the go.mod file, but we must assume
                        // that its existing contents match something between Go 1.11 and 1.16.
                        // Go 1.11 through 1.16 do not support graph pruning, but the latest Go
                        // version uses a pruned module graph — so we need to convert the
                        // requirements to support pruning.
-                       var err error
-                       rs, err = convertPruning(ctx, rs, pruned)
-                       if err != nil {
-                               base.Fatalf("go: %v", err)
+                       if gover.Compare(v, ExplicitIndirectVersion) >= 0 {
+                               var err error
+                               rs, err = convertPruning(ctx, rs, pruned)
+                               if err != nil {
+                                       base.Fatalf("go: %v", err)
+                               }
                        }
                } else {
                        rawGoVersion.Store(mainModule, modFileGoVersion(MainModules.ModFile(mainModule)))