]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/link: with -importcfg don't strip trailing ".a"
authorIan Lance Taylor <iant@golang.org>
Tue, 5 Dec 2017 07:10:56 +0000 (23:10 -0800)
committerIan Lance Taylor <iant@golang.org>
Tue, 12 Dec 2017 04:55:56 +0000 (04:55 +0000)
When using -importcfg, the import paths recorded by the compiler in
the object file are simply the import paths. When not using -importcfg,
the import paths have a trailing ".a". Assume that if we are using
-importcfg with the compiler, we are using it with the linker,
and so if the linker sees an -importcfg option it should not
strip ".a" from the import path read from the object files.

This was mostly working because the linker only strips a trailing
".x" for a literal dot and any single character 'x'. Since few import
paths end with ".x", most programs worked fine.

Fixes #22986

Change-Id: I6c10a160b97dd63fff3931f27a1514c856e8cd52
Reviewed-on: https://go-review.googlesource.com/81878
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
src/cmd/go/go_test.go
src/cmd/link/internal/ld/ld.go

index 732628ccd8a90e45ce180c966f3221355afb5bce..643b3b9506ba25b7ad4c5d66d7afb080ece14e6a 100644 (file)
@@ -5433,3 +5433,50 @@ func TestFailFast(t *testing.T) {
                })
        }
 }
+
+// Issue 22986.
+func TestImportPath(t *testing.T) {
+       tg := testgo(t)
+       defer tg.cleanup()
+       tg.parallel()
+
+       tg.tempFile("src/a/a.go", `
+package main
+
+import (
+       "log"
+       p "a/p-1.0"
+)
+
+func main() {
+       if !p.V {
+               log.Fatal("false")
+       }
+}`)
+
+       tg.tempFile("src/a/a_test.go", `
+package main_test
+
+import (
+       p "a/p-1.0"
+       "testing"
+)
+
+func TestV(t *testing.T) {
+       if !p.V {
+               t.Fatal("false")
+       }
+}`)
+
+       tg.tempFile("src/a/p-1.0/p.go", `
+package p
+
+var V = true
+
+func init() {}
+`)
+
+       tg.setenv("GOPATH", tg.path("."))
+       tg.run("build", "-o", tg.path("a.exe"), "a")
+       tg.run("test", "a")
+}
index b260ef28c8e423133e6efb32dabb59dba340e84f..896e1c87d242f271c8af99a0acf55016a5e6d15c 100644 (file)
@@ -87,8 +87,14 @@ func (ctxt *Link) readImportCfg(file string) {
        }
 }
 
-func pkgname(lib string) string {
+func pkgname(ctxt *Link, lib string) string {
        name := path.Clean(lib)
+
+       // When using importcfg, we have the final package name.
+       if ctxt.PackageFile != nil {
+               return name
+       }
+
        // runtime.a -> runtime, runtime.6 -> runtime
        pkg := name
        if len(pkg) >= 2 && pkg[len(pkg)-2] == '.' {
@@ -116,7 +122,7 @@ func findlib(ctxt *Link, lib string) (string, bool) {
                if filepath.IsAbs(name) {
                        pname = name
                } else {
-                       pkg := pkgname(lib)
+                       pkg := pkgname(ctxt, lib)
                        // Add .a if needed; the new -importcfg modes
                        // do not put .a into the package name anymore.
                        // This only matters when people try to mix
@@ -149,7 +155,7 @@ func findlib(ctxt *Link, lib string) (string, bool) {
 }
 
 func addlib(ctxt *Link, src string, obj string, lib string) *sym.Library {
-       pkg := pkgname(lib)
+       pkg := pkgname(ctxt, lib)
 
        // already loaded?
        if l := ctxt.LibraryByPkg[pkg]; l != nil {