Commit e8fd15fd authored by Russ Cox's avatar Russ Cox

cmd/go: replace PackageInternal.GoFiles, AllGoFiles with methods

These are rarely used and can be computed on demand,
to make clear that they are never out of sync with the
lists in the non-internal Package fields.

Change-Id: I8c621dceaff1aeb39a3ed83f18e848adf14d7106
Reviewed-on: https://go-review.googlesource.com/56284Reviewed-by: 's avatarDavid Crawshaw <crawshaw@golang.org>
Reviewed-by: 's avatarIan Lance Taylor <iant@golang.org>
parent b4f21d6e
......@@ -44,28 +44,6 @@ 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,7 +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.
files := base.FilterDotUnderscoreFiles(base.RelPaths(pkg.Internal.AllGoFiles))
files := base.RelPaths(pkg.InternalAllGoFiles())
base.Run(str.StringList(cfg.BuildToolexec, base.Tool("fix"), files))
}
}
......@@ -59,7 +59,7 @@ 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.
files := base.FilterDotUnderscoreFiles(base.RelPaths(pkg.Internal.AllGoFiles))
files := base.RelPaths(pkg.InternalAllGoFiles())
for _, file := range files {
fileC <- file
}
......
......@@ -153,7 +153,7 @@ func runGenerate(cmd *base.Command, args []string) {
}
// Even if the arguments are .go files, this loop suffices.
for _, pkg := range load.Packages(args) {
for _, file := range pkg.Internal.GoFiles {
for _, file := range pkg.InternalGoFiles() {
if !generate(pkg.Name, file) {
break
}
......
......@@ -301,7 +301,7 @@ func download(arg string, parent *load.Package, stk *load.ImportStack, mode int)
// due to wildcard expansion.
for _, p := range pkgs {
if *getFix {
files := base.FilterDotUnderscoreFiles(base.RelPaths(p.Internal.AllGoFiles))
files := base.RelPaths(p.InternalAllGoFiles())
base.Run(cfg.BuildToolexec, str.StringList(base.Tool("fix"), files))
// The imports might have changed, so reload again.
......
......@@ -95,8 +95,6 @@ type PackageInternal struct {
Build *build.Package
Pkgdir string // overrides build.PkgDir
Imports []*Package // this package's direct imports
GoFiles []string // GoFiles+CgoFiles+TestGoFiles+XTestGoFiles files, absolute paths
AllGoFiles []string // gofiles + IgnoredGoFiles, absolute paths
Target string // installed file for this package (may be executable)
Pkgfile string // where package will be (or is already) built or installed
ForceLibrary bool // this package is a library (even if named "main")
......@@ -980,21 +978,6 @@ func (p *Package) load(stk *ImportStack, bp *build.Package, err error) {
importPaths = append(importPaths, "runtime/internal/sys")
}
// Build list of full paths to all Go files in the package,
// for use by commands like go fmt.
p.Internal.GoFiles = str.StringList(p.GoFiles, p.CgoFiles, p.TestGoFiles, p.XTestGoFiles)
for i := range p.Internal.GoFiles {
p.Internal.GoFiles[i] = filepath.Join(p.Dir, p.Internal.GoFiles[i])
}
sort.Strings(p.Internal.GoFiles)
p.Internal.AllGoFiles = str.StringList(p.IgnoredGoFiles)
for i := range p.Internal.AllGoFiles {
p.Internal.AllGoFiles[i] = filepath.Join(p.Dir, p.Internal.AllGoFiles[i])
}
p.Internal.AllGoFiles = append(p.Internal.AllGoFiles, p.Internal.GoFiles...)
sort.Strings(p.Internal.AllGoFiles)
// Check for case-insensitive collision of input files.
// To avoid problems on case-insensitive files, we reject any package
// where two different input files have equal names under a case-insensitive
......@@ -1141,6 +1124,36 @@ func (p *Package) load(stk *ImportStack, bp *build.Package, err error) {
}
}
// mkAbs rewrites list, which must be paths relative to p.Dir,
// into a sorted list of absolute paths. It edits list in place but for
// convenience also returns list back to its caller.
func (p *Package) mkAbs(list []string) []string {
for i, f := range list {
list[i] = filepath.Join(p.Dir, f)
}
sort.Strings(list)
return list
}
// InternalGoFiles returns the list of Go files being built for the package,
// using absolute paths.
func (p *Package) InternalGoFiles() []string {
return p.mkAbs(str.StringList(p.GoFiles, p.CgoFiles, p.TestGoFiles, p.XTestGoFiles))
}
// InternalGoFiles returns the list of all Go files possibly relevant for the package,
// using absolute paths. "Possibly relevant" means that files are not excluded
// due to build tags, but files with names beginning with . or _ are still excluded.
func (p *Package) InternalAllGoFiles() []string {
var extra []string
for _, f := range p.IgnoredGoFiles {
if f != "" && f[0] != '.' || f[0] != '_' {
extra = append(extra, f)
}
}
return p.mkAbs(str.StringList(extra, p.GoFiles, p.CgoFiles, p.TestGoFiles, p.XTestGoFiles))
}
// InternalDeps returns the full dependency list for p,
// built by traversing p.Internal.Imports, their .Internal.Imports, and so on.
// It guarantees that the returned list has only one package per ImportPath
......
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