1
0
mirror of https://github.com/golang/go synced 2024-11-18 16:04:44 -07:00

Revert "go/buildutil: handle symlinks in filenames"

Reason: ContainingPackage should do all I/O through build.Context.

This reverts commit 3a9a2cbbc4.

Change-Id: I3897d8da5026ddc470989e3f239540286e89df4d
Reviewed-on: https://go-review.googlesource.com/33922
Reviewed-by: Alan Donovan <adonovan@google.com>
This commit is contained in:
Rebecca Stambler 2016-12-05 22:44:22 +00:00 committed by Alan Donovan
parent 07e766bf81
commit ae1141fc8b
2 changed files with 14 additions and 49 deletions

View File

@ -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)

View File

@ -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)
}
}