diff --git a/imports/fix.go b/imports/fix.go index de1d7467..f2fec885 100644 --- a/imports/fix.go +++ b/imports/fix.go @@ -467,7 +467,8 @@ func scanGoDirs(goRoot bool) { } if typ == os.ModeDir { base := filepath.Base(path) - if base == "" || base[0] == '.' || base[0] == '_' || base == "testdata" { + if base == "" || base[0] == '.' || base[0] == '_' || + base == "testdata" || base == "node_modules" { return filepath.SkipDir } fi, err := os.Lstat(path) diff --git a/imports/fix_test.go b/imports/fix_test.go index 1bd43d83..7c7018e9 100644 --- a/imports/fix_test.go +++ b/imports/fix_test.go @@ -1346,6 +1346,26 @@ func TestIgnoreConfiguration(t *testing.T) { }) } +// Skip "node_modules" directory. +func TestSkipNodeModules(t *testing.T) { + testConfig{ + gopathFiles: map[string]string{ + "example.net/node_modules/pkg/a.go": "package pkg\nconst X = 1", + "otherwise-longer.net/not_modules/pkg/a.go": "package pkg\nconst X = 1", + }, + }.test(t, func(t *goimportTest) { + const in = "package x\n\nconst _ = pkg.X\n" + const want = "package x\n\nimport \"otherwise-longer.net/not_modules/pkg\"\n\nconst _ = pkg.X\n" + buf, err := Process(t.gopath+"/src/x/x.go", []byte(in), nil) + if err != nil { + t.Fatal(err) + } + if string(buf) != want { + t.Errorf("wrong output.\ngot:\n%q\nwant:\n%q\n", buf, want) + } + }) +} + func strSet(ss []string) map[string]bool { m := make(map[string]bool) for _, s := range ss {