]> Cypherpunks repositories - gostls13.git/commitdiff
go/build: look in $GOROOT/src/cmd/foo/bar for import cmd/foo/bar
authorRuss Cox <rsc@golang.org>
Thu, 7 Aug 2014 16:33:19 +0000 (12:33 -0400)
committerRuss Cox <rsc@golang.org>
Thu, 7 Aug 2014 16:33:19 +0000 (12:33 -0400)
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
src/pkg/go/build/build_test.go

index 09730d6351b979d65b09dff02dda187cf8e415a1..6db0275032d3d86d15cdea32eec032c7605959cc 100644 (file)
@@ -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 {
index f0d243cd53fa2fac7957a784a8f3cfe0940d3faf..0040101134a61efbb8fbb9855d3f440e0710a0ca 100644 (file)
@@ -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")
+       }
+}