Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/go: module info (from runtime/debug.modinfo) dropped from final binary if unused #28753

Closed
hyangah opened this Issue Nov 12, 2018 · 6 comments

Comments

Projects
None yet
4 participants
@hyangah
Copy link
Contributor

hyangah commented Nov 12, 2018

Dependency on submodule (in the same repo) sometimes doesn't appear in the debug modinfo.

Tested with Go built from the tip today

$ go version
go version devel +f58b02a29c Sun Nov 11 18:49:40 2018 +0000 linux/amd64

How to reproduce:

$ go get rsc.io/goversion

$ git clone https://github.com/hyangah/modtest7.git
$ cd modtest7
$ tree
.
├── go.mod
├── go.sum
├── hello
│   ├── go.mod
│   └── hello.go
└── main.go
$ cat main.go
package main

import (
	"github.com/hyangah/modtest7/hello"
)

func main() {
	hello.Hi()
}

$ go build
$ goversion -m modtest7
modtest7 devel +f58b02a29c Sun Nov 11 18:49:40 2018 +0000
	path  github.com/hyangah/modtest7
	mod   github.com/hyangah/modtest7  (devel)

I expected the module github.com/hyangah/modtest7/hello is listed as a dependency, but it didn't appear.

go build -x -v output
$ go build -x -v
WORK=/tmp/go-build666928546
mkdir -p /usr/local/google/home/hakim/go/pkg/mod/cache/vcs/690aa3d58c3a1743e72ae301f73382e197623d579a5ad3ddc35eca733d606c24 # git2 https://github.com/hyangah/modtest7
cd /usr/local/google/home/hakim/go/pkg/mod/cache/vcs/690aa3d58c3a1743e72ae301f73382e197623d579a5ad3ddc35eca733d606c24; git init --bare
0.005s # cd /usr/local/google/home/hakim/go/pkg/mod/cache/vcs/690aa3d58c3a1743e72ae301f73382e197623d579a5ad3ddc35eca733d606c24; git init --bare
cd /usr/local/google/home/hakim/go/pkg/mod/cache/vcs/690aa3d58c3a1743e72ae301f73382e197623d579a5ad3ddc35eca733d606c24; git remote add origin https://github.com/hyangah/modtest7
0.003s # cd /usr/local/google/home/hakim/go/pkg/mod/cache/vcs/690aa3d58c3a1743e72ae301f73382e197623d579a5ad3ddc35eca733d606c24; git remote add origin https://github.com/hyangah/modtest7
go: finding github.com/hyangah/modtest7/hello v0.0.0-20181112233247-afc59526b1e9
cd /usr/local/google/home/hakim/go/pkg/mod/cache/vcs/690aa3d58c3a1743e72ae301f73382e197623d579a5ad3ddc35eca733d606c24; git -c log.showsignature=false log -n1 '--format=format:%H %ct %D' afc59526b1e9
0.003s # cd /usr/local/google/home/hakim/go/pkg/mod/cache/vcs/690aa3d58c3a1743e72ae301f73382e197623d579a5ad3ddc35eca733d606c24; git -c log.showsignature=false log -n1 '--format=format:%H %ct %D' afc59526b1e9
cd /usr/local/google/home/hakim/go/pkg/mod/cache/vcs/690aa3d58c3a1743e72ae301f73382e197623d579a5ad3ddc35eca733d606c24; git tag -l
0.002s # cd /usr/local/google/home/hakim/go/pkg/mod/cache/vcs/690aa3d58c3a1743e72ae301f73382e197623d579a5ad3ddc35eca733d606c24; git tag -l
cd /usr/local/google/home/hakim/go/pkg/mod/cache/vcs/690aa3d58c3a1743e72ae301f73382e197623d579a5ad3ddc35eca733d606c24; git ls-remote -q origin
0.244s # cd /usr/local/google/home/hakim/go/pkg/mod/cache/vcs/690aa3d58c3a1743e72ae301f73382e197623d579a5ad3ddc35eca733d606c24; git ls-remote -q origin
cd /usr/local/google/home/hakim/go/pkg/mod/cache/vcs/690aa3d58c3a1743e72ae301f73382e197623d579a5ad3ddc35eca733d606c24; git fetch -f origin 'refs/heads/*:refs/heads/*' 'refs/tags/*:refs/tags/*'
0.293s # cd /usr/local/google/home/hakim/go/pkg/mod/cache/vcs/690aa3d58c3a1743e72ae301f73382e197623d579a5ad3ddc35eca733d606c24; git fetch -f origin 'refs/heads/*:refs/heads/*' 'refs/tags/*:refs/tags/*'
cd /usr/local/google/home/hakim/go/pkg/mod/cache/vcs/690aa3d58c3a1743e72ae301f73382e197623d579a5ad3ddc35eca733d606c24; git -c log.showsignature=false log -n1 '--format=format:%H %ct %D' afc59526b1e9
0.003s # cd /usr/local/google/home/hakim/go/pkg/mod/cache/vcs/690aa3d58c3a1743e72ae301f73382e197623d579a5ad3ddc35eca733d606c24; git -c log.showsignature=false log -n1 '--format=format:%H %ct %D' afc59526b1e9
cd /usr/local/google/home/hakim/go/pkg/mod/cache/vcs/690aa3d58c3a1743e72ae301f73382e197623d579a5ad3ddc35eca733d606c24; git cat-file blob afc59526b1e9357dfd74c2b55fedf7c09461e2e9:hello/go.mod
0.003s # cd /usr/local/google/home/hakim/go/pkg/mod/cache/vcs/690aa3d58c3a1743e72ae301f73382e197623d579a5ad3ddc35eca733d606c24; git cat-file blob afc59526b1e9357dfd74c2b55fedf7c09461e2e9:hello/go.mod
cd /usr/local/google/home/hakim/go/pkg/mod/cache/vcs/690aa3d58c3a1743e72ae301f73382e197623d579a5ad3ddc35eca733d606c24; git cat-file blob afc59526b1e9357dfd74c2b55fedf7c09461e2e9:hello/go.mod
0.003s # cd /usr/local/google/home/hakim/go/pkg/mod/cache/vcs/690aa3d58c3a1743e72ae301f73382e197623d579a5ad3ddc35eca733d606c24; git cat-file blob afc59526b1e9357dfd74c2b55fedf7c09461e2e9:hello/go.mod
go: downloading github.com/hyangah/modtest7/hello v0.0.0-20181112233247-afc59526b1e9
cd /usr/local/google/home/hakim/go/pkg/mod/cache/vcs/690aa3d58c3a1743e72ae301f73382e197623d579a5ad3ddc35eca733d606c24; git cat-file blob afc59526b1e9357dfd74c2b55fedf7c09461e2e9:hello/go.mod
0.003s # cd /usr/local/google/home/hakim/go/pkg/mod/cache/vcs/690aa3d58c3a1743e72ae301f73382e197623d579a5ad3ddc35eca733d606c24; git cat-file blob afc59526b1e9357dfd74c2b55fedf7c09461e2e9:hello/go.mod
cd /usr/local/google/home/hakim/go/pkg/mod/cache/vcs/690aa3d58c3a1743e72ae301f73382e197623d579a5ad3ddc35eca733d606c24; git -c core.autocrlf=input -c core.eol=lf archive --format=zip --prefix=prefix/ afc59526b1e9357dfd74c2b55fedf7c09461e2e9 -- hello
0.004s # cd /usr/local/google/home/hakim/go/pkg/mod/cache/vcs/690aa3d58c3a1743e72ae301f73382e197623d579a5ad3ddc35eca733d606c24; git -c core.autocrlf=input -c core.eol=lf archive --format=zip --prefix=prefix/ afc59526b1e9357dfd74c2b55fedf7c09461e2e9 -- hello
cd /usr/local/google/home/hakim/go/pkg/mod/cache/vcs/690aa3d58c3a1743e72ae301f73382e197623d579a5ad3ddc35eca733d606c24; git cat-file blob afc59526b1e9357dfd74c2b55fedf7c09461e2e9:LICENSE
0.003s # cd /usr/local/google/home/hakim/go/pkg/mod/cache/vcs/690aa3d58c3a1743e72ae301f73382e197623d579a5ad3ddc35eca733d606c24; git cat-file blob afc59526b1e9357dfd74c2b55fedf7c09461e2e9:LICENSE
mkdir -p $WORK/b001/
cat >$WORK/b001/importcfg.link << 'EOF' # internal
packagefile github.com/hyangah/modtest7=/usr/local/google/home/hakim/.cache/go-build/f3/f3364a757515e565a17db8ce81386ba3ce3cfc3d411b3d94838c597c3de137e7-d
packagefile github.com/hyangah/modtest7/hello=/usr/local/google/home/hakim/.cache/go-build/27/2763495febf1ae14d202f8e098930fc5d5329828aa73caf78f1d3517f3b8e2e3-d
packagefile runtime=/usr/local/google/home/hakim/.cache/go-build/fa/fadd0857eaf0d83dbe2ea7918b5b8c1b7849ff105ff155d26c29341dfb479130-d
packagefile internal/bytealg=/usr/local/google/home/hakim/.cache/go-build/b9/b9aa84a005f37674bb1d54facbba420cac72f89554ec18744e4fc10e15b1a573-d
packagefile internal/cpu=/usr/local/google/home/hakim/golang/go/pkg/linux_amd64/internal/cpu.a
packagefile runtime/internal/atomic=/usr/local/google/home/hakim/golang/go/pkg/linux_amd64/runtime/internal/atomic.a
packagefile runtime/internal/math=/usr/local/google/home/hakim/golang/go/pkg/linux_amd64/runtime/internal/math.a
packagefile runtime/internal/sys=/usr/local/google/home/hakim/golang/go/pkg/linux_amd64/runtime/internal/sys.a
EOF
mkdir -p $WORK/b001/exe/
cd .
/usr/local/google/home/hakim/golang/go/pkg/tool/linux_amd64/link -o $WORK/b001/exe/a.out -importcfg $WORK/b001/importcfg.link -buildmode=exe -buildid=yV_6u0tuDNnxPReT53qd/iEGjVHL_ChZAAZfv5eKJ/JVIBetcUg4mrkMBZzVD2/yV_6u0tuDNnxPReT53qd -extld=gcc /usr/local/google/home/hakim/.cache/go-build/f3/f3364a757515e565a17db8ce81386ba3ce3cfc3d411b3d94838c597c3de137e7-d
/usr/local/google/home/hakim/golang/go/pkg/tool/linux_amd64/buildid -w $WORK/b001/exe/a.out # internal
mv $WORK/b001/exe/a.out modtest7
rm -r $WORK/b001/

I suspect this is related to #28083.

@hyangah hyangah added the modules label Nov 12, 2018

@bcmills

This comment has been minimized.

Copy link
Member

bcmills commented Jan 17, 2019

It looks like the linker is stripping out the debug information for some reason: if I modify the program to access it at run-time, then goversion also detects it successfully.

modtest7$ git checkout HEAD main.go

modtest7$ rm ./modtest7 && gotip build

modtest7$ $GOPATH/bin/goversion -m ./modtest7
./modtest7 devel +6e9f664 Thu Jan 17 17:07:23 2019 +0000

modtest7$ cat >main.go <<EOF
> package main

import (
        "fmt"
        "runtime/debug"

        "github.com/hyangah/modtest7/hello"
)

func main() {
        hello.Hi()
        bi, ok := debug.ReadBuildInfo()
        if !ok {
                panic("no build info")
        }
        fmt.Println(bi.Main.Path, bi.Main.Version)
        for _, d := range bi.Deps {
                fmt.Println(d.Path, d.Version)
        }
}
> EOF

modtest7$ rm ./modtest7 && gotip build

modtest7$ $GOPATH/bin/goversion -m ./modtest7
./modtest7 devel +6e9f664 Thu Jan 17 17:07:23 2019 +0000
        path  github.com/hyangah/modtest7
        mod   github.com/hyangah/modtest7        (devel)
        dep   github.com/hyangah/modtest7/hello  v0.0.0-20181112233247-afc59526b1e9

@bcmills bcmills added this to the Go1.12 milestone Jan 17, 2019

@bcmills

This comment has been minimized.

Copy link
Member

bcmills commented Jan 17, 2019

Yeah, it's definitely being pruned out for some reason. An explicit runtime.KeepAlive is all it takes to make goversion work:

modtest7$ cat >main.go <<EOF
package main

import (
        "runtime"

        "github.com/hyangah/modtest7/hello"
)

func main() {
        hello.Hi()
        runtime.KeepAlive(__debug_modinfo__)
}
EOF

modtest7$ rm ./modtest7 && gotip build

modtest7$ $GOPATH/bin/goversion -m ./modtest7
./modtest7 devel +6e9f664 Thu Jan 17 17:07:23 2019 +0000
        path  github.com/hyangah/modtest7
        mod   github.com/hyangah/modtest7        (devel)
        dep   github.com/hyangah/modtest7/hello  v0.0.0-20181112233247-afc59526b1e9
@bcmills

This comment has been minimized.

Copy link
Member

bcmills commented Jan 17, 2019

@ianlancetaylor, @randall77, @josharian, @jayconrod, @heschik:
Any idea what might have changed to cause the compiler and/or linker to prune out the __debug_modinfo__ string from the final binary?

It is unused, but defined in package main using //go:linkname __debug_modinfo__ runtime/debug.modinfo.

@bcmills bcmills changed the title cmd/go: submodule info is omitted in runtime/debug.modinfo cmd/go: module info (from runtime/debug.modinfo) is dropped if unreachable Jan 17, 2019

@bcmills bcmills changed the title cmd/go: module info (from runtime/debug.modinfo) is dropped if unreachable cmd/go: module info (from runtime/debug.modinfo) dropped from final binary if unused Jan 17, 2019

@heschik

This comment has been minimized.

Copy link
Contributor

heschik commented Jan 17, 2019

I guess https://golang.org/cl/157097 removed the only use of the variable, allowing it to be eliminated?

@bcmills

This comment has been minimized.

Copy link
Member

bcmills commented Jan 17, 2019

Maybe, but @hyangah filed this issue back in November, and that commit only landed this week. I guess it was fixed incidentally and then regressed again!

@gopherbot

This comment has been minimized.

Copy link

gopherbot commented Jan 17, 2019

Change https://golang.org/cl/158357 mentions this issue: cmd/go/internal/modload: ensure that __debug_modinfo__ is not discarded during linking

@gopherbot gopherbot closed this in 9d23975 Jan 23, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.