Skip to content

Commit 760636d

Browse files
committed
cmd/go: ignore dot and underscore files in fmt, fix, and get -fix
No test because as far as I can tell, there aren't existing tests for these. Fixes #18383 Change-Id: I06eaef05777a1474886167e3797c5bcd93189d1b Reviewed-on: https://go-review.googlesource.com/45156 Run-TryBot: Brad Fitzpatrick <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent a48998b commit 760636d

File tree

4 files changed

+28
-3
lines changed

4 files changed

+28
-3
lines changed

src/cmd/go/internal/base/path.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,28 @@ func RelPaths(paths []string) []string {
4444
return out
4545
}
4646

47+
// FilterDotUnderscoreFiles returns a slice containing all elements
48+
// of path whose base name doesn't begin with "." or "_".
49+
func FilterDotUnderscoreFiles(path []string) []string {
50+
var out []string // lazily initialized
51+
for i, p := range path {
52+
base := filepath.Base(p)
53+
if strings.HasPrefix(base, ".") || strings.HasPrefix(base, "_") {
54+
if out == nil {
55+
out = append(make([]string, 0, len(path)), path[:i]...)
56+
}
57+
continue
58+
}
59+
if out != nil {
60+
out = append(out, p)
61+
}
62+
}
63+
if out == nil {
64+
return path
65+
}
66+
return out
67+
}
68+
4769
// IsTestFile reports whether the source file is a set of tests and should therefore
4870
// be excluded from coverage analysis.
4971
func IsTestFile(file string) bool {

src/cmd/go/internal/fix/fix.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ func runFix(cmd *base.Command, args []string) {
3333
// Use pkg.gofiles instead of pkg.Dir so that
3434
// the command only applies to this package,
3535
// not to packages in subdirectories.
36-
base.Run(str.StringList(cfg.BuildToolexec, base.Tool("fix"), base.RelPaths(pkg.Internal.AllGoFiles)))
36+
files := base.FilterDotUnderscoreFiles(base.RelPaths(pkg.Internal.AllGoFiles))
37+
base.Run(str.StringList(cfg.BuildToolexec, base.Tool("fix"), files))
3738
}
3839
}

src/cmd/go/internal/fmtcmd/fmt.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ func runFmt(cmd *base.Command, args []string) {
4545
// Use pkg.gofiles instead of pkg.Dir so that
4646
// the command only applies to this package,
4747
// not to packages in subdirectories.
48-
base.Run(str.StringList(gofmt, "-l", "-w", base.RelPaths(pkg.Internal.AllGoFiles)))
48+
files := base.FilterDotUnderscoreFiles(base.RelPaths(pkg.Internal.AllGoFiles))
49+
base.Run(str.StringList(gofmt, "-l", "-w", files))
4950
}
5051
}
5152

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,8 @@ func download(arg string, parent *load.Package, stk *load.ImportStack, mode int)
298298
// due to wildcard expansion.
299299
for _, p := range pkgs {
300300
if *getFix {
301-
base.Run(cfg.BuildToolexec, str.StringList(base.Tool("fix"), base.RelPaths(p.Internal.AllGoFiles)))
301+
files := base.FilterDotUnderscoreFiles(base.RelPaths(p.Internal.AllGoFiles))
302+
base.Run(cfg.BuildToolexec, str.StringList(base.Tool("fix"), files))
302303

303304
// The imports might have changed, so reload again.
304305
p = load.ReloadPackage(arg, stk)

0 commit comments

Comments
 (0)