Commit 760636d5 authored by Brad Fitzpatrick's avatar Brad Fitzpatrick

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 <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: 's avatarIan Lance Taylor <iant@golang.org>
parent a48998be
......@@ -44,6 +44,28 @@ func RelPaths(paths []string) []string {
return out
}
// FilterDotUnderscoreFiles returns a slice containing all elements
// of path whose base name doesn't begin with "." or "_".
func FilterDotUnderscoreFiles(path []string) []string {
var out []string // lazily initialized
for i, p := range path {
base := filepath.Base(p)
if strings.HasPrefix(base, ".") || strings.HasPrefix(base, "_") {
if out == nil {
out = append(make([]string, 0, len(path)), path[:i]...)
}
continue
}
if out != nil {
out = append(out, p)
}
}
if out == nil {
return path
}
return out
}
// IsTestFile reports whether the source file is a set of tests and should therefore
// be excluded from coverage analysis.
func IsTestFile(file string) bool {
......
......@@ -33,6 +33,7 @@ func runFix(cmd *base.Command, args []string) {
// Use pkg.gofiles instead of pkg.Dir so that
// the command only applies to this package,
// not to packages in subdirectories.
base.Run(str.StringList(cfg.BuildToolexec, base.Tool("fix"), base.RelPaths(pkg.Internal.AllGoFiles)))
files := base.FilterDotUnderscoreFiles(base.RelPaths(pkg.Internal.AllGoFiles))
base.Run(str.StringList(cfg.BuildToolexec, base.Tool("fix"), files))
}
}
......@@ -45,7 +45,8 @@ func runFmt(cmd *base.Command, args []string) {
// Use pkg.gofiles instead of pkg.Dir so that
// the command only applies to this package,
// not to packages in subdirectories.
base.Run(str.StringList(gofmt, "-l", "-w", base.RelPaths(pkg.Internal.AllGoFiles)))
files := base.FilterDotUnderscoreFiles(base.RelPaths(pkg.Internal.AllGoFiles))
base.Run(str.StringList(gofmt, "-l", "-w", files))
}
}
......
......@@ -298,7 +298,8 @@ func download(arg string, parent *load.Package, stk *load.ImportStack, mode int)
// due to wildcard expansion.
for _, p := range pkgs {
if *getFix {
base.Run(cfg.BuildToolexec, str.StringList(base.Tool("fix"), base.RelPaths(p.Internal.AllGoFiles)))
files := base.FilterDotUnderscoreFiles(base.RelPaths(p.Internal.AllGoFiles))
base.Run(cfg.BuildToolexec, str.StringList(base.Tool("fix"), files))
// The imports might have changed, so reload again.
p = load.ReloadPackage(arg, stk)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment