Skip to content

Commit d11e1f9

Browse files
author
Bryan C. Mills
committed
cmd/go/internal/search: consolidate package-pattern predicates into Match methods
This change consolidates predicates currently scattered throughout various parts of the package and module loader into methods on the search.Match type. That not only makes them more concise, but also encourages consistency, both in the code and in reasoning about the kinds of patterns that need to be handled. (For example, the IsLocal predicate was previously two different calls, either of which could be easily forgotten at a given call site.) Factored out from CL 185344 and CL 185345. Updates #32917 Change-Id: Ifa450ffaf6101f673e0ed69ced001a487d6f9335 Reviewed-on: https://go-review.googlesource.com/c/go/+/221458 Run-TryBot: Bryan C. Mills <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Michael Matloob <[email protected]> Reviewed-by: Jay Conrod <[email protected]>
1 parent 156c607 commit d11e1f9

File tree

6 files changed

+109
-90
lines changed

6 files changed

+109
-90
lines changed

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

+6-7
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ package get
77

88
import (
99
"fmt"
10-
"go/build"
1110
"os"
1211
"path/filepath"
1312
"runtime"
@@ -198,8 +197,8 @@ func downloadPaths(patterns []string) []string {
198197
}
199198
var pkgs []string
200199
for _, m := range search.ImportPathsQuiet(patterns) {
201-
if len(m.Pkgs) == 0 && strings.Contains(m.Pattern, "...") {
202-
pkgs = append(pkgs, m.Pattern)
200+
if len(m.Pkgs) == 0 && strings.Contains(m.Pattern(), "...") {
201+
pkgs = append(pkgs, m.Pattern())
203202
} else {
204203
pkgs = append(pkgs, m.Pkgs...)
205204
}
@@ -285,11 +284,11 @@ func download(arg string, parent *load.Package, stk *load.ImportStack, mode int)
285284
// We delay this until after reloadPackage so that the old entry
286285
// for p has been replaced in the package cache.
287286
if wildcardOkay && strings.Contains(arg, "...") {
288-
var match *search.Match
289-
if build.IsLocalImport(arg) {
290-
match = search.MatchPackagesInFS(arg)
287+
match := search.NewMatch(arg)
288+
if match.IsLocal() {
289+
match.MatchPackagesInFS()
291290
} else {
292-
match = search.MatchPackages(arg)
291+
match.MatchPackages()
293292
}
294293
args = match.Pkgs
295294
for _, err := range match.Errs {

src/cmd/go/internal/load/pkg.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -2074,13 +2074,13 @@ func PackagesAndErrors(patterns []string) []*Package {
20742074
for _, m := range matches {
20752075
for _, pkg := range m.Pkgs {
20762076
if pkg == "" {
2077-
panic(fmt.Sprintf("ImportPaths returned empty package for pattern %s", m.Pattern))
2077+
panic(fmt.Sprintf("ImportPaths returned empty package for pattern %s", m.Pattern()))
20782078
}
20792079
p := loadImport(pre, pkg, base.Cwd, nil, &stk, nil, 0)
2080-
p.Match = append(p.Match, m.Pattern)
2080+
p.Match = append(p.Match, m.Pattern())
20812081
p.Internal.CmdlinePkg = true
2082-
if m.Literal {
2083-
// Note: do not set = m.Literal unconditionally
2082+
if m.IsLiteral() {
2083+
// Note: do not set = m.IsLiteral unconditionally
20842084
// because maybe we'll see p matching both
20852085
// a literal and also a non-literal pattern.
20862086
p.Internal.CmdlinePkgLiteral = true

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ func runGet(cmd *base.Command, args []string) {
520520
// If the pattern did not match any packages, look up a new module.
521521
// If the pattern doesn't match anything on the last iteration,
522522
// we'll print a warning after the outer loop.
523-
if !search.IsRelativePath(arg.path) && !match.Literal && arg.path != "all" {
523+
if !search.IsRelativePath(arg.path) && !match.IsLiteral() && arg.path != "all" {
524524
addQuery(&query{querySpec: querySpec{path: arg.path, vers: arg.vers}, arg: arg.raw})
525525
} else {
526526
for _, err := range match.Errs {

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

+19-23
Original file line numberDiff line numberDiff line change
@@ -69,21 +69,20 @@ func ImportPathsQuiet(patterns []string, tags map[string]bool) []*search.Match {
6969
updateMatches := func(matches []*search.Match, iterating bool) {
7070
for i, m := range matches {
7171
switch {
72-
case build.IsLocalImport(m.Pattern) || filepath.IsAbs(m.Pattern):
72+
case m.IsLocal():
7373
// Evaluate list of file system directories on first iteration.
7474
if fsDirs == nil {
7575
fsDirs = make([][]string, len(matches))
7676
}
7777
if fsDirs[i] == nil {
78-
var dirs []string
79-
if m.Literal {
80-
dirs = []string{m.Pattern}
78+
if m.IsLiteral() {
79+
fsDirs[i] = []string{m.Pattern()}
8180
} else {
82-
match := search.MatchPackagesInFS(m.Pattern)
83-
dirs = match.Pkgs
84-
m.Errs = match.Errs
81+
m.MatchPackagesInFS()
82+
// Pull out the matching directories: we are going to resolve them
83+
// to package paths below.
84+
fsDirs[i], m.Pkgs = m.Pkgs, nil
8585
}
86-
fsDirs[i] = dirs
8786
}
8887

8988
// Make a copy of the directory list and translate to import paths.
@@ -92,9 +91,8 @@ func ImportPathsQuiet(patterns []string, tags map[string]bool) []*search.Match {
9291
// from not being in the build list to being in it and back as
9392
// the exact version of a particular module increases during
9493
// the loader iterations.
95-
m.Pkgs = str.StringList(fsDirs[i])
96-
pkgs := m.Pkgs
97-
m.Pkgs = m.Pkgs[:0]
94+
pkgs := str.StringList(fsDirs[i])
95+
m.Pkgs = pkgs[:0]
9896
for _, pkg := range pkgs {
9997
var dir string
10098
if !filepath.IsAbs(pkg) {
@@ -172,10 +170,13 @@ func ImportPathsQuiet(patterns []string, tags map[string]bool) []*search.Match {
172170
m.Pkgs = append(m.Pkgs, pkg)
173171
}
174172

175-
case strings.Contains(m.Pattern, "..."):
176-
m.Pkgs = matchPackages(m.Pattern, loaded.tags, true, buildList)
173+
case m.IsLiteral():
174+
m.Pkgs = []string{m.Pattern()}
177175

178-
case m.Pattern == "all":
176+
case strings.Contains(m.Pattern(), "..."):
177+
m.Pkgs = matchPackages(m.Pattern(), loaded.tags, true, buildList)
178+
179+
case m.Pattern() == "all":
179180
loaded.testAll = true
180181
if iterating {
181182
// Enumerate the packages in the main module.
@@ -187,15 +188,13 @@ func ImportPathsQuiet(patterns []string, tags map[string]bool) []*search.Match {
187188
m.Pkgs = loaded.computePatternAll(m.Pkgs)
188189
}
189190

190-
case search.IsMetaPackage(m.Pattern): // std, cmd
191+
case m.Pattern() == "std" || m.Pattern() == "cmd":
191192
if len(m.Pkgs) == 0 {
192-
match := search.MatchPackages(m.Pattern)
193-
m.Pkgs = match.Pkgs
194-
m.Errs = match.Errs
193+
m.MatchPackages() // Locate the packages within GOROOT/src.
195194
}
196195

197196
default:
198-
m.Pkgs = []string{m.Pattern}
197+
panic(fmt.Sprintf("internal error: modload missing case for pattern %s", m.Pattern()))
199198
}
200199
}
201200
}
@@ -204,10 +203,7 @@ func ImportPathsQuiet(patterns []string, tags map[string]bool) []*search.Match {
204203

205204
var matches []*search.Match
206205
for _, pattern := range search.CleanPatterns(patterns) {
207-
matches = append(matches, &search.Match{
208-
Pattern: pattern,
209-
Literal: !strings.Contains(pattern, "...") && !search.IsMetaPackage(pattern),
210-
})
206+
matches = append(matches, search.NewMatch(pattern))
211207
}
212208

213209
loaded = newLoader(tags)

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,8 @@ type QueryResult struct {
381381
// module and only the version "latest", without checking for other possible
382382
// modules.
383383
func QueryPackage(path, query string, allowed func(module.Version) bool) ([]QueryResult, error) {
384-
if search.IsMetaPackage(path) || strings.Contains(path, "...") {
384+
m := search.NewMatch(path)
385+
if m.IsLocal() || !m.IsLiteral() {
385386
return nil, fmt.Errorf("pattern %s is not an importable package", path)
386387
}
387388
return QueryPattern(path, query, allowed)

0 commit comments

Comments
 (0)