From d078d483ce87b4311f79e988a0b609d3c53d3cb4 Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Thu, 7 Aug 2014 12:33:19 -0400 Subject: [PATCH] go/build: look in $GOROOT/src/cmd/foo/bar for import cmd/foo/bar This lets us have non-main packages like cmd/internal or cmd/nm/internal/whatever. The src/pkg migration (see golang.org/s/go14mainrepo) will allow this as a natural side effect. The explicit change here just allows use of the effect a little sooner. LGTM=r R=r CC=golang-codereviews https://golang.org/cl/117630043 --- src/pkg/go/build/build.go | 7 ++++++- src/pkg/go/build/build_test.go | 10 ++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/pkg/go/build/build.go b/src/pkg/go/build/build.go index 09730d6351..6db0275032 100644 --- a/src/pkg/go/build/build.go +++ b/src/pkg/go/build/build.go @@ -521,7 +521,12 @@ func (ctxt *Context) Import(path string, srcDir string, mode ImportMode) (*Packa // Determine directory from import path. if ctxt.GOROOT != "" { - dir := ctxt.joinPath(ctxt.GOROOT, "src", "pkg", path) + var dir string + if strings.HasPrefix(path, "cmd/") { + dir = ctxt.joinPath(ctxt.GOROOT, "src", path) + } else { + dir = ctxt.joinPath(ctxt.GOROOT, "src", "pkg", path) + } isDir := ctxt.isDir(dir) binaryOnly = !isDir && mode&AllowBinary != 0 && pkga != "" && ctxt.isFile(ctxt.joinPath(ctxt.GOROOT, pkga)) if isDir || binaryOnly { diff --git a/src/pkg/go/build/build_test.go b/src/pkg/go/build/build_test.go index f0d243cd53..0040101134 100644 --- a/src/pkg/go/build/build_test.go +++ b/src/pkg/go/build/build_test.go @@ -193,3 +193,13 @@ func TestMatchFile(t *testing.T) { } } } + +func TestImportCmd(t *testing.T) { + p, err := Import("cmd/internal/objfile", "", 0) + if err != nil { + t.Fatal(err) + } + if !strings.HasSuffix(filepath.ToSlash(p.Dir), "src/cmd/internal/objfile") { + t.Fatalf("Import cmd/internal/objfile returned Dir=%q, want %q", filepath.ToSlash(p.Dir), ".../src/cmd/internal/objfile") + } +} -- 2.48.1