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

Comments

Projects
None yet
3 participants
@mvdan
Copy link
Member

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 this to the Go1.12 milestone Dec 1, 2018

@bcmills bcmills added the GoCommand label Dec 1, 2018

@mvdan

This comment has been minimized.

Copy link
Member Author

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

This comment has been minimized.

Copy link
Member Author

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

This comment has been minimized.

Copy link
Member Author

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

This comment has been minimized.

Copy link

commented Dec 1, 2018

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

@gopherbot

This comment has been minimized.

Copy link

commented Dec 4, 2018

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

@gopherbot gopherbot closed this in 8a5797a Dec 4, 2018

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.