Skip to content

Compiling with modules and -trimpath records temp path in binary #28008

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

Closed
fd0 opened this issue Oct 4, 2018 · 2 comments
Closed

Compiling with modules and -trimpath records temp path in binary #28008

fd0 opened this issue Oct 4, 2018 · 2 comments
Labels
FrozenDueToAge modules NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@fd0
Copy link

fd0 commented Oct 4, 2018

What version of Go are you using (go version)?

go version go1.11.1 linux/amd64

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

GOARCH="amd64"
GOBIN=""
GOCACHE="/home/fd0/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/fd0/shared/work/go"
GOPROXY=""
GORACE=""
GOROOT="/home/fd0/sdk/go1.11.1"
GOTMPDIR=""
GOTOOLDIR="/home/fd0/sdk/go1.11.1/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build079176254=/tmp/go-build -gno-record-gcc-switches"

What did you do?

For restic we're trying hard to provide reproducible builds. Among others, we strip the path to the directory with the source code using -trimpath, this worked great for Go 1.10. Unfortunately, building restic with Go 1.11 with Modules leaves a rather strange temporary path to _gomod_.go (which seems to record the build versions) in the binary. I did not find a way to strip or remove this path, resulting in different binaries for most builds. The temporary path is also cached in the build cache, I'm not sure if that's a good or bad thing, as consecutive builds without changing the code embeds the same temporary path.

This issue is related to #16860

Minimal example:

unset GOPATH
export GO111MODULE=on

cd $(mktemp -d /tmp/go-temp-path-XXXXXX)

cat <<EOF >main.go
package main

import "fmt"

func main() {
       fmt.Println("example")
}
EOF

go mod init example

The resulting binary example contains the path to the source code, and there's a reference to _gomod_.go but without a path:

go build

strings example | egrep '(/tmp|_gomod_)'
_gomod_.go
/tmp/go-temp-path-yN4rfF/main.go

What did you expect to see?

Using -trimpath should remove the path to the code and not embed any new temporary paths:

go build -gcflags -trimpath=$PWD

strings example | grep /tmp

What did you see instead?

Using -trimpath removes the path to the code, but now the _gomod_.go is referenced with a temporary path:

go build -gcflags -trimpath=$PWD

strings example | grep /tmp
/tmp/go-build222692051/b001/_gomod_.go

Additional remarks

It does not matter which path is removed via -trimpath, the temporary path to _gomod_.go is embedded as soon as -trimpath is used, e.g.:

go build -gcflags -trimpath=/x

strings example | grep /tmp
/tmp/go-build425085084/b001/_gomod_.go
@rsc
Copy link
Contributor

rsc commented Oct 24, 2018

This is a duplicate of #16860.

@fd0
Copy link
Author

fd0 commented Oct 28, 2018

For the record, when building without modules, I don't observe this behavior, i.e. trimpath works as it should.

@golang golang locked and limited conversation to collaborators Oct 28, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge modules NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

4 participants