diff options
author | Russ Cox <rsc@golang.org> | 2023-05-23 21:12:23 -0400 |
---|---|---|
committer | Gopher Robot <gobot@golang.org> | 2023-05-25 17:51:28 +0000 |
commit | ff07c540b13983aff41e2af4196853602c1192dd (patch) | |
tree | df632b5289cc89e2d07b176d770f39882456d5b2 /src/cmd/go/internal/modload/load.go | |
parent | aa99c4d2925e3628460482db8657765880e6836c (diff) | |
download | go-ff07c540b13983aff41e2af4196853602c1192dd.tar.gz go-ff07c540b13983aff41e2af4196853602c1192dd.zip |
cmd/go: add go get go@version and toolchain@version
go get go@version and toolchain@version updates the
go and toolchain lines in go.mod. If toolchain ends up <= go,
it is dropped.
When the go version crosses certain version boundaries,
it may be necessary to run 'go mod tidy -go=version'.
That's left for a followup CL.
When the go or toolchain version ends up higher than the
current toolchain version, we cannot be sure we know how
to write the file out, so we fail with an error message.
In GOTOOLCHAIN auto mode, the newer toolchain should
be downloaded and reinvoked; that's left for a followup CL too.
For #57001.
Change-Id: Ibfdcc549b40555a53bdb2d019816d18f1bd16be6
Reviewed-on: https://go-review.googlesource.com/c/go/+/497081
TryBot-Result: Gopher Robot <gobot@golang.org>
Auto-Submit: Russ Cox <rsc@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
Reviewed-by: Bryan Mills <bcmills@google.com>
Diffstat (limited to 'src/cmd/go/internal/modload/load.go')
-rw-r--r-- | src/cmd/go/internal/modload/load.go | 24 |
1 files changed, 18 insertions, 6 deletions
diff --git a/src/cmd/go/internal/modload/load.go b/src/cmd/go/internal/modload/load.go index 6d620de0769..9eb9e6ddf8e 100644 --- a/src/cmd/go/internal/modload/load.go +++ b/src/cmd/go/internal/modload/load.go @@ -143,6 +143,9 @@ type PackageOpts struct { // module. GoVersion string + // TidyGo, if true, indicates that GoVersion is from the tidy -go= flag. + TidyGo bool + // Tags are the build tags in effect (as interpreted by the // cmd/go/internal/imports package). // If nil, treated as equivalent to imports.Tags(). @@ -338,7 +341,7 @@ func LoadPackages(ctx context.Context, opts PackageOpts, patterns ...string) (ma } } - initialRS := LoadModFile(ctx) + initialRS := loadModFile(ctx, &opts) ld := loadFromRoots(ctx, loaderParams{ PackageOpts: opts, @@ -407,6 +410,17 @@ func LoadPackages(ctx context.Context, opts PackageOpts, patterns ...string) (ma } } + // Update the go.mod file's Go version if necessary. + if modFile := ModFile(); modFile != nil && ld.GoVersion != "" { + mg, _ := ld.requirements.Graph(ctx) + if ld.TidyGo { + if v := mg.Selected("go"); gover.Compare(ld.GoVersion, v) < 0 { + base.Fatalf("go: cannot tidy -go=%v: dependencies require %v", ld.GoVersion, v) + } + } + modFile.AddGoStmt(ld.GoVersion) + } + if !ExplicitWriteGoMod { modfetch.TrimGoSum(keep) @@ -419,11 +433,6 @@ func LoadPackages(ctx context.Context, opts PackageOpts, patterns ...string) (ma base.Fatalf("go: %v", err) } } - - // Update the go.mod file's Go version if necessary. - if modFile := ModFile(); modFile != nil && ld.GoVersion != "" { - modFile.AddGoStmt(ld.GoVersion) - } } // Success! Update go.mod and go.sum (if needed) and return the results. @@ -628,6 +637,9 @@ var ( // if dir is in the module cache copy of a module in our build list. func pathInModuleCache(ctx context.Context, dir string, rs *Requirements) string { tryMod := func(m module.Version) (string, bool) { + if gover.IsToolchain(m.Path) { + return "", false + } var root string var err error if repl := Replacement(m); repl.Path != "" && repl.Version == "" { |