Skip to content
This repository was archived by the owner on Sep 9, 2020. It is now read-only.

Commit ccd9993

Browse files
authored
Merge pull request #551 from sectioneight/ignore-directories-fillpackage
Ignore dot-prefixed files and subdirs when scanning packages
2 parents 070b761 + 27fbbf2 commit ccd9993

File tree

5 files changed

+43
-2
lines changed

5 files changed

+43
-2
lines changed

internal/gps/_testdata/src/dotgodir/.go/.gitkeep

Whitespace-only changes.

internal/gps/_testdata/src/dotgodir/foo.go/.gitkeep

Whitespace-only changes.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright 2017 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package foo
6+
7+
import "sort"
8+
9+
var _ = sort.Strings
10+
11+
// yes, this is dumb, don't use ".go" in your directory names
12+
// See https://github.com/golang/dep/issues/550 for more information

internal/gps/pkgtree/pkgtree.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,17 @@ func fillPackage(p *build.Package) error {
199199
var testImports []string
200200
var imports []string
201201
for _, file := range gofiles {
202-
// Skip underscore-led files, in keeping with the rest of the toolchain.
203-
if filepath.Base(file)[0] == '_' {
202+
// Skip underscore-led or dot-led files, in keeping with the rest of the toolchain.
203+
bPrefix := filepath.Base(file)[0]
204+
if bPrefix == '_' || bPrefix == '.' {
204205
continue
205206
}
207+
208+
// Skip any directories that happened to get caught by glob
209+
if stat, err := os.Stat(file); err == nil && stat.IsDir() {
210+
continue
211+
}
212+
206213
pf, err := parser.ParseFile(token.NewFileSet(), file, nil, parser.ImportsOnly|parser.ParseComments)
207214
if err != nil {
208215
if os.IsPermission(err) {

internal/gps/pkgtree/pkgtree_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1280,6 +1280,28 @@ func TestListPackages(t *testing.T) {
12801280
},
12811281
},
12821282
},
1283+
"skip directories starting with '.'": {
1284+
fileRoot: j("dotgodir"),
1285+
importRoot: "dotgodir",
1286+
out: PackageTree{
1287+
ImportRoot: "dotgodir",
1288+
Packages: map[string]PackageOrErr{
1289+
"dotgodir": {
1290+
P: Package{
1291+
ImportPath: "dotgodir",
1292+
Imports: []string{},
1293+
},
1294+
},
1295+
"dotgodir/foo.go": {
1296+
P: Package{
1297+
ImportPath: "dotgodir/foo.go",
1298+
Name: "foo",
1299+
Imports: []string{"sort"},
1300+
},
1301+
},
1302+
},
1303+
},
1304+
},
12831305
}
12841306

12851307
for name, fix := range table {

0 commit comments

Comments
 (0)