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: -ldflags pattern matching broken when building from symlinked pwd #24359

Closed
knz opened this issue Mar 12, 2018 · 12 comments
Closed

cmd/go: -ldflags pattern matching broken when building from symlinked pwd #24359

knz opened this issue Mar 12, 2018 · 12 comments
Labels
FrozenDueToAge help wanted NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@knz
Copy link

knz commented Mar 12, 2018

tl;dr: if the PWD of the current shell is a symlink to a dir in $GOPATH, then linking with -X does not work.

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

go version go1.10 freebsd/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="/data/home/kena/.cache/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="freebsd"
GOOS="freebsd"
GOPATH="/data/home/kena/src/go"
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/freebsd_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
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 -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build068611065=/tmp/go-build -gno-record-gcc-switches"

What did you do?

Check the README here: https://github.com/knz/build-x-bug

In short:

  • make package in $GOPATH. Use go build -ldflags '-X yourpackage/var=val' -> this works
  • create symlink to package in $GOPATH, in another dir, set that as PWD. Then use same go build command -> this breaks, -X does not set the var.

What did you expect to see?

-X with a package path relative to $GOPATH should work regardless of PWD.

What did you see instead?

Whether -X works properly or not is dependent on the PWD.

Suggested fix.

use filepath.Abs on the PWD before trying to make clever comparisons in the linker.

@knz
Copy link
Author

knz commented Mar 12, 2018

I think that filepath.Abs on the pwd is not sufficient, GOPATH itself must also be normalized with Abs.

@knz
Copy link
Author

knz commented Mar 12, 2018

I also think this is a regression with 1.10 - wasn't there in 1.9

@andybons andybons changed the title tools/link: -X broken when building from symlinked pwd cmd/link: -X broken when building from symlinked pwd Mar 13, 2018
@andybons
Copy link
Member

/cc @ianlancetaylor

@andybons andybons added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Mar 13, 2018
@ianlancetaylor ianlancetaylor changed the title cmd/link: -X broken when building from symlinked pwd cmd/go: -ldflags pattern matching broken when building from symlinked pwd Mar 13, 2018
@ianlancetaylor
Copy link
Member

It works if you use -ldflags=all='-X ...'.

Perhaps the $PWD referring to a symlink changes the matching of the -ldflags pattern somehow.

@ianlancetaylor
Copy link
Member

Likely could be fixed by calling filepath.EvalSymlinks in getwd before setting base.Cwd in cmd/go/internal/base.

@ianlancetaylor ianlancetaylor added NeedsFix The path to resolution is known, but the work has not been done. help wanted labels Mar 13, 2018
@gopherbot gopherbot removed the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Mar 13, 2018
@ianlancetaylor ianlancetaylor added this to the Go1.11 milestone Mar 13, 2018
@ysmolski
Copy link
Member

@ianlancetaylor I have verified your version and it turns out to be a working fix. Should we have a proper test for it in go_test.go?

@ianlancetaylor
Copy link
Member

Yes, it ought to have a test.

@gopherbot
Copy link
Contributor

Change https://golang.org/cl/101256 mentions this issue: cmd/go: fix -X importpath.name=value when building from symlinked wd

@rsc
Copy link
Contributor

rsc commented Apr 18, 2018

I wrote on the CL:

In general, don't use symlinks and expect things to work.
The call to EvalSymlinks will fix this one test case, where evaluating the symlink
makes the path move into GOPATH. But it will break the inverse test case, where
evaluating the symlink makes the path move out of GOPATH.

The answer is to not use symlinks.

I think we should leave the current behavior alone. GOPATH is on its way out anyway.

@rsc rsc closed this as completed Apr 18, 2018
@knz
Copy link
Author

knz commented Apr 18, 2018

In general, don't use symlinks and expect things to work.

It worked in every Go version prior to 1.10. Why break it until GOPATH is out?

@ianlancetaylor
Copy link
Member

It worked by accident in every prior Go version. I don't feel strongly about this particular issue, but as a general rule we don't want to lock ourselves into behavior that was not documented and that worked by accident.

@RaduBerinde
Copy link
Contributor

In general, don't use symlinks and expect things to work.
The call to EvalSymlinks will fix this one test case, where evaluating the symlink
makes the path move into GOPATH. But it will break the inverse test case, where
evaluating the symlink makes the path move out of GOPATH.

The answer is to not use symlinks.

It's reasonable to disallow symlinks in the GOPATH itself. But whether the cwd is a symlink or not shouldn't matter. At least that's my expectation as a user (and it's not something I would expect to be specifically documented).

benesch added a commit to benesch/cockroach that referenced this issue Apr 23, 2018

Verified

This commit was signed with the committer’s verified signature.
benesch Nikhil Benesch
Build info injected via linker flags, like the Git commit SHA, is
dropped on the floor when building from a symlink (e.g., ~/roach ->
~/go/src/cockroachdb/cockroach). This issue was filed upstream
(golang/go#24359), but the Go team refused to fix it. Luckily, we can
work around it by teaching Make to resolve any symlinks in the CWD.

Release note: None
benesch added a commit to benesch/cockroach that referenced this issue Apr 25, 2018
Build info injected via linker flags, like the Git commit SHA, is
dropped on the floor when building from a symlink (e.g., ~/roach ->
~/go/src/cockroachdb/cockroach). This issue was filed upstream
(golang/go#24359), but the Go team refused to fix it. Luckily, we can
work around it by teaching Make to resolve any symlinks in the CWD.

Release note (build change): Build metadata, like the commit SHA and build
time, is properly injected into the binary when using Go 1.10 and building from
a symlink.
craig bot pushed a commit to cockroachdb/cockroach that referenced this issue Apr 25, 2018
25008: Makefile: work around symlink bug in Go toolchain r=knz a=benesch

Build info injected via linker flags, like the Git commit SHA, is
dropped on the floor when building from a symlink (e.g., ~/roach ->
~/go/src/cockroachdb/cockroach). This issue was filed upstream
(golang/go#24359), but the Go team refused to fix it. Luckily, we can
work around it by teaching Make to resolve any symlinks in the CWD.

Release note (build change): Build metadata, like the commit SHA and build
time, is properly injected into the binary when using Go 1.10 and building from
a symlink.

Co-authored-by: Nikhil Benesch <nikhil.benesch@gmail.com>
benesch added a commit to benesch/cockroach that referenced this issue Apr 25, 2018

Verified

This commit was signed with the committer’s verified signature.
benesch Nikhil Benesch
Build info injected via linker flags, like the Git commit SHA, is
dropped on the floor when building from a symlink (e.g., ~/roach ->
~/go/src/cockroachdb/cockroach). This issue was filed upstream
(golang/go#24359), but the Go team refused to fix it. Luckily, we can
work around it by teaching Make to resolve any symlinks in the CWD.

Release note (build change): Build metadata, like the commit SHA and build
time, is properly injected into the binary when using Go 1.10 and building from
a symlink.
craig bot pushed a commit to cockroachdb/cockroach that referenced this issue Apr 26, 2018
25062: backport-2.0: Makefile: work around symlink bug in Go toolchain r=knz a=benesch

Backport 1/1 commits from #25008.

/cc @cockroachdb/release

---

Build info injected via linker flags, like the Git commit SHA, is
dropped on the floor when building from a symlink (e.g., ~/roach ->
~/go/src/cockroachdb/cockroach). This issue was filed upstream
(golang/go#24359), but the Go team refused to fix it. Luckily, we can
work around it by teaching Make to resolve any symlinks in the CWD.

Release note (build change): Build metadata, like the commit SHA and build
time, is properly injected into the binary when using Go 1.10 and building from
a symlink.


Co-authored-by: Nikhil Benesch <nikhil.benesch@gmail.com>
@golang golang locked and limited conversation to collaborators Apr 23, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge help wanted NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

7 participants