Commit d1be0fd9 authored by Ian Lance Taylor's avatar Ian Lance Taylor

cmd/link: with -importcfg don't strip trailing ".a"

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: 's avatarRuss Cox <rsc@golang.org>
parent 943e2635
......@@ -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")
}
......@@ -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 {
......
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