Skip to content

Commit

Permalink
cmd/go: ignore '@' when cleaning local and absolute file path args
Browse files Browse the repository at this point in the history
Since CL 194600, search.CleanPaths preserves characters after '@' in
each argument. This was done so that paths could be cleaned while
version queries were preserved. However, local and absolute file paths
may contain '@' characters.

With this change, '@' is treated as a normal character by
search.CleanPaths in local and absolute paths.

Fixes #35115

Change-Id: Ia7d37e0a2737442d4f1796cc2fc3a59237a8ddfe
Reviewed-on: https://go-review.googlesource.com/c/go/+/202761
Run-TryBot: Jay Conrod <jayconrod@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
  • Loading branch information
Jay Conrod committed Oct 23, 2019
1 parent 67fb553 commit 7833302
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 7 deletions.
12 changes: 7 additions & 5 deletions src/cmd/go/internal/search/search.go
Expand Up @@ -361,18 +361,20 @@ func ImportPathsQuiet(patterns []string) []*Match {
return out
}

// CleanPatterns returns the patterns to use for the given
// command line. It canonicalizes the patterns but does not
// evaluate any matches. It preserves text after '@' for commands
// that accept versions.
// CleanPatterns returns the patterns to use for the given command line. It
// canonicalizes the patterns but does not evaluate any matches. For patterns
// that are not local or absolute paths, it preserves text after '@' to avoid
// modifying version queries.
func CleanPatterns(patterns []string) []string {
if len(patterns) == 0 {
return []string{"."}
}
var out []string
for _, a := range patterns {
var p, v string
if i := strings.IndexByte(a, '@'); i < 0 {
if build.IsLocalImport(a) || filepath.IsAbs(a) {
p = a
} else if i := strings.IndexByte(a, '@'); i < 0 {
p = a
} else {
p = a[:i]
Expand Down
2 changes: 2 additions & 0 deletions src/cmd/go/script_test.go
Expand Up @@ -117,6 +117,7 @@ func (ts *testScript) setup() {
"GOSUMDB=" + testSumDBVerifierKey,
"GONOPROXY=",
"GONOSUMDB=",
"PWD=" + ts.cd,
tempEnvName() + "=" + filepath.Join(ts.workdir, "tmp"),
"devnull=" + os.DevNull,
"goversion=" + goVersion(ts),
Expand Down Expand Up @@ -414,6 +415,7 @@ func (ts *testScript) cmdCd(neg bool, args []string) {
ts.fatalf("%s is not a directory", dir)
}
ts.cd = dir
ts.envMap["PWD"] = dir
fmt.Fprintf(&ts.log, "%s\n", ts.cd)
}

Expand Down
35 changes: 33 additions & 2 deletions src/cmd/go/testdata/script/mod_fs_patterns.txt
@@ -1,7 +1,6 @@
# File system pattern searches should skip sub-modules and vendor directories.

env GO111MODULE=on

# File system pattern searches should skip sub-modules and vendor directories.
cd x

# all packages
Expand Down Expand Up @@ -40,6 +39,24 @@ stderr '^can.t load package: package ./nonexist: cannot find package "." in:\n\t
! stderr 'import lookup disabled'
stderr 'can.t load package: package ./go.mod: cannot find package'


# File system paths and patterns should allow the '@' character.
cd ../@at
go list $PWD
stdout '^at$'
go list $PWD/...
stdout '^at$'

# The '@' character is not allowed in directory paths that are part of
# a package path.
cd ../badat/bad@
! go list .
stderr 'directory . outside available modules'
! go list $PWD
stderr 'directory . outside available modules'
! go list $PWD/...
stderr 'directory . outside available modules'

-- x/go.mod --
module m

Expand All @@ -64,3 +81,17 @@ package z

-- x/y/z/w/w.go --
package w

-- @at/go.mod --
module at

go 1.14
-- @at/at.go --
package at

-- badat/go.mod --
module badat

go 1.14
-- badat/bad@/bad.go --
package bad

0 comments on commit 7833302

Please sign in to comment.