Commit d078d483 authored by Russ Cox's avatar Russ Cox

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
parent 08033f98
...@@ -521,7 +521,12 @@ func (ctxt *Context) Import(path string, srcDir string, mode ImportMode) (*Packa ...@@ -521,7 +521,12 @@ func (ctxt *Context) Import(path string, srcDir string, mode ImportMode) (*Packa
// Determine directory from import path. // Determine directory from import path.
if ctxt.GOROOT != "" { 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) isDir := ctxt.isDir(dir)
binaryOnly = !isDir && mode&AllowBinary != 0 && pkga != "" && ctxt.isFile(ctxt.joinPath(ctxt.GOROOT, pkga)) binaryOnly = !isDir && mode&AllowBinary != 0 && pkga != "" && ctxt.isFile(ctxt.joinPath(ctxt.GOROOT, pkga))
if isDir || binaryOnly { if isDir || binaryOnly {
......
...@@ -193,3 +193,13 @@ func TestMatchFile(t *testing.T) { ...@@ -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")
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment