Closed
Description
What version of Go are you using (go version
)?
tip (3235f7c)
Does this issue reproduce with the latest release?
Yes, happens with Go 1.11, 1.12, and tip, but not with Go 1.10.
What operating system and processor architecture are you using (go env
)?
linux/amd64
What did you do?
$ mkdir -p /tmp/src/hello
$ cat >hello.go
package main
func main() { println("hello") }
$ GO111MODULE=off GOPATH=/tmp go build -ldflags=-linkmode=external
# hello
loadinternal: cannot find runtime/cgo
$ echo $?
0
$ ./hello
hello
When using external linking for a pure Go program, the linker prints loadinternal: cannot find runtime/cgo
message. The linking actually succeeded, and the generated binary works fine.
What did you expect to see?
No error message, like with Go 1.10.
$ GOPATH=/tmp go1.10 build -ldflags=-linkmode=external
$ ./hello
hello
What did you see instead?
Spurious loadinternal: cannot find runtime/cgo
error message.
Metadata
Metadata
Assignees
Type
Projects
Relationships
Development
No branches or pull requests
Activity
cherrymui commentedon Apr 18, 2019
This also happens in module mode:
But it does not happen if the file name (hello.go) is given explicitly.
[-]cmd/go or cmd/link: spurious error message when external linking a pure Go program[/-][+]cmd/go: spurious error message when external linking a pure Go program[/+]julieqiu commentedon Apr 22, 2019
/cc @jayconrod @bcmills
bcmills commentedon May 8, 2019
I can reproduce the failure. I'm bisecting it now to try to get some idea of how it broke.
bcmills commentedon May 8, 2019
git bisect
points to CL 129059 (CC @rsc @alandonovan).bcmills commentedon May 8, 2019
This is a result of the
-ldflags=-linkmode=external
flag now (correctly) being passed when compiling thehello
package, which reveals a typestate bug inload.PackagesAndErrors
.The decision in the
LinkerDeps
function about whether to add theruntime/cgo
dependency is based on whether the-ldflags=-linkmode=external
flag is set for the package, which itself depends on whetherCmdlinePkg
flag is set:go/src/cmd/go/internal/load/flag.go
Line 43 in 19966e9
That field is set after the call to
loadImport
:go/src/cmd/go/internal/load/pkg.go
Lines 1962 to 1964 in 19966e9
However, by that point
loadImport
has already calledload
on the package:go/src/cmd/go/internal/load/pkg.go
Line 511 in 19966e9
...and
load
is what's supposed to figure out the implicit linker dependencies:go/src/cmd/go/internal/load/pkg.go
Line 1534 in 19966e9
rsc commentedon May 9, 2019
@cherrymui, unless this is important for you for some reason, we'd like to postpone the fix to Go 1.14. It's subtle code.
/cc @bcmills
gopherbot commentedon May 9, 2019
Change https://golang.org/cl/176217 mentions this issue:
cmd/go/internal/work: set CmdlinePkg in loadImport instead of after
cherrymui commentedon May 9, 2019
Sure, it is fine to postpone.
53 remaining items