From 21e6612d6fc29742024132f7f081e558c6807737 Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Wed, 1 Nov 2017 16:12:40 -0400 Subject: [PATCH] go/build: fix ImportDir to report PkgTarget for directories in GOROOT/GOPATH This makes ImportDir("$GOROOT/src/math", 0) and Import("math", "", 0) equivalent. It was an oversight that they were not before. An upcoming change to the go command relies on the two returning the same results. Change-Id: I187da4830fae85f8dde673c22836ff2da6801047 Reviewed-on: https://go-review.googlesource.com/75290 Run-TryBot: Russ Cox TryBot-Result: Gobot Gobot Reviewed-by: David Crawshaw --- src/go/build/build.go | 2 ++ src/go/build/build_test.go | 13 +++++++++++++ 2 files changed, 15 insertions(+) diff --git a/src/go/build/build.go b/src/go/build/build.go index d8163d0172..68fb423983 100644 --- a/src/go/build/build.go +++ b/src/go/build/build.go @@ -544,6 +544,7 @@ func (ctxt *Context) Import(path string, srcDir string, mode ImportMode) (*Packa p.Goroot = true p.ImportPath = sub p.Root = ctxt.GOROOT + setPkga() // p.ImportPath changed goto Found } } @@ -571,6 +572,7 @@ func (ctxt *Context) Import(path string, srcDir string, mode ImportMode) (*Packa // Record it. p.ImportPath = sub p.Root = root + setPkga() // p.ImportPath changed goto Found } } diff --git a/src/go/build/build_test.go b/src/go/build/build_test.go index 979f76c177..ac5d2c3bb9 100644 --- a/src/go/build/build_test.go +++ b/src/go/build/build_test.go @@ -382,3 +382,16 @@ func TestImportVendorParentFailure(t *testing.T) { t.Fatalf("error on failed import does not mention GOROOT/src/vendor directory:\n%s", e) } } + +func TestImportDirTarget(t *testing.T) { + testenv.MustHaveGoBuild(t) // really must just have source + ctxt := Default + ctxt.GOPATH = "" + p, err := ctxt.ImportDir(filepath.Join(ctxt.GOROOT, "src/path"), 0) + if err != nil { + t.Fatal(err) + } + if p.PkgTargetRoot == "" || p.PkgObj == "" { + t.Errorf("p.PkgTargetRoot == %q, p.PkgObj == %q, want non-empty", p.PkgTargetRoot, p.PkgObj) + } +} -- 2.48.1