From 4c6345e8dcc0f4b741dea01f83c49ad201a26673 Mon Sep 17 00:00:00 2001 From: Chris Stockton Date: Tue, 15 Nov 2016 09:48:37 -0700 Subject: [PATCH] imports: respect ignoredDirs for symlinks Fixes golang/go#17916 Added tests. Change-Id: Ie44e4bcbec267b6c16249336c5d48bae86acc2b5 Reviewed-on: https://go-review.googlesource.com/33246 Run-TryBot: Brad Fitzpatrick Reviewed-by: Brad Fitzpatrick --- imports/fix.go | 3 +++ imports/fix_test.go | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/imports/fix.go b/imports/fix.go index a241c2976..c74bdd2c0 100644 --- a/imports/fix.go +++ b/imports/fix.go @@ -462,6 +462,9 @@ func shouldTraverse(dir string, fi os.FileInfo) bool { if !ts.IsDir() { return false } + if skipDir(ts) { + return false + } realParent, err := filepath.EvalSymlinks(dir) if err != nil { diff --git a/imports/fix_test.go b/imports/fix_test.go index f75a1d630..f984fd410 100644 --- a/imports/fix_test.go +++ b/imports/fix_test.go @@ -889,6 +889,40 @@ var ( t.Fatalf("results differ\nGOT:\n%s\nWANT:\n%s\n", got, output) } }) + + // Add a .goimportsignore and ensure it is respected. + if err := ioutil.WriteFile(newGoPath+"/src/.goimportsignore", []byte("x/mypkg\n"), 0666); err != nil { + t.Fatal(err) + } + + withEmptyGoPath(func() { + build.Default.GOPATH = newGoPath + + input := `package p + +var ( + _ = fmt.Print + _ = mypkg.Foo +) +` + output := `package p + +import "fmt" + +var ( + _ = fmt.Print + _ = mypkg.Foo +) +` + buf, err := Process(newGoPath+"/src/myotherpkg/toformat.go", []byte(input), &Options{}) + if err != nil { + t.Fatal(err) + } + if got := string(buf); got != output { + t.Fatalf("ignored results differ\nGOT:\n%s\nWANT:\n%s\n", got, output) + } + }) + } // Test for correctly identifying the name of a vendored package when it