aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/go/internal/search/search.go
diff options
context:
space:
mode:
authorJay Conrod <jayconrod@google.com>2019-11-08 13:05:56 -0500
committerJay Conrod <jayconrod@google.com>2019-11-11 17:53:51 +0000
commit70be4819a47263055040d6beb5c0b1b31487f52c (patch)
treea0d2f013ad6efeb05c031c24af652095470121d5 /src/cmd/go/internal/search/search.go
parent275a7be3da9fded6ec71c92633bca30caf6dd93b (diff)
downloadgo-70be4819a47263055040d6beb5c0b1b31487f52c.tar.gz
go-70be4819a47263055040d6beb5c0b1b31487f52c.zip
cmd/go: fix windows test failures
search.CleanPatterns now preserves backslash separators in absolute paths in Windows. These had resulted in inconsistent error messages. search.MatchPackagesInFS is now more accepting of patterns with backslashes. It was inconsistent before. Several tests are fixed to work with Windows (mostly to match slashes or backslashes). Fixes #25300 Change-Id: Ibbf9ccd145353f7e3d345205c6fcc01d7066d1c2 Reviewed-on: https://go-review.googlesource.com/c/go/+/206144 Run-TryBot: Jay Conrod <jayconrod@google.com> Reviewed-by: Bryan C. Mills <bcmills@google.com>
Diffstat (limited to 'src/cmd/go/internal/search/search.go')
-rw-r--r--src/cmd/go/internal/search/search.go64
1 files changed, 40 insertions, 24 deletions
diff --git a/src/cmd/go/internal/search/search.go b/src/cmd/go/internal/search/search.go
index ef3835bfa4..ad33e60af1 100644
--- a/src/cmd/go/internal/search/search.go
+++ b/src/cmd/go/internal/search/search.go
@@ -125,32 +125,43 @@ func SetModRoot(dir string) {
modRoot = dir
}
-// MatchPackagesInFS is like allPackages but is passed a pattern
-// beginning ./ or ../, meaning it should scan the tree rooted
-// at the given directory. There are ... in the pattern too.
-// (See go help packages for pattern syntax.)
+// MatchPackagesInFS is like MatchPackages but is passed a pattern that
+// begins with an absolute path or "./" or "../". On Windows, the pattern may
+// use slash or backslash separators or a mix of both.
+//
+// MatchPackagesInFS scans the tree rooted at the directory that contains the
+// first "..." wildcard and returns a match with packages that
func MatchPackagesInFS(pattern string) *Match {
m := &Match{
Pattern: pattern,
Literal: false,
}
+ // Clean the path and create a matching predicate.
+ // filepath.Clean removes "./" prefixes (and ".\" on Windows). We need to
+ // preserve these, since they are meaningful in MatchPattern and in
+ // returned import paths.
+ cleanPattern := filepath.Clean(pattern)
+ isLocal := strings.HasPrefix(pattern, "./") || (os.PathSeparator == '\\' && strings.HasPrefix(pattern, `.\`))
+ prefix := ""
+ if cleanPattern != "." && isLocal {
+ prefix = "./"
+ cleanPattern = "." + string(os.PathSeparator) + cleanPattern
+ }
+ slashPattern := filepath.ToSlash(cleanPattern)
+ match := MatchPattern(slashPattern)
+
// Find directory to begin the scan.
// Could be smarter but this one optimization
// is enough for now, since ... is usually at the
// end of a path.
- i := strings.Index(pattern, "...")
- dir, _ := path.Split(pattern[:i])
+ i := strings.Index(cleanPattern, "...")
+ dir, _ := filepath.Split(cleanPattern[:i])
// pattern begins with ./ or ../.
// path.Clean will discard the ./ but not the ../.
// We need to preserve the ./ for pattern matching
// and in the returned import paths.
- prefix := ""
- if strings.HasPrefix(pattern, "./") {
- prefix = "./"
- }
- match := MatchPattern(pattern)
if modRoot != "" {
abs, err := filepath.Abs(dir)
@@ -381,21 +392,26 @@ func CleanPatterns(patterns []string) []string {
v = a[i:]
}
- // Arguments are supposed to be import paths, but
- // as a courtesy to Windows developers, rewrite \ to /
- // in command-line arguments. Handles .\... and so on.
- if filepath.Separator == '\\' {
- p = strings.ReplaceAll(p, `\`, `/`)
- }
+ // Arguments may be either file paths or import paths.
+ // As a courtesy to Windows developers, rewrite \ to /
+ // in arguments that look like import paths.
+ // Don't replace slashes in absolute paths.
+ if filepath.IsAbs(p) {
+ p = filepath.Clean(p)
+ } else {
+ if filepath.Separator == '\\' {
+ p = strings.ReplaceAll(p, `\`, `/`)
+ }
- // Put argument in canonical form, but preserve leading ./.
- if strings.HasPrefix(p, "./") {
- p = "./" + path.Clean(p)
- if p == "./." {
- p = "."
+ // Put argument in canonical form, but preserve leading ./.
+ if strings.HasPrefix(p, "./") {
+ p = "./" + path.Clean(p)
+ if p == "./." {
+ p = "."
+ }
+ } else {
+ p = path.Clean(p)
}
- } else {
- p = path.Clean(p)
}
out = append(out, p+v)