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

Ignore dot-prefixed files and subdirs when scanning packages #551

Merged
merged 5 commits into from
May 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delete this code, its not needed now.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it still is - in the event of e.g. a directory named foo.go (as the latest commit introduces). no?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It still is. My comment about "NOTE: this doesn't handle directories" no longer applies, as demonstrated by the attached test case.

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"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's also add e.g. a foo.go dir to accompany the .go dir, so that we're testing both the dot-led and the dir-ending-in-.go case.

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