Skip to content

Commit

Permalink
Refactor match package code
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewhughes934 committed Mar 19, 2024
1 parent 4a2279e commit 7093c8e
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions src/cmd/go/internal/load/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,31 @@ func MatchPackage(pattern, cwd string) func(*Package) bool {
return false
}

cwdRel, err := filepath.Rel(cwd, p.Dir)
if err == nil {
if cwdRel != "." && !strings.HasPrefix(cwdRel, "../") {
for _, elem := range strings.Split(cwdRel, string(filepath.Separator)) {
// Avoid .foo, _foo, and testdata subdirectory trees.
if strings.HasPrefix(elem, ".") || strings.HasPrefix(elem, "_") || elem == "testdata" {
return false
}
relToWorkingDir, err := filepath.Rel(cwd, p.Dir)
if err != nil {
return matchPath(rel)
}

hasAnyPrefix := func(dir string, prefixes ...string) bool {
for _, prefix := range prefixes {
if strings.HasPrefix(dir, prefix) {
return true
}
}
return false
}

if relToWorkingDir == "." || hasAnyPrefix(relToWorkingDir, ".."+string(filepath.Separator)) {
// Not a special directory so can return immediately
return matchPath(rel)
}

// Otherwise avoid special directories "testdata" or beginning with ".", "_".
pathComponents := strings.Split(relToWorkingDir, string(filepath.Separator))
for _, elem := range pathComponents {
if hasAnyPrefix(elem, ".", "_") || elem == "testdata" {
return false
}
}

return matchPath(rel)
Expand Down

0 comments on commit 7093c8e

Please sign in to comment.