Skip to content

Commit d4a8e58

Browse files
committed
imports: skip "node_modules" directories
Updates golang/go#16417 Change-Id: Ia4a5f997036274d09cca2ff10e500e403c1725ba Reviewed-on: https://go-review.googlesource.com/25044 Reviewed-by: Andrew Gerrand <[email protected]>
1 parent edf8e6f commit d4a8e58

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

imports/fix.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,8 @@ func scanGoDirs(goRoot bool) {
467467
}
468468
if typ == os.ModeDir {
469469
base := filepath.Base(path)
470-
if base == "" || base[0] == '.' || base[0] == '_' || base == "testdata" {
470+
if base == "" || base[0] == '.' || base[0] == '_' ||
471+
base == "testdata" || base == "node_modules" {
471472
return filepath.SkipDir
472473
}
473474
fi, err := os.Lstat(path)

imports/fix_test.go

+20
Original file line numberDiff line numberDiff line change
@@ -1346,6 +1346,26 @@ func TestIgnoreConfiguration(t *testing.T) {
13461346
})
13471347
}
13481348

1349+
// Skip "node_modules" directory.
1350+
func TestSkipNodeModules(t *testing.T) {
1351+
testConfig{
1352+
gopathFiles: map[string]string{
1353+
"example.net/node_modules/pkg/a.go": "package pkg\nconst X = 1",
1354+
"otherwise-longer.net/not_modules/pkg/a.go": "package pkg\nconst X = 1",
1355+
},
1356+
}.test(t, func(t *goimportTest) {
1357+
const in = "package x\n\nconst _ = pkg.X\n"
1358+
const want = "package x\n\nimport \"otherwise-longer.net/not_modules/pkg\"\n\nconst _ = pkg.X\n"
1359+
buf, err := Process(t.gopath+"/src/x/x.go", []byte(in), nil)
1360+
if err != nil {
1361+
t.Fatal(err)
1362+
}
1363+
if string(buf) != want {
1364+
t.Errorf("wrong output.\ngot:\n%q\nwant:\n%q\n", buf, want)
1365+
}
1366+
})
1367+
}
1368+
13491369
func strSet(ss []string) map[string]bool {
13501370
m := make(map[string]bool)
13511371
for _, s := range ss {

0 commit comments

Comments
 (0)