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: the example 'GOFLAGS=-ldflags=-s -ldflags=-w' does not work #29053

Closed
mvdan opened this issue Dec 1, 2018 · 5 comments
Closed

cmd/go: the example 'GOFLAGS=-ldflags=-s -ldflags=-w' does not work #29053

mvdan opened this issue Dec 1, 2018 · 5 comments
Labels
FrozenDueToAge GoCommand cmd/go NeedsFix The path to resolution is known, but the work has not been done. release-blocker
Milestone

Comments

@mvdan
Copy link
Member

mvdan commented Dec 1, 2018

$ go version
go version devel +f70bd91435 Sat Dec 1 05:30:16 2018 +0000 linux/amd64
$ go env
GOARCH="amd64"
GOBIN="/home/mvdan/go/bin"
GOCACHE="/home/mvdan/go/cache"
GOEXE=""
GOFLAGS="-ldflags=-s -ldflags=-w"
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/mvdan/go/land:/home/mvdan/go"
GOPROXY=""
GORACE=""
GOROOT="/home/mvdan/tip"
GOTMPDIR=""
GOTOOLDIR="/home/mvdan/tip/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-build539552164=/tmp/go-build -gno-record-gcc-switches"

In https://golang.org/cmd/go/, one can read:

for example, to set '-ldflags=-s -w' you can use 'GOFLAGS=-ldflags=-s -ldflags=-w'.

I don't use a debugger, so skipping DWARF and symbol tables speeds up linking binaries and saves space on disk. So I've set up export GOFLAGS="-ldflags=-s -ldflags=-w" in my .bash_profile, like the doc page says. And that works fine for go build and go install, for example.

However:

$ go vet ./...
go vet: ldflags flag may be set only once
run "go help vet" for more information
$ GOFLAGS= go vet ./...
[success]

This might be because go vet does its own flag parsing. The fact that go test shows the same bug seems to point in that direction.

Seems like a bug to fix in 1.12, since the cmd/go doc page suggests doing something that completely breaks go test and go vet. It's also broken on 1.11.2. I'll have a quick look this afternoon.

/cc @bcmills @alandonovan

@bcmills bcmills added NeedsFix The path to resolution is known, but the work has not been done. release-blocker labels Dec 1, 2018
@bcmills bcmills added this to the Go1.12 milestone Dec 1, 2018
@bcmills bcmills added the GoCommand cmd/go label Dec 1, 2018
@mvdan
Copy link
Member Author

mvdan commented Dec 1, 2018

This is actually worse than I thought - it seems like repeated flags don't even work with go build:

$ cat main.go
package main

func main() {}
$ GOFLAGS='-ldflags=-s -ldflags=-w' go build -x main.go
[...]
[...]/link [...] -w [...]

Seems like only the last -ldflags gets passed down.

@mvdan
Copy link
Member Author

mvdan commented Dec 1, 2018

The same happens with go build -ldflags=-s -ldflags=-w -x main.go, so I don't think this is particular to GOFLAGS.

@mvdan
Copy link
Member Author

mvdan commented Dec 1, 2018

It looks like that's how PerPackageFlag is supposed to work:

// Special case: -gcflags="" means no flags for command-line arguments
// (overrides previous -gcflags="-whatever").

That is, -ldflags=-foo -ldflags=-bar is supposed to override and not merge. If they always merged, there wouldn't be a way to override previous flags. That explains why my commands above don't work as I expected.

That begs the question; how can one use -ldflags='-w -s' in GOFLAGS, then? The way documented in cmd/go is clearly not working. Do we need to fix how GOFLAGS works, or do we need to change how PerPackageFlag handles repeated flags? I presume the former.

For now, I'll send a CL to at least stop go vet and go test from giving false errors.

/cc @rsc

@mvdan mvdan changed the title cmd/go: incorrect "flag may only be set once" error on vet and test cmd/go: the example 'GOFLAGS=-ldflags=-s -ldflags=-w' does not work Dec 1, 2018
@gopherbot
Copy link

Change https://golang.org/cl/151980 mentions this issue: cmd/gointernal/cmdflag: remove "set only once" check

@gopherbot
Copy link

Change https://golang.org/cl/152479 mentions this issue: cmd/go: revert multi-flag GOFLAGS doc example

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge GoCommand cmd/go NeedsFix The path to resolution is known, but the work has not been done. release-blocker
Projects
None yet
Development

No branches or pull requests

3 participants