Skip to content

Commit 8c8948c

Browse files
ConradIrwinmatloob
authored andcommitted
cmd/go: add support for go get -u tool
Change-Id: I14d20c6c77d0d0a83cb547d954ba7f244166bc43 Reviewed-on: https://go-review.googlesource.com/c/go/+/563176 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Sam Thanawalla <[email protected]> Reviewed-by: Michael Matloob <[email protected]>
1 parent 4a1167d commit 8c8948c

File tree

4 files changed

+49
-2
lines changed

4 files changed

+49
-2
lines changed

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ func runGet(ctx context.Context, cmd *base.Command, args []string) {
342342
r := newResolver(ctx, queries)
343343
r.performLocalQueries(ctx)
344344
r.performPathQueries(ctx)
345+
r.performToolQueries(ctx)
345346

346347
for {
347348
r.performWildcardQueries(ctx)
@@ -515,6 +516,7 @@ type resolver struct {
515516
pathQueries []*query // package path literal queries in original order
516517
wildcardQueries []*query // path wildcard queries in original order
517518
patternAllQueries []*query // queries with the pattern "all"
519+
toolQueries []*query // queries with the pattern "tool"
518520

519521
// Indexed "none" queries. These are also included in the slices above;
520522
// they are indexed here to speed up noneForPath.
@@ -574,6 +576,8 @@ func newResolver(ctx context.Context, queries []*query) *resolver {
574576
for _, q := range queries {
575577
if q.pattern == "all" {
576578
r.patternAllQueries = append(r.patternAllQueries, q)
579+
} else if q.pattern == "tool" {
580+
r.toolQueries = append(r.toolQueries, q)
577581
} else if q.patternIsLocal {
578582
r.localQueries = append(r.localQueries, q)
579583
} else if q.isWildcard() {
@@ -1050,6 +1054,19 @@ func (r *resolver) queryPath(ctx context.Context, q *query) {
10501054
})
10511055
}
10521056

1057+
// performToolQueries populates the candidates for each query whose
1058+
// pattern is "tool".
1059+
func (r *resolver) performToolQueries(ctx context.Context) {
1060+
for _, q := range r.toolQueries {
1061+
for tool := range modload.MainModules.Tools() {
1062+
q.pathOnce(tool, func() pathSet {
1063+
pkgMods, err := r.queryPackages(ctx, tool, q.version, r.initialSelected)
1064+
return pathSet{pkgMods: pkgMods, err: err}
1065+
})
1066+
}
1067+
}
1068+
}
1069+
10531070
// performPatternAllQueries populates the candidates for each query whose
10541071
// pattern is "all".
10551072
//

src/cmd/go/internal/modget/query.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,9 @@ func (q *query) validate() error {
199199

200200
if search.IsMetaPackage(q.pattern) && q.pattern != "all" {
201201
if q.pattern != q.raw {
202+
if q.pattern == "tool" {
203+
return fmt.Errorf("can't request explicit version of \"tool\" pattern")
204+
}
202205
return fmt.Errorf("can't request explicit version of standard-library pattern %q", q.pattern)
203206
}
204207
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
-- .info --
2+
{"Version": "v1.1.0"}
3+
-- .mod --
4+
module example.com/tools
5+
-- cmd/hello/hello.go --
6+
package main
7+
8+
import "fmt"
9+
10+
func main() {
11+
fmt.Println("hello v1.1")
12+
}

src/cmd/go/testdata/script/mod_get_tool.txt

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
# test go get -tool
2-
go get -tool example.com/tools/cmd/hello
2+
go get -tool example.com/tools/cmd/hello@v1.0.0
33
cmp go.mod go.mod.want
44

5+
go get -u tool
6+
cmp go.mod go.mod.upgraded
7+
58
# test -tool with @none
69
go get -tool example.com/tools/cmd/hello@none
710
cmp go.mod go.mod.gone
@@ -19,6 +22,10 @@ stderr 'can''t request explicit version "none" of path "./cmd/..." in main modul
1922
! go get -tool all
2023
stderr 'go get -tool does not work with "all"'
2124

25+
# test tool@none
26+
! go get tool@none
27+
stderr 'can''t request explicit version of "tool" pattern'
28+
2229
-- main.go --
2330
package main
2431

@@ -36,12 +43,20 @@ go 1.24
3643
tool example.com/tools/cmd/hello
3744

3845
require example.com/tools v1.0.0 // indirect
46+
-- go.mod.upgraded --
47+
module example.com/foo
48+
49+
go 1.24
50+
51+
tool example.com/tools/cmd/hello
52+
53+
require example.com/tools v1.1.0 // indirect
3954
-- go.mod.gone --
4055
module example.com/foo
4156

4257
go 1.24
4358

44-
require example.com/tools v1.0.0 // indirect
59+
require example.com/tools v1.1.0 // indirect
4560
-- go.mod.empty --
4661
module example.com/foo
4762

0 commit comments

Comments
 (0)