]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: local import fixes
authorRuss Cox <rsc@golang.org>
Sun, 11 Mar 2012 19:53:42 +0000 (15:53 -0400)
committerRuss Cox <rsc@golang.org>
Sun, 11 Mar 2012 19:53:42 +0000 (15:53 -0400)
1) The -D argument should always be a pseudo-import path,
like _/Users/rsc/foo/bar, never a standard import path,
because we want local imports to always resolve to pseudo-paths.

2) Disallow local imports in non-local packages.  Otherwise
everything works but you get two copies of a package
(the real one and the "local" one) in your binary.

R=golang-dev, bradfitz, yiyu.jgl
CC=golang-dev
https://golang.org/cl/5787055

src/cmd/go/build.go
src/cmd/go/pkg.go

index 0b51a22d4f9810d7853d2050c1bc4b52dc253542..24c2a05d226ed4c6ad2ede7ef12f0811842a79ef 100644 (file)
@@ -383,6 +383,7 @@ func goFilesPackage(gofiles []string) *Package {
 
        bp, err := ctxt.ImportDir(dir, 0)
        pkg := new(Package)
+       pkg.local = true
        pkg.load(&stk, bp, err)
        pkg.localPrefix = dirToImportPath(dir)
        pkg.ImportPath = "command-line-arguments"
@@ -1202,7 +1203,7 @@ func (gcToolchain) pack(b *builder, p *Package, objDir, afile string, ofiles []s
 
 func (gcToolchain) ld(b *builder, p *Package, out string, allactions []*action, mainpkg string, ofiles []string) error {
        importArgs := b.includeArgs("-L", allactions)
-       return b.run(p.Dir, p.ImportPath, tool(archChar+"l"), "-o", out, importArgs, buildLdflags, mainpkg)
+       return b.run(".", p.ImportPath, tool(archChar+"l"), "-o", out, importArgs, buildLdflags, mainpkg)
 }
 
 func (gcToolchain) cc(b *builder, p *Package, objdir, ofile, cfile string) error {
@@ -1284,7 +1285,7 @@ func (tools gccgcToolchain) ld(b *builder, p *Package, out string, allactions []
                ldflags = append(ldflags, afile)
        }
        ldflags = append(ldflags, cgoldflags...)
-       return b.run(p.Dir, p.ImportPath, "gccgo", "-o", out, buildGccgoflags, ofiles, "-Wl,-(", ldflags, "-Wl,-)")
+       return b.run(".", p.ImportPath, "gccgo", "-o", out, buildGccgoflags, ofiles, "-Wl,-(", ldflags, "-Wl,-)")
 }
 
 func (gccgcToolchain) cc(b *builder, p *Package, objdir, ofile, cfile string) error {
@@ -1308,6 +1309,9 @@ func (b *builder) gccld(p *Package, out string, flags []string, obj []string) er
 
 // gccCmd returns a gcc command line prefix
 func (b *builder) gccCmd(objdir string) []string {
+       // NOTE: env.go's mkEnv knows that the first three
+       // strings returned are "gcc", "-I", objdir (and cuts them off).
+
        // TODO: HOST_CC?
        a := []string{"gcc", "-I", objdir, "-g", "-O2"}
 
index 9bdd56240b7555c591d1d72dfccb7a1ffbd25b33..1b6a8c5124cdcc21629a4d8b6e2961fb59a859d8 100644 (file)
@@ -279,9 +279,8 @@ func (p *Package) load(stk *importStack, bp *build.Package, err error) *Package
        p.copyBuild(bp)
 
        // The localPrefix is the path we interpret ./ imports relative to.
-       // Now that we've fixed the import path, it's just the import path.
        // Synthesized main packages sometimes override this.
-       p.localPrefix = p.ImportPath
+       p.localPrefix = dirToImportPath(p.Dir)
 
        if err != nil {
                p.Incomplete = true
@@ -343,6 +342,16 @@ func (p *Package) load(stk *importStack, bp *build.Package, err error) *Package
                }
                p1 := loadImport(path, p.Dir, stk, p.build.ImportPos[path])
                if p1.local {
+                       if !p.local && p.Error == nil {
+                               p.Error = &PackageError{
+                                       ImportStack: stk.copy(),
+                                       Err:         fmt.Sprintf("local import %q in non-local package", path),
+                               }
+                               pos := p.build.ImportPos[path]
+                               if len(pos) > 0 {
+                                       p.Error.Pos = pos[0].String()
+                               }
+                       }
                        path = p1.ImportPath
                        importPaths[i] = path
                }