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: cannot pass multiple -ldflags via GOFLAGS #51194

Closed
rittneje opened this issue Feb 14, 2022 · 2 comments
Closed

cmd/go: cannot pass multiple -ldflags via GOFLAGS #51194

rittneje opened this issue Feb 14, 2022 · 2 comments

Comments

@rittneje
Copy link

rittneje commented Feb 14, 2022

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.

@ianlancetaylor
Copy link
Contributor

I think this is effectively a dup of #26849.

@mvdan
Copy link
Member

mvdan commented Feb 15, 2022

Have to agree with @ianlancetaylor; closing.

@mvdan mvdan closed this as completed Feb 15, 2022
@golang golang locked and limited conversation to collaborators Feb 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants