Skip to content

Commit 7833302

Browse files
author
Jay Conrod
committed
cmd/go: ignore '@' when cleaning local and absolute file path args
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 golang#35115 Change-Id: Ia7d37e0a2737442d4f1796cc2fc3a59237a8ddfe Reviewed-on: https://go-review.googlesource.com/c/go/+/202761 Run-TryBot: Jay Conrod <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Bryan C. Mills <[email protected]>
1 parent 67fb553 commit 7833302

3 files changed

Lines changed: 42 additions & 7 deletions

File tree

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -361,18 +361,20 @@ func ImportPathsQuiet(patterns []string) []*Match {
361361
return out
362362
}
363363

364-
// CleanPatterns returns the patterns to use for the given
365-
// command line. It canonicalizes the patterns but does not
366-
// evaluate any matches. It preserves text after '@' for commands
367-
// that accept versions.
364+
// CleanPatterns returns the patterns to use for the given command line. It
365+
// canonicalizes the patterns but does not evaluate any matches. For patterns
366+
// that are not local or absolute paths, it preserves text after '@' to avoid
367+
// modifying version queries.
368368
func CleanPatterns(patterns []string) []string {
369369
if len(patterns) == 0 {
370370
return []string{"."}
371371
}
372372
var out []string
373373
for _, a := range patterns {
374374
var p, v string
375-
if i := strings.IndexByte(a, '@'); i < 0 {
375+
if build.IsLocalImport(a) || filepath.IsAbs(a) {
376+
p = a
377+
} else if i := strings.IndexByte(a, '@'); i < 0 {
376378
p = a
377379
} else {
378380
p = a[:i]

src/cmd/go/script_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ func (ts *testScript) setup() {
117117
"GOSUMDB=" + testSumDBVerifierKey,
118118
"GONOPROXY=",
119119
"GONOSUMDB=",
120+
"PWD=" + ts.cd,
120121
tempEnvName() + "=" + filepath.Join(ts.workdir, "tmp"),
121122
"devnull=" + os.DevNull,
122123
"goversion=" + goVersion(ts),
@@ -414,6 +415,7 @@ func (ts *testScript) cmdCd(neg bool, args []string) {
414415
ts.fatalf("%s is not a directory", dir)
415416
}
416417
ts.cd = dir
418+
ts.envMap["PWD"] = dir
417419
fmt.Fprintf(&ts.log, "%s\n", ts.cd)
418420
}
419421

src/cmd/go/testdata/script/mod_fs_patterns.txt

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
# File system pattern searches should skip sub-modules and vendor directories.
2-
31
env GO111MODULE=on
42

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

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

42+
43+
# File system paths and patterns should allow the '@' character.
44+
cd ../@at
45+
go list $PWD
46+
stdout '^at$'
47+
go list $PWD/...
48+
stdout '^at$'
49+
50+
# The '@' character is not allowed in directory paths that are part of
51+
# a package path.
52+
cd ../badat/bad@
53+
! go list .
54+
stderr 'directory . outside available modules'
55+
! go list $PWD
56+
stderr 'directory . outside available modules'
57+
! go list $PWD/...
58+
stderr 'directory . outside available modules'
59+
4360
-- x/go.mod --
4461
module m
4562

@@ -64,3 +81,17 @@ package z
6481

6582
-- x/y/z/w/w.go --
6683
package w
84+
85+
-- @at/go.mod --
86+
module at
87+
88+
go 1.14
89+
-- @at/at.go --
90+
package at
91+
92+
-- badat/go.mod --
93+
module badat
94+
95+
go 1.14
96+
-- badat/bad@/bad.go --
97+
package bad

0 commit comments

Comments
 (0)