From ae1141fc8b3e38d9e074b383af516745815897c3 Mon Sep 17 00:00:00 2001 From: Rebecca Stambler Date: Mon, 5 Dec 2016 22:44:22 +0000 Subject: [PATCH] Revert "go/buildutil: handle symlinks in filenames" Reason: ContainingPackage should do all I/O through build.Context. This reverts commit 3a9a2cbbc44c28889bacca49eb91cdd645eb8fe5. Change-Id: I3897d8da5026ddc470989e3f239540286e89df4d Reviewed-on: https://go-review.googlesource.com/33922 Reviewed-by: Alan Donovan --- go/buildutil/util.go | 14 ++--------- go/buildutil/util_test.go | 49 ++++++++++----------------------------- 2 files changed, 14 insertions(+), 49 deletions(-) diff --git a/go/buildutil/util.go b/go/buildutil/util.go index c2d28431..6dc0cfb8 100644 --- a/go/buildutil/util.go +++ b/go/buildutil/util.go @@ -66,21 +66,11 @@ func ContainingPackage(ctxt *build.Context, dir, filename string) (*build.Packag // paths will not use `\` unless the PathSeparator // is also `\`, thus we can rely on filepath.ToSlash for some sanity. - resolvedFilename, err := filepath.EvalSymlinks(filepath.Dir(filename)) - if err != nil { - return nil, fmt.Errorf("can't evaluate symlinks of %s: %v", path.Dir(filename), err) - } - - resolvedDir := filepath.ToSlash(resolvedFilename) - dirSlash := resolvedDir + "/" + dirSlash := path.Dir(filepath.ToSlash(filename)) + "/" // We assume that no source root (GOPATH[i] or GOROOT) contains any other. for _, srcdir := range ctxt.SrcDirs() { - resolvedSrcdir, err := filepath.EvalSymlinks(srcdir) - if err != nil { - continue // e.g. non-existent dir on $GOPATH - } - srcdirSlash := filepath.ToSlash(resolvedSrcdir) + "/" + srcdirSlash := filepath.ToSlash(srcdir) + "/" if dirHasPrefix(dirSlash, srcdirSlash) { importPath := dirSlash[len(srcdirSlash) : len(dirSlash)-len("/")] return ctxt.Import(importPath, dir, build.FindOnly) diff --git a/go/buildutil/util_test.go b/go/buildutil/util_test.go index 148214dc..dd55533c 100644 --- a/go/buildutil/util_test.go +++ b/go/buildutil/util_test.go @@ -10,7 +10,6 @@ package buildutil_test import ( "go/build" - "io/ioutil" "os" "path/filepath" "runtime" @@ -24,46 +23,22 @@ func TestContainingPackage(t *testing.T) { goroot := runtime.GOROOT() gopath := filepath.SplitList(os.Getenv("GOPATH"))[0] - // Make a symlink to gopath for test - tmp, err := ioutil.TempDir(os.TempDir(), "go") - if err != nil { - t.Errorf("Unable to create a temporary directory in %s", os.TempDir()) + tests := [][2]string{ + {goroot + "/src/fmt/print.go", "fmt"}, + {goroot + "/src/encoding/json/foo.go", "encoding/json"}, + {goroot + "/src/encoding/missing/foo.go", "(not found)"}, + {gopath + "/src/golang.org/x/tools/go/buildutil/util_test.go", + "golang.org/x/tools/go/buildutil"}, } - - // symlink between $GOPATH/src and /tmp/go/src - // in order to test all possible symlink cases - if err := os.Symlink(gopath+"/src", tmp+"/src"); err != nil { - t.Fatal(err) - } - - defer os.RemoveAll(tmp) - - for _, test := range []struct { - gopath, filename, wantPkg string - }{ - {gopath, goroot + "/src/fmt/print.go", "fmt"}, - {gopath, goroot + "/src/encoding/json/foo.go", "encoding/json"}, - {gopath, goroot + "/src/encoding/missing/foo.go", "(not found)"}, - {gopath, gopath + "/src/golang.org/x/tools/go/buildutil/util_test.go", - "golang.org/x/tools/go/buildutil"}, - {gopath, tmp + "/src/golang.org/x/tools/go/buildutil/util_test.go", - "golang.org/x/tools/go/buildutil"}, - {tmp, gopath + "/src/golang.org/x/tools/go/buildutil/util_test.go", - "golang.org/x/tools/go/buildutil"}, - {tmp, tmp + "/src/golang.org/x/tools/go/buildutil/util_test.go", - "golang.org/x/tools/go/buildutil"}, - } { - var got string - var buildContext = build.Default - buildContext.GOPATH = test.gopath - bp, err := buildutil.ContainingPackage(&buildContext, ".", test.filename) + for _, test := range tests { + file, want := test[0], test[1] + bp, err := buildutil.ContainingPackage(&build.Default, ".", file) + got := bp.ImportPath if err != nil { got = "(not found)" - } else { - got = bp.ImportPath } - if got != test.wantPkg { - t.Errorf("ContainingPackage(%q) = %s, want %s", test.filename, got, test.wantPkg) + if got != want { + t.Errorf("ContainingPackage(%q) = %s, want %s", file, got, want) } }