Skip to content

Commit

Permalink
cmd/go: fix go get go@badversion
Browse files Browse the repository at this point in the history
It was panicking instead of printing a nice error.

Fixes golang#61258.
Fixes golang#61259.

Change-Id: Ia30853db5bc7f1f2a4c7e91169c659ae2b79adcb
Reviewed-on: https://go-review.googlesource.com/c/go/+/509097
Run-TryBot: Russ Cox <[email protected]>
TryBot-Result: Gopher Robot <[email protected]>
Reviewed-by: Bryan Mills <[email protected]>
  • Loading branch information
rsc authored and bradfitz committed Jul 15, 2023
1 parent b995362 commit bdc95d9
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 17 deletions.
30 changes: 23 additions & 7 deletions src/cmd/go/internal/modfetch/toolchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ func (r *toolchainRepo) Versions(ctx context.Context, prefix string) (*Versions,
}
}

// Always include our own version.
// This means that the development branch of Go 1.21 (say) will allow 'go get [email protected]'
// even though there are no Go 1.21 releases yet.
// Once there is a release, 1.21 will be treated as a query matching the latest available release.
// Before then, 1.21 will be treated as a query that resolves to this entry we are adding (1.21).
if v := gover.Local(); !have[v] {
list = append(list, goPrefix+v)
}

if r.path == "go" {
sort.Slice(list, func(i, j int) bool {
return gover.Compare(list[i], list[j]) < 0
Expand All @@ -74,20 +83,27 @@ func (r *toolchainRepo) Versions(ctx context.Context, prefix string) (*Versions,
}

func (r *toolchainRepo) Stat(ctx context.Context, rev string) (*RevInfo, error) {
// If we're asking about "go" (not "toolchain"), pretend to have
// all earlier Go versions available without network access:
// we will provide those ourselves, at least in GOTOOLCHAIN=auto mode.
if r.path == "go" && gover.Compare(rev, gover.Local()) <= 0 {
return &RevInfo{Version: rev}, nil
}

// Convert rev to DL version and stat that to make sure it exists.
// In theory the go@ versions should be like 1.21.0
// and the toolchain@ versions should be like go1.21.0
// but people will type the wrong one, and so we accept
// both and silently correct it to the standard form.
prefix := ""
v := rev
v = strings.TrimPrefix(v, "go")
if r.path == "toolchain" {
prefix = "go"
}

if !gover.IsValid(v) {
return nil, fmt.Errorf("invalid %s version %s", r.path, rev)
}
// If we're asking about "go" (not "toolchain"), pretend to have
// all earlier Go versions available without network access:
// we will provide those ourselves, at least in GOTOOLCHAIN=auto mode.
if r.path == "go" && gover.Compare(v, gover.Local()) <= 0 {
return &RevInfo{Version: prefix + v}, nil
}
if gover.IsLang(v) {
return nil, fmt.Errorf("go language version %s is not a toolchain version", rev)
}
Expand Down
5 changes: 4 additions & 1 deletion src/cmd/go/internal/modget/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,10 +239,13 @@ func (q *query) matchesPath(path string) bool {
// canMatchInModule reports whether the given module path can potentially
// contain q.pattern.
func (q *query) canMatchInModule(mPath string) bool {
if gover.IsToolchain(mPath) {
return false
}
if q.canMatchWildcardInModule != nil {
return q.canMatchWildcardInModule(mPath)
}
return str.HasPathPrefix(q.pattern, mPath) && !gover.IsToolchain(mPath)
return str.HasPathPrefix(q.pattern, mPath)
}

// pathOnce invokes f to generate the pathSet for the given path,
Expand Down
6 changes: 5 additions & 1 deletion src/cmd/go/internal/modload/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,11 @@ func newQueryMatcher(path string, query, current string, allowed AllowedFunc) (*
// AllowedFunc of qm.
func (qm *queryMatcher) allowsVersion(ctx context.Context, v string) bool {
if qm.prefix != "" && !strings.HasPrefix(v, qm.prefix) {
return false
if gover.IsToolchain(qm.path) && strings.TrimSuffix(qm.prefix, ".") == v {
// Allow 1.21 to match "1.21." prefix.
} else {
return false
}
}
if qm.filter != nil && !qm.filter(v) {
return false
Expand Down
43 changes: 35 additions & 8 deletions src/cmd/go/testdata/script/mod_get_toolchain.txt
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
# setup
env TESTGO_VERSION=go1.99.0
env TESTGO_VERSION=go1.99rc1
env TESTGO_VERSION_SWITCH=switch

# go get go should use the latest Go 1.23
cp go.mod.orig go.mod
go get go
stderr '^go: upgraded go 1.21 => 1.23.9$'
grep 'go 1.23.9' go.mod
grep 'toolchain go1.99.0' go.mod
grep 'toolchain go1.99rc1' go.mod

# go get [email protected] should use the latest Go 1.23
cp go.mod.orig go.mod
go get [email protected]
stderr '^go: upgraded go 1.21 => 1.23.9$'
grep 'go 1.23.9' go.mod
grep 'toolchain go1.99.0' go.mod
grep 'toolchain go1.99rc1' go.mod

# go get [email protected] should use the latest Go 1.22
cp go.mod.orig go.mod
go get [email protected]
stderr '^go: upgraded go 1.21 => 1.22.9$'
grep 'go 1.22.9' go.mod
grep 'toolchain go1.99.0' go.mod
grep 'toolchain go1.99rc1' go.mod

# go get go@patch should use the latest patch release
go get [email protected]
go get go@patch
stderr '^go: upgraded go 1.22.1 => 1.22.9$'
grep 'go 1.22.9' go.mod
grep 'toolchain go1.99.0' go.mod
grep 'toolchain go1.99rc1' go.mod

# go get [email protected] does NOT find the release candidate
cp go.mod.orig go.mod
Expand All @@ -40,20 +40,20 @@ cp go.mod.orig go.mod
go get [email protected]
stderr '^go: upgraded go 1.21 => 1.24rc1$'
grep 'go 1.24rc1' go.mod
grep 'toolchain go1.99.0' go.mod
grep 'toolchain go1.99rc1' go.mod

# go get go@latest finds the latest Go 1.23
cp go.mod.orig go.mod
go get go@latest
stderr '^go: upgraded go 1.21 => 1.23.9$'
grep 'go 1.23.9' go.mod
grep 'toolchain go1.99.0' go.mod
grep 'toolchain go1.99rc1' go.mod

# Again, with toolchains.

# go get toolchain should find go1.999testmod.
go get toolchain
stderr '^go: upgraded toolchain go1.99.0 => go1.999testmod$'
stderr '^go: upgraded toolchain go1.99rc1 => go1.999testmod$'
grep 'go 1.23.9' go.mod
grep 'toolchain go1.999testmod' go.mod

Expand Down Expand Up @@ -96,6 +96,33 @@ stderr '^go: added toolchain go1.999testmod$'
grep 'go 1.21' go.mod
grep 'toolchain go1.999testmod' go.mod

# Bug fixes.

# go get go@garbage should fail but not crash
! go get go@garbage
! stderr panic
stderr '^go: invalid go version garbage$'

# go get [email protected] is OK - we silently correct to 1.21.0
go get [email protected]
go get [email protected]
stderr '^go: upgraded go 1.19 => 1.21.0'

# go get [email protected] is OK too.
go get [email protected]
stderr '^go: downgraded toolchain go1.999testmod => go1.24rc1$'

# go get [email protected] should work if we are the Go 1.21 language version,
# even though there's no toolchain for it.
# (Older versions resolve to the latest release in that version, so for example
# go get [email protected] might resolve to 1.20.9, but if we're the devel copy of
# Go 1.21, there's no release yet to resolve to, so we resolve to ourselves.)
env TESTGO_VERSION=go1.21
go get [email protected] toolchain@none
go get [email protected]
grep 'go 1.21$' go.mod
! grep toolchain go.mod

-- go.mod.orig --
module m

Expand Down

0 comments on commit bdc95d9

Please sign in to comment.