mirror of
https://github.com/golang/go
synced 2024-11-18 18:34:40 -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:
parent
07e766bf81
commit
ae1141fc8b
@ -66,21 +66,11 @@ func ContainingPackage(ctxt *build.Context, dir, filename string) (*build.Packag
|
|||||||
// paths will not use `\` unless the PathSeparator
|
// paths will not use `\` unless the PathSeparator
|
||||||
// is also `\`, thus we can rely on filepath.ToSlash for some sanity.
|
// is also `\`, thus we can rely on filepath.ToSlash for some sanity.
|
||||||
|
|
||||||
resolvedFilename, err := filepath.EvalSymlinks(filepath.Dir(filename))
|
dirSlash := path.Dir(filepath.ToSlash(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 + "/"
|
|
||||||
|
|
||||||
// We assume that no source root (GOPATH[i] or GOROOT) contains any other.
|
// We assume that no source root (GOPATH[i] or GOROOT) contains any other.
|
||||||
for _, srcdir := range ctxt.SrcDirs() {
|
for _, srcdir := range ctxt.SrcDirs() {
|
||||||
resolvedSrcdir, err := filepath.EvalSymlinks(srcdir)
|
srcdirSlash := filepath.ToSlash(srcdir) + "/"
|
||||||
if err != nil {
|
|
||||||
continue // e.g. non-existent dir on $GOPATH
|
|
||||||
}
|
|
||||||
srcdirSlash := filepath.ToSlash(resolvedSrcdir) + "/"
|
|
||||||
if dirHasPrefix(dirSlash, srcdirSlash) {
|
if dirHasPrefix(dirSlash, srcdirSlash) {
|
||||||
importPath := dirSlash[len(srcdirSlash) : len(dirSlash)-len("/")]
|
importPath := dirSlash[len(srcdirSlash) : len(dirSlash)-len("/")]
|
||||||
return ctxt.Import(importPath, dir, build.FindOnly)
|
return ctxt.Import(importPath, dir, build.FindOnly)
|
||||||
|
@ -10,7 +10,6 @@ package buildutil_test
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"go/build"
|
"go/build"
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
@ -24,46 +23,22 @@ func TestContainingPackage(t *testing.T) {
|
|||||||
goroot := runtime.GOROOT()
|
goroot := runtime.GOROOT()
|
||||||
gopath := filepath.SplitList(os.Getenv("GOPATH"))[0]
|
gopath := filepath.SplitList(os.Getenv("GOPATH"))[0]
|
||||||
|
|
||||||
// Make a symlink to gopath for test
|
tests := [][2]string{
|
||||||
tmp, err := ioutil.TempDir(os.TempDir(), "go")
|
{goroot + "/src/fmt/print.go", "fmt"},
|
||||||
if err != nil {
|
{goroot + "/src/encoding/json/foo.go", "encoding/json"},
|
||||||
t.Errorf("Unable to create a temporary directory in %s", os.TempDir())
|
{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"},
|
||||||
}
|
}
|
||||||
|
for _, test := range tests {
|
||||||
// symlink between $GOPATH/src and /tmp/go/src
|
file, want := test[0], test[1]
|
||||||
// in order to test all possible symlink cases
|
bp, err := buildutil.ContainingPackage(&build.Default, ".", file)
|
||||||
if err := os.Symlink(gopath+"/src", tmp+"/src"); err != nil {
|
got := bp.ImportPath
|
||||||
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)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
got = "(not found)"
|
got = "(not found)"
|
||||||
} else {
|
|
||||||
got = bp.ImportPath
|
|
||||||
}
|
}
|
||||||
if got != test.wantPkg {
|
if got != want {
|
||||||
t.Errorf("ContainingPackage(%q) = %s, want %s", test.filename, got, test.wantPkg)
|
t.Errorf("ContainingPackage(%q) = %s, want %s", file, got, want)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user