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: build/install without package argument doesn't evaluate -ldflags on Windows #24750

Closed
RolandvdH opened this issue Apr 7, 2018 · 12 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. OS-Windows
Milestone

Comments

@RolandvdH
Copy link

RolandvdH commented Apr 7, 2018

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

go version go1.10 windows/amd64

Does this issue reproduce with the latest release?

yes

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

set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\Roland\AppData\Local\go-build
set GOEXE=.exe
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=d:\dvlp\go
set GORACE=
set GOROOT=C:\Go
set GOTMPDIR=
set GOTOOLDIR=C:\Go\pkg\tool\windows_amd64
set GCCGO=gccgo
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=C:\Users\Roland\AppData\Local\Temp\go-build158086470=/tmp/go-build -gno-record-gcc-switches

What did you do?

In GOPATH\src\hello\main,go:

package main

import (
	"fmt"
)

var version string = "non provided"

func main() {
	fmt.Printf("version: %s\n", version)
}
$ go build -ldflags="-X main.version=0.0.1"
$ hello
version: non provided

$ go build -ldflags="-X main.version=0.0.1" hello
$ hello
version: 0.0.1

What did you expect to see?

version to have the ldflags supplied value.

What did you see instead?

Build without package specified did not replace version value. (Same difference when using go install).

@mvdan
Copy link
Member

mvdan commented Apr 8, 2018

Are you positive that you were running the right binary in both cases? I cannot reproduce this with 1.10.1 on Linux.

@mvdan mvdan added WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels Apr 8, 2018
@mvdan mvdan changed the title go build/install w/o package doesn't evaluate -ldflags cmd/go: build/install without package argument doesn't evaluate -ldflags Apr 8, 2018
@RolandvdH
Copy link
Author

@mvdan As stated this happened on 1.10 Windows. I didn't try Linux. I did try the syntax with a .go program unit instead of a package name, this worked as expected.

@RolandvdH
Copy link
Author

Oh and I tried some other flags (like "-s -w"). These too didn't have any effect.

@mvdan
Copy link
Member

mvdan commented Apr 9, 2018

Thanks. For now I'm going to assume that this has something to do with Windows, and wait for someone to try to reproduce the issue on a similar environment.

If you have more information to share, please do.

@mvdan mvdan added OS-Windows and removed WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. labels Apr 9, 2018
@mvdan mvdan changed the title cmd/go: build/install without package argument doesn't evaluate -ldflags cmd/go: build/install without package argument doesn't evaluate -ldflags on Windows Apr 9, 2018
@alexbrainman
Copy link
Member

Yes. I think this is broken.

c:\>cd %GOPATH%\src\issue\go\24750

c:\Users\Alex\dev\src\issue\go\24750>type main.go
package main

import (
        "fmt"
)

var version string = "non provided"

func main() {
        fmt.Printf("version: %s\n", version)
}

c:\Users\Alex\dev\src\issue\go\24750>go build -ldflags="-X main.version=0.0.1" -o a.exe issue/go/24750 && a.exe
version: 0.0.1

c:\Users\Alex\dev\src\issue\go\24750>go build -ldflags="-X main.version=0.0.1" -o a.exe && a.exe
version: non provided

c:\Users\Alex\dev\src\issue\go\24750>

Alex

@mvdan mvdan added this to the Go1.11 milestone Apr 9, 2018
@fkollmann
Copy link

Works fine for me with current development version:

C:\Projekte\go-source\bin\go.exe build -ldflags="-X main.version=0.0.1" -o a.exe

.\a.exe
version: 0.0.1

C:\Projekte\go-source\bin\go.exe version
go version devel +bbfae469a1 Sat Apr 7 21:01:07 2018 +0000 windows/amd64

@alexbrainman
Copy link
Member

@fkollmann does not work for me for bbfae46

I do not know what is different between your system and mine.

Alex

@fkollmann
Copy link

fkollmann commented Apr 15, 2018

@alexbrainman that's odd, I reconfirmed that it's working for me.

To get the obvious out of the way, I also confirmed it does not depend on the path, or GOROOT or GOPATH being set. It also does not depend on whether cmd or powershell is being used.

So I guess the difference has to be at the compile time of go.exe (read: how go was built). This is, what I did using Powershell:

git clone https://github.com/golang/go.git go-source-24750
cd .\go-source-24750\
git checkout bbfae469a1d207a415dd813d799df1ed2dd2d80a

$env:GOROOT_BOOTSTRAP="C:\Go\" ; $env:CGO_ENABLED="0" ; $env:GOPATH=$PWD.Path ; $env:GOBIN="$env:GOPATH\bin" ; $env:GOROOT=$env:GOPATH

.\all.bat

I get the following result:

$env:GOROOT="C:\Projekte\go-source-24750\"

& "$env:GOROOT\bin\go.exe" build -ldflags="-X main.version=0.0.1" -o a.exe

.\a.exe
version: 0.0.1

& "$env:GOROOT\bin\go.exe" version
go version devel +bbfae469a1 Sat Apr 7 21:01:07 2018 +0000 windows/amd64

@alexbrainman
Copy link
Member

@fkollmann I do not know why it works for you. Here is my environment:

set MYHOME=c:\users\alex\zzz
set GOROOT=%MYHOME%\go
set GOROOT_BOOTSTRAP=%MYHOME%\go1.4.3
set GOPATH=%MYHOME%
set MINGW=%MYHOME%\mingw
set PATH=%PATH%;%MINGW%\bin;%GOROOT%\bin
cd %GOROOT%\src
cmd

Alex

@jrwren
Copy link

jrwren commented Apr 19, 2018

I have the same issue with Go 1.10.1 on Macos.

Thanks to the OP report that it has to do with package argument I am able to build again, so I am very happy.

It seems that -ldflags isn't being passed to link.

In my case, I was trying to build using a relative package name ./bin/myserverd by changing this to the full package name I was able to build. g/s/bin/myserverd

The package named there is nested rather deeply in my homedir for mono repo reasons and so I symlink a dir, s, in my homedir to src/checkout/go/src/g/s/. This is related. I do not experience the problem if my cwd is $HOME/src/checkout/go/src/g/s/ but I do experience the problem if my cwd is $HOME/s/

Here is an example of the seg fault output. I'm trimming the package file output as it does not seem relevant:

$ go build -x -ldflags='-v -s' ./bin/streetwatchd
WORK=/var/folders/4v/d6697ns10hxd9rx5bnlns4s40000gq/T/go-build128070422
mkdir -p $WORK/b001/
cat >$WORK/b001/importcfg.link << 'EOF' # internal
packagefile....
EOF
mkdir -p $WORK/b001/exe/
cd .
/usr/local/Cellar/go/1.10.1/libexec/pkg/tool/darwin_amd64/link -o $WORK/b001/exe/a.out -importcfg $WORK/b001/importcfg.link -buildmode=exe -buildid=aGprtrH0Eqaos8kIv3mG/_tqWao8a9TORxlmwMWUm/_CMchbc9kZyIhBc0QztG/aGprtrH0Eqaos8kIv3mG -extld=clang /Users/jay.wren/Library/Caches/go-build/08/08241fa23cddc23cdd0a05d92025406c7be27350223efe13f490da700a76c4ec-d
# sm/streetwatch/bin/streetwatchd
/usr/local/Cellar/go/1.10.1/libexec/pkg/tool/darwin_amd64/link: /usr/local/Cellar/go/1.10.1/libexec/pkg/tool/darwin_amd64/link: running dsymutil failed: signal: segmentation fault

If I use a slightly different, but should be equivalent package name:

$ go build -x -ldflags='-v -s' sm/streetwatch/bin/streetwatchd
WORK=/var/folders/4v/d6697ns10hxd9rx5bnlns4s40000gq/T/go-build284099272
mkdir -p $WORK/b001/
cat >$WORK/b001/importcfg.link << 'EOF' # internal
packagefile...
EOF
mkdir -p $WORK/b001/exe/
cd .
/usr/local/Cellar/go/1.10.1/libexec/pkg/tool/darwin_amd64/link -o $WORK/b001/exe/a.out -importcfg $WORK/b001/importcfg.link -buildmode=exe -buildid=VrcDpKq8-k_83_bRwGi-/_tqWao8a9TORxlmwMWUm/_CMchbc9kZyIhBc0QztG/VrcDpKq8-k_83_bRwGi- -v -s -extld=clang /Users/jay.wren/Library/Caches/go-build/08/08241fa23cddc23cdd0a05d92025406c7be27350223efe13f490da700a76c4ec-d
/usr/local/Cellar/go/1.10.1/libexec/pkg/tool/darwin_amd64/buildid -w $WORK/b001/exe/a.out # internal
# sm/streetwatch/bin/streetwatchd
HEADER = -H1 -T0x1001000 -D0x0 -R0x1000
 0.00 deadcode
 0.18 pclntab=5630361 bytes, funcdata total 927501 bytes
 0.20 dodata
 0.21 symsize = 0
 0.23 symsize = 0
 0.26 dynreloc
 0.30 dwarf
 0.37 symsize = 0
 0.57 reloc
 0.66 asmb
 0.66 codeblk
 0.75 datblk
 0.82 sym
 0.88 headr
 0.97 host link: "clang" "-m64" "-Wl,-headerpad,1144" "-Wl,-no_pie" "-Wl,-pagezero_size,4000000" "-o" "/var/folders/4v/d6697ns10hxd9rx5bnlns4s40000gq/T/go-build284099272/b001/exe/a.out" "-Qunused-arguments" "/var/folders/4v/d6697ns10hxd9rx5bnlns4s40000gq/T/go-link-560459325/go.o" "/var/folders/4v/d6697ns10hxd9rx5bnlns4s40000gq/T/go-link-560459325/000000.o" "/var/folders/4v/d6697ns10hxd9rx5bnlns4s40000gq/T/go-link-560459325/000001.o" "/var/folders/4v/d6697ns10hxd9rx5bnlns4s40000gq/T/go-link-560459325/000002.o" "/var/folders/4v/d6697ns10hxd9rx5bnlns4s40000gq/T/go-link-560459325/000003.o" "/var/folders/4v/d6697ns10hxd9rx5bnlns4s40000gq/T/go-link-560459325/000004.o" "/var/folders/4v/d6697ns10hxd9rx5bnlns4s40000gq/T/go-link-560459325/000005.o" "/var/folders/4v/d6697ns10hxd9rx5bnlns4s40000gq/T/go-link-560459325/000006.o" "/var/folders/4v/d6697ns10hxd9rx5bnlns4s40000gq/T/go-link-560459325/000007.o" "/var/folders/4v/d6697ns10hxd9rx5bnlns4s40000gq/T/go-link-560459325/000008.o" "/var/folders/4v/d6697ns10hxd9rx5bnlns4s40000gq/T/go-link-560459325/000009.o" "/var/folders/4v/d6697ns10hxd9rx5bnlns4s40000gq/T/go-link-560459325/000010.o" "/var/folders/4v/d6697ns10hxd9rx5bnlns4s40000gq/T/go-link-560459325/000011.o" "/var/folders/4v/d6697ns10hxd9rx5bnlns4s40000gq/T/go-link-560459325/000012.o" "/var/folders/4v/d6697ns10hxd9rx5bnlns4s40000gq/T/go-link-560459325/000013.o" "/var/folders/4v/d6697ns10hxd9rx5bnlns4s40000gq/T/go-link-560459325/000014.o" "/var/folders/4v/d6697ns10hxd9rx5bnlns4s40000gq/T/go-link-560459325/000015.o" "/var/folders/4v/d6697ns10hxd9rx5bnlns4s40000gq/T/go-link-560459325/000016.o" "/var/folders/4v/d6697ns10hxd9rx5bnlns4s40000gq/T/go-link-560459325/000017.o" "/var/folders/4v/d6697ns10hxd9rx5bnlns4s40000gq/T/go-link-560459325/000018.o" "/var/folders/4v/d6697ns10hxd9rx5bnlns4s40000gq/T/go-link-560459325/000019.o" "/var/folders/4v/d6697ns10hxd9rx5bnlns4s40000gq/T/go-link-560459325/000020.o" "/var/folders/4v/d6697ns10hxd9rx5bnlns4s40000gq/T/go-link-560459325/000021.o" "/var/folders/4v/d6697ns10hxd9rx5bnlns4s40000gq/T/go-link-560459325/000022.o" "/var/folders/4v/d6697ns10hxd9rx5bnlns4s40000gq/T/go-link-560459325/000023.o" "/var/folders/4v/d6697ns10hxd9rx5bnlns4s40000gq/T/go-link-560459325/000024.o" "/var/folders/4v/d6697ns10hxd9rx5bnlns4s40000gq/T/go-link-560459325/000025.o" "/var/folders/4v/d6697ns10hxd9rx5bnlns4s40000gq/T/go-link-560459325/000026.o" "/var/folders/4v/d6697ns10hxd9rx5bnlns4s40000gq/T/go-link-560459325/000027.o" "/var/folders/4v/d6697ns10hxd9rx5bnlns4s40000gq/T/go-link-560459325/000028.o" "/var/folders/4v/d6697ns10hxd9rx5bnlns4s40000gq/T/go-link-560459325/000029.o" "/var/folders/4v/d6697ns10hxd9rx5bnlns4s40000gq/T/go-link-560459325/000030.o" "/var/folders/4v/d6697ns10hxd9rx5bnlns4s40000gq/T/go-link-560459325/000031.o" "/var/folders/4v/d6697ns10hxd9rx5bnlns4s40000gq/T/go-link-560459325/000032.o" "/var/folders/4v/d6697ns10hxd9rx5bnlns4s40000gq/T/go-link-560459325/000033.o" "/var/folders/4v/d6697ns10hxd9rx5bnlns4s40000gq/T/go-link-560459325/000034.o" "/var/folders/4v/d6697ns10hxd9rx5bnlns4s40000gq/T/go-link-560459325/000035.o" "/var/folders/4v/d6697ns10hxd9rx5bnlns4s40000gq/T/go-link-560459325/000036.o" "/var/folders/4v/d6697ns10hxd9rx5bnlns4s40000gq/T/go-link-560459325/000037.o" "/var/folders/4v/d6697ns10hxd9rx5bnlns4s40000gq/T/go-link-560459325/000038.o" "/var/folders/4v/d6697ns10hxd9rx5bnlns4s40000gq/T/go-link-560459325/000039.o" "/var/folders/4v/d6697ns10hxd9rx5bnlns4s40000gq/T/go-link-560459325/000040.o" "/var/folders/4v/d6697ns10hxd9rx5bnlns4s40000gq/T/go-link-560459325/000041.o" "/var/folders/4v/d6697ns10hxd9rx5bnlns4s40000gq/T/go-link-560459325/000042.o" "/var/folders/4v/d6697ns10hxd9rx5bnlns4s40000gq/T/go-link-560459325/000043.o" "/var/folders/4v/d6697ns10hxd9rx5bnlns4s40000gq/T/go-link-560459325/000044.o" "/var/folders/4v/d6697ns10hxd9rx5bnlns4s40000gq/T/go-link-560459325/000045.o" "/var/folders/4v/d6697ns10hxd9rx5bnlns4s40000gq/T/go-link-560459325/000046.o" "/var/folders/4v/d6697ns10hxd9rx5bnlns4s40000gq/T/go-link-560459325/000047.o" "/var/folders/4v/d6697ns10hxd9rx5bnlns4s40000gq/T/go-link-560459325/000048.o" "/var/folders/4v/d6697ns10hxd9rx5bnlns4s40000gq/T/go-link-560459325/000049.o" "/var/folders/4v/d6697ns10hxd9rx5bnlns4s40000gq/T/go-link-560459325/000050.o" "/var/folders/4v/d6697ns10hxd9rx5bnlns4s40000gq/T/go-link-560459325/000051.o" "/var/folders/4v/d6697ns10hxd9rx5bnlns4s40000gq/T/go-link-560459325/000052.o" "/var/folders/4v/d6697ns10hxd9rx5bnlns4s40000gq/T/go-link-560459325/000053.o" "/var/folders/4v/d6697ns10hxd9rx5bnlns4s40000gq/T/go-link-560459325/000054.o" "/var/folders/4v/d6697ns10hxd9rx5bnlns4s40000gq/T/go-link-560459325/000055.o" "/var/folders/4v/d6697ns10hxd9rx5bnlns4s40000gq/T/go-link-560459325/000056.o" "/var/folders/4v/d6697ns10hxd9rx5bnlns4s40000gq/T/go-link-560459325/000057.o" "/var/folders/4v/d6697ns10hxd9rx5bnlns4s40000gq/T/go-link-560459325/000058.o" "/var/folders/4v/d6697ns10hxd9rx5bnlns4s40000gq/T/go-link-560459325/000059.o" "-g" "-O2" "-g" "-O2" "-framework" "CoreFoundation" "-framework" "Security" "-g" "-O2" "-g" "-O2" "-g" "-O2" "-lpthread" "-g" "-O2" "-g" "-O2" "-g" "-O2" "-L/usr/local/opt/openssl@1.1/lib" "-L/usr/local/opt/openssl/lib" "-lssl" "-lcrypto" "-nopie"
 1.34 cpu time
367689 symbols
471028 liveness data
mv $WORK/b001/exe/a.out streetwatchd
rm -r $WORK/b001/

@gopherbot
Copy link

Change https://golang.org/cl/109235 mentions this issue: cmd/go/internal/load: use strings.ToLower when comparing package directory names

@gopherbot
Copy link

Change https://golang.org/cl/129059 mentions this issue: cmd/go: fix -gcflags, -ldflags not applying to current directory

gopherbot pushed a commit that referenced this issue Aug 17, 2018
A flag setting like -gcflags=-e applies only to the packages
named on the command line, not to their dependencies.
The way we used to implement this was to remember the
command line arguments, reinterpret them as pattern matches
instead of package argument generators (globs), and apply them
during package load. The reason for this complexity was to
address a command-line like:

	go build -gcflags=-e fmt runtime

The load of fmt will load dependencies, including runtime,
and the load of runtime will reuse the result of the earlier load.
Because we were computing the effective -gcflags for each
package during the load, we had to have a way to tell, when
encountering runtime during the load of fmt, that runtime had
been named on the command line, even though we hadn't
gotten that far. That would be easy if the only possible
arguments were import paths, but we also need to handle

	go build -gcflags=-e fmt runt...
	go build -gcflags=-e fmt $GOROOT/src/runtime
	go build -gcflags=-e fmt $GOROOT/src/runt...
	and so on.

The match predicates usually did their job well, but not
always. In particular, thanks to symlinks and case-insensitive
file systems and unusual ways to spell file paths, it's always
been possible in various corner cases to give an argument
that evalutes to the runtime package during loading but
failed to match it when reused to determine "was this package
named on the command line?"

CL 109235 fixed one instance of this problem by making
a directory pattern match case-insensitive on Windows, but that
is incorrect in some other cases and doesn't address the root problem,
namely that there will probably always be odd corner cases
where pattern matching and pattern globbing are not exactly aligned.

This CL eliminates the assumption that pattern matching
and pattern globbing are always completely in agreement,
by simply marking the packages named on the command line
after the package load returns them. This means delaying
the computation of tool flags until after the load too,
for a few different ways packages are loaded.
The different load entry points add some complexity,
which is why the original approach seemed more attractive,
but the original approach had complexity that we simply
didn't recognize at the time.

This CL then rolls back the CL 109235 pattern-matching change,
but it keeps the test introduced in that CL. That test still passes.

In addition to fixing ambiguity due to case-sensitive file systems,
this new approach also very likely fixes various ambiguities that
might arise from abuse of symbolic links.

Fixes #24232.
Fixes #24456.
Fixes #24750.
Fixes #25046.
Fixes #25878.

Change-Id: I0b09825785dfb5112fb11494cff8527ebf57966f
Reviewed-on: https://go-review.googlesource.com/129059
Run-TryBot: Russ Cox <rsc@golang.org>
Reviewed-by: Alan Donovan <adonovan@google.com>
@golang golang locked and limited conversation to collaborators Aug 10, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. OS-Windows
Projects
None yet
Development

No branches or pull requests

6 participants