Skip to content

Commit 35268a9

Browse files
committed
cmd/go: additional doc-inspired tests and bug fixes
Additional tests and bug fixes realized while writing go.dev/doc/gotoolchain (CL 500775). - Handle go get [email protected] (resolve to latest patch release, same as go get [email protected]). (See modload/query.go and gover/mod.go.) - Handle go get go@patch toolchain@patch. (See modload/query.go and gover/mod.go.) - Remove prefix-goVERSION-suffix form for toolchain name, standardizing on goVERSION-suffix. I have no good explanation for having two forms, so simplify to one. (See vendor and gover.) - Fail toolchain downloads when GOSUMDB=off. Because toolchain downloads cannot always be predicted (especially during switching rather than selection), they cannot be listed in go.sum. We rely on the checksum database for integrity of the download, especially if proxied. If the checksum database is disabled, this integrity check won't happen, so fail toolchain downloads. (See modfetch/sumdb.go and script/gotoolchain_net.txt) - Use names from documentation in package toolchain (Select, Switch; SwitchTo renamed to Exec to avoid both names; reqs.go renamed to switch.go; toolchain.go renamed to select.go.) - Make "go env GOTOOLCHAIN" and "go env -w GOTOOLCHAIN" work even when GOTOOLCHAIN is misconfigured. (See special case at top of Select in select.go.) - Clarify what goInstallVersion does (report whether this is go install or go run pkg@version) and explain the potential version switch more clearly. Use the Switcher directly instead of reimplementing it. (See select.go.) - Document go@ and toolchain@ forms in go help get, linking to go.dev/doc/toolchain. (See modget/get.go.) - Update URL of documentation in $GOROOT/go.env. For #57001. Change-Id: I895ef3519ff95db8710ed23b36ebaf4f648120cb Reviewed-on: https://go-review.googlesource.com/c/go/+/500797 Reviewed-by: Michael Matloob <[email protected]> Run-TryBot: Russ Cox <[email protected]> TryBot-Bypass: Russ Cox <[email protected]>
1 parent bf01652 commit 35268a9

16 files changed

+359
-179
lines changed

go.env

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ GOPROXY=https://proxy.golang.org,direct
88
GOSUMDB=sum.golang.org
99

1010
# Automatically download newer toolchains as directed by go.mod files.
11-
# See https://go.dev/s/gotoolchain for details.
11+
# See https://go.dev/doc/toolchain for details.
1212
GOTOOLCHAIN=auto

src/cmd/go/alldocs.go

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cmd/go/internal/gover/mod.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ func ModIsValid(path, vers string) bool {
8484
// The caller is assumed to have checked that ModIsValid(path, vers) is true.
8585
func ModIsPrefix(path, vers string) bool {
8686
if IsToolchain(path) {
87+
if path == "toolchain" {
88+
return IsLang(FromToolchain(vers))
89+
}
8790
return IsLang(vers)
8891
}
8992
// Semver
@@ -110,3 +113,15 @@ func ModIsPrerelease(path, vers string) bool {
110113
}
111114
return semver.Prerelease(vers) != ""
112115
}
116+
117+
// ModMajorMinor returns the "major.minor" truncation of the version v,
118+
// for use as a prefix in "@patch" queries.
119+
func ModMajorMinor(path, vers string) string {
120+
if IsToolchain(path) {
121+
if path == "toolchain" {
122+
return "go" + Lang(FromToolchain(vers))
123+
}
124+
return Lang(vers)
125+
}
126+
return semver.MajorMinor(vers)
127+
}

src/cmd/go/internal/gover/toolchain.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,17 @@ import (
1414

1515
// FromToolchain returns the Go version for the named toolchain,
1616
// derived from the name itself (not by running the toolchain).
17-
// A toolchain is named "goVERSION" or "anything-goVERSION".
17+
// A toolchain is named "goVERSION".
1818
// A suffix after the VERSION introduced by a +, -, space, or tab is removed.
1919
// Examples:
2020
//
2121
// FromToolchain("go1.2.3") == "1.2.3"
2222
// FromToolchain("go1.2.3-bigcorp") == "1.2.3"
23-
// FromToolchain("gccgo-go1.23rc4") == "1.23rc4"
2423
// FromToolchain("invalid") == ""
2524
func FromToolchain(name string) string {
2625
var v string
2726
if strings.HasPrefix(name, "go") {
2827
v = name[2:]
29-
} else if i := strings.Index(name, "-go"); i >= 0 {
30-
v = name[i+3:]
3128
} else {
3229
return ""
3330
}

src/cmd/go/internal/gover/toolchain_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ var fromToolchainTests = []testCase1[string, string]{
1414
{"go1.2.3+bigcorp", ""},
1515
{"go1.2.3-bigcorp", "1.2.3"},
1616
{"go1.2.3-bigcorp more text", "1.2.3"},
17-
{"gccgo-go1.23rc4", "1.23rc4"},
18-
{"gccgo-go1.23rc4-bigdwarf", "1.23rc4"},
17+
{"gccgo-go1.23rc4", ""},
18+
{"gccgo-go1.23rc4-bigdwarf", ""},
1919
}

src/cmd/go/internal/modfetch/sumdb.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ import (
3333

3434
// useSumDB reports whether to use the Go checksum database for the given module.
3535
func useSumDB(mod module.Version) bool {
36+
if mod.Path == "golang.org/toolchain" {
37+
// Downloaded toolchains cannot be listed in go.sum,
38+
// so we require checksum database lookups even if
39+
// GOSUMDB=off or GONOSUMDB matches the pattern.
40+
// If GOSUMDB=off, then the eventual lookup will fail
41+
// with a good error message.
42+
return true
43+
}
3644
return cfg.GOSUMDB != "off" && !module.MatchPrefixPatterns(cfg.GONOSUMDB, mod.Path)
3745
}
3846

@@ -70,6 +78,10 @@ func dbDial() (dbName string, db *sumdb.Client, err error) {
7078
gosumdb = "sum.golang.org https://sum.golang.google.cn"
7179
}
7280

81+
if gosumdb == "off" {
82+
return "", nil, fmt.Errorf("checksum database disabled by GOSUMDB=off")
83+
}
84+
7385
key := strings.Fields(gosumdb)
7486
if len(key) >= 1 {
7587
if k := knownGOSUMDB[key[0]]; k != "" {

src/cmd/go/internal/modget/get.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ To remove a dependency on a module and downgrade modules that require it:
7272
7373
go get example.com/mod@none
7474
75+
To upgrade the minimum required Go version to the latest released Go version:
76+
77+
go get go@latest
78+
79+
To upgrade the Go toolchain to the latest patch release of the current Go toolchain:
80+
81+
go get toolchain@patch
82+
7583
See https://golang.org/ref/mod#go-get for details.
7684
7785
In earlier versions of Go, 'go get' was used to build and install packages.
@@ -106,6 +114,9 @@ from a repository.
106114
107115
For more about modules, see https://golang.org/ref/mod.
108116
117+
For more about using 'go get' to update the minimum Go version and
118+
suggested Go toolchain, see https://go.dev/doc/toolchain.
119+
109120
For more about specifying packages, see 'go help packages'.
110121
111122
This text describes the behavior of get using modules to manage source

src/cmd/go/internal/modload/query.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ func newQueryMatcher(path string, query, current string, allowed AllowedFunc) (*
393393
qm.mayUseLatest = true
394394
} else {
395395
qm.mayUseLatest = module.IsPseudoVersion(current)
396-
qm.prefix = semver.MajorMinor(current) + "."
396+
qm.prefix = gover.ModMajorMinor(qm.path, current) + "."
397397
qm.filter = func(mv string) bool { return gover.ModCompare(qm.path, mv, current) >= 0 }
398398
}
399399

0 commit comments

Comments
 (0)