Description
What version of Go are you using (go version
)?
$ go version go version go1.16.13 darwin/amd64
Does this issue reproduce with the latest release?
Yes.
What did you do?
I set GOFLAGS="-ldflags='-s -w'"
to automatically exclude the symbol table on all binaries we compile.
What did you expect to see?
I expected it to work the same as if I passed -ldflags='-s -w'
.
What did you see instead?
go: parsing $GOFLAGS: unknown flag -w'
This is because the compiler naively just splits GOFLAGS
on spaces without considering quotes. The docs support this unfortunate behavior.
A space-separated list of -flag=value settings to apply to go commands by default, when the given flag is known by the current command. Each entry must be a standalone flag. Because the entries are space-separated, flag values must not contain spaces. Flags listed on the command line are applied after this list and therefore override it.
There is no workaround for this, other than to not use GOFLAGS
for this purpose. Passing GOFLAGS="-ldflags=-s -ldflags=-w"
as was previously suggested by the documention does not work, becuase only the last flag will be applied, as per the documentation for -ldflags
.
This issue was previously mentioned in #29053 but was not truly fixed.
Either the GOFLAGS
parsing logic should be amended to account for quotes instead of just splitting on spaces, or another env var (perhpas GO_LDFLAGS
to align with what make.bash offers) should be added.
Activity
ianlancetaylor commentedon Feb 14, 2022
I think this is effectively a dup of #26849.
mvdan commentedon Feb 15, 2022
Have to agree with @ianlancetaylor; closing.