Skip to content

Commit 24d2235

Browse files
committed
cmd/go: add 'work' package pattern
The 'work' package pattern will resolve to the set of packages in the work (formerly called main) modules. It's essentially 'all', but without the dependencies. And the implementation is similar to that of 'all', except that we don't expand to the dependencies. Fixes #71294 Change-Id: I3d02beb74fa4e5c6de2290e24eedc51745d13080 Reviewed-on: https://go-review.googlesource.com/c/go/+/643235 Reviewed-by: Alan Donovan <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 9bd2160 commit 24d2235

File tree

5 files changed

+136
-2
lines changed

5 files changed

+136
-2
lines changed

src/cmd/go/internal/load/search.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"path/filepath"
99
"strings"
1010

11+
"cmd/go/internal/modload"
1112
"cmd/go/internal/search"
1213
"cmd/internal/pkgpattern"
1314
)
@@ -45,11 +46,23 @@ func MatchPackage(pattern, cwd string) func(*Package) bool {
4546
return matchPath(rel)
4647
}
4748
case pattern == "all":
49+
// This is slightly inaccurate: it matches every package, which isn't the same
50+
// as matching the "all" package pattern.
51+
// TODO(matloob): Should we make this more accurate? Does anyone depend on this behavior?
4852
return func(p *Package) bool { return true }
4953
case pattern == "std":
5054
return func(p *Package) bool { return p.Standard }
5155
case pattern == "cmd":
5256
return func(p *Package) bool { return p.Standard && strings.HasPrefix(p.ImportPath, "cmd/") }
57+
case pattern == "tool" && modload.Enabled():
58+
return func(p *Package) bool {
59+
return modload.MainModules.Tools()[p.ImportPath]
60+
}
61+
case pattern == "work" && modload.Enabled():
62+
return func(p *Package) bool {
63+
return p.Module != nil && modload.MainModules.Contains(p.Module.Path)
64+
}
65+
5366
default:
5467
matchPath := pkgpattern.MatchPattern(pattern)
5568
return func(p *Package) bool { return matchPath(p.ImportPath) }

src/cmd/go/internal/modload/load.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,13 @@ func LoadPackages(ctx context.Context, opts PackageOpts, patterns ...string) (ma
323323
}
324324
matchPackages(ctx, m, opts.Tags, includeStd, mg.BuildList())
325325

326+
case m.Pattern() == "work":
327+
matchModules := MainModules.Versions()
328+
if opts.MainModule != (module.Version{}) {
329+
matchModules = []module.Version{opts.MainModule}
330+
}
331+
matchPackages(ctx, m, opts.Tags, omitStd, matchModules)
332+
326333
case m.Pattern() == "all":
327334
if ld == nil {
328335
// The initial roots are the packages and tools in the main module.

src/cmd/go/internal/search/search.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,14 @@ func (m *Match) IsLocal() bool {
6060
}
6161

6262
// IsMeta reports whether the pattern is a “meta-package” keyword that represents
63-
// multiple packages, such as "std", "cmd", "tool", or "all".
63+
// multiple packages, such as "std", "cmd", "tool", "work", or "all".
6464
func (m *Match) IsMeta() bool {
6565
return IsMetaPackage(m.pattern)
6666
}
6767

6868
// IsMetaPackage checks if name is a reserved package name that expands to multiple packages.
6969
func IsMetaPackage(name string) bool {
70-
return name == "std" || name == "cmd" || name == "tool" || name == "all"
70+
return name == "std" || name == "cmd" || name == "tool" || name == "work" || name == "all"
7171
}
7272

7373
// A MatchError indicates an error that occurred while attempting to match a
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Test the work and tool patterns in a per-package flag
2+
3+
go build -n '-gcflags=work=-fakeflag' example.com/foo/a
4+
stderr 'compile.*-p example.com/foo/a.*-fakeflag'
5+
! stderr 'compile.*-p example.com/dep.*-fakeflag'
6+
7+
go build -n '-gcflags=tool=-fakeflag' example.com/foo/a example.com/dep/tooldep
8+
! stderr 'compile.*-p example.com/foo/a.*-fakeflag'
9+
! stderr 'compile.*-p example.com/dep.*-fakeflag'
10+
stderr 'compile.*-p main.*-fakeflag.*main.go'
11+
12+
-- go.mod --
13+
module example.com/foo
14+
15+
go 1.24
16+
17+
tool example.com/dep/tooldep
18+
19+
require example.com/dep v1.0.0
20+
21+
replace example.com/dep => ./dep
22+
-- a/a.go --
23+
package a
24+
25+
import _ "example.com/dep"
26+
-- dep/go.mod --
27+
module example.com/dep
28+
29+
go 1.24
30+
-- dep/dep.go --
31+
package dep
32+
-- dep/tooldep/main.go --
33+
package main
34+
35+
import _ "example.com/dep"
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
cd m
2+
go list all
3+
stdout 'example.com/dep'
4+
stdout 'example.com/m/a'
5+
stdout 'example.com/m/b'
6+
go list work
7+
! stdout 'example.com/dep'
8+
stdout 'example.com/m/a'
9+
stdout 'example.com/m/b'
10+
11+
cd ../n
12+
go list all
13+
stdout 'example.com/n/c'
14+
stdout 'example.com/n/d'
15+
stdout 'unsafe'
16+
go list work
17+
stdout 'example.com/n/c'
18+
stdout 'example.com/n/d'
19+
! stdout 'unsafe'
20+
21+
cd ../w
22+
go list all
23+
stdout 'example.com/dep'
24+
stdout 'example.com/m/a'
25+
stdout 'example.com/m/b'
26+
stdout 'example.com/n/c'
27+
stdout 'example.com/n/d'
28+
stdout 'unsafe'
29+
go list work
30+
! stdout 'example.com/dep'
31+
stdout 'example.com/m/a'
32+
stdout 'example.com/m/b'
33+
stdout 'example.com/n/c'
34+
stdout 'example.com/n/d'
35+
! stdout 'unsafe'
36+
37+
-- m/go.mod --
38+
module example.com/m
39+
40+
go 1.24
41+
42+
require example.com/dep v1.0.0
43+
replace example.com/dep v1.0.0 => ../dep
44+
-- m/a/a.go --
45+
package a
46+
-- m/b/b.go --
47+
package b
48+
49+
import _ "example.com/dep"
50+
-- n/go.mod --
51+
module example.com/n
52+
53+
go 1.24
54+
-- n/c/c.go --
55+
package c
56+
-- n/d/d.go --
57+
package d
58+
59+
import _ "unsafe"
60+
-- w/go.work --
61+
go 1.24
62+
63+
use (
64+
../m
65+
../n
66+
)
67+
-- dep/go.mod --
68+
module example.com/dep
69+
70+
go 1.24
71+
-- dep/dep.go --
72+
package dep
73+
-- want_w_all.txt --
74+
example.com/dep
75+
example.com/work/a
76+
example.com/work/b
77+
-- want_w_all.txt --
78+
example.com/work/a
79+
example.com/work/b

0 commit comments

Comments
 (0)