Skip to content

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

Closed
@rittneje

Description

@rittneje

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

ianlancetaylor commented on Feb 14, 2022

@ianlancetaylor
Contributor

I think this is effectively a dup of #26849.

mvdan

mvdan commented on Feb 15, 2022

@mvdan
Member

Have to agree with @ianlancetaylor; closing.

locked and limited conversation to collaborators on Feb 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @ianlancetaylor@mvdan@gopherbot@rittneje

        Issue actions

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