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

Commit

Permalink
Merge pull request #551 from sectioneight/ignore-directories-fillpackage
Browse files Browse the repository at this point in the history
Ignore dot-prefixed files and subdirs when scanning packages
  • Loading branch information
sdboyer committed May 11, 2017
2 parents 070b761 + 27fbbf2 commit ccd9993
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 2 deletions.
Empty file.
Empty file.
12 changes: 12 additions & 0 deletions internal/gps/_testdata/src/dotgodir/foo.go/foo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package foo

import "sort"

var _ = sort.Strings

// yes, this is dumb, don't use ".go" in your directory names
// See https://github.com/golang/dep/issues/550 for more information
11 changes: 9 additions & 2 deletions internal/gps/pkgtree/pkgtree.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,17 @@ func fillPackage(p *build.Package) error {
var testImports []string
var imports []string
for _, file := range gofiles {
// Skip underscore-led files, in keeping with the rest of the toolchain.
if filepath.Base(file)[0] == '_' {
// Skip underscore-led or dot-led files, in keeping with the rest of the toolchain.
bPrefix := filepath.Base(file)[0]
if bPrefix == '_' || bPrefix == '.' {
continue
}

// Skip any directories that happened to get caught by glob
if stat, err := os.Stat(file); err == nil && stat.IsDir() {
continue
}

pf, err := parser.ParseFile(token.NewFileSet(), file, nil, parser.ImportsOnly|parser.ParseComments)
if err != nil {
if os.IsPermission(err) {
Expand Down
22 changes: 22 additions & 0 deletions internal/gps/pkgtree/pkgtree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1280,6 +1280,28 @@ func TestListPackages(t *testing.T) {
},
},
},
"skip directories starting with '.'": {
fileRoot: j("dotgodir"),
importRoot: "dotgodir",
out: PackageTree{
ImportRoot: "dotgodir",
Packages: map[string]PackageOrErr{
"dotgodir": {
P: Package{
ImportPath: "dotgodir",
Imports: []string{},
},
},
"dotgodir/foo.go": {
P: Package{
ImportPath: "dotgodir/foo.go",
Name: "foo",
Imports: []string{"sort"},
},
},
},
},
},
}

for name, fix := range table {
Expand Down

0 comments on commit ccd9993

Please sign in to comment.