Skip to content

Fix make with LDFLAGS#162

Open
Tim Hockin (thockin) wants to merge 1 commit into
agent-substrate:mainfrom
thockin:fix-ko-ldflags
Open

Fix make with LDFLAGS#162
Tim Hockin (thockin) wants to merge 1 commit into
agent-substrate:mainfrom
thockin:fix-ko-ldflags

Conversation

@thockin
Copy link
Copy Markdown
Collaborator

@thockin Tim Hockin (thockin) commented Jun 3, 2026

$(KO) != $(GO)

ko does not support any way to pass build flags directly. We have to
use GOFLAGS. Go does not fully shell-parse GOFLAGS, it just does simple
whitespace splitting and whole-value quoting.

E.g. this fails for good reason:
GOFLAGS="-ldflags=-X=foo=foo -X=bar=bar"
...becomes:
[ "go", "build", "-ldflags=-X=foo=foo", "-X=bar=bar" ]
...where -X is passed to go build

We can try:
GOFLAGS="-ldflags='-X=foo=foo -X=bar=bar'"
...but that becomes
[ "go", "build", "-ldflags='-X=foo=foo", "-X=bar=bar'" ]
...which is unfortunate.

Go does some quote handling, but:
GOFLAGS='-ldflags="-X=foo=foo -X=bar=bar"'
...still becomes:
[ "go", "build", "-ldflags="-X=foo=foo", "-X=bar=bar"" ]
...which is surprising.

We can try:
GOFLAGS="-ldflags=-X=foo=foo -ldflags=-X=bar=bar"
...but go just takes the last value of that flag rather than the accumulation.

What does work is:
GOFLAGS='"-ldflags=-X=foo=foo -X=bar=bar"'
...with the WHOLE flag quoted.

@BenTheElder
Copy link
Copy Markdown
Collaborator

How does this behave with the install scripts, which do not use the makefile at all AFAIK?

@BenTheElder
Copy link
Copy Markdown
Collaborator

Do we really need ldflags? Why not just use the Go-buil- in version stamping? Kubernetes uses ldflags because this functionality didn't exist.

@thockin
Copy link
Copy Markdown
Collaborator Author

There's no way I can see to set a version string in Go's build info?

@BenTheElder
Copy link
Copy Markdown
Collaborator

Go automatically grabs the version from git

@thockin
Copy link
Copy Markdown
Collaborator Author

vcs.revision is the commit, we want a symbolic name for releases

@thockin Tim Hockin (thockin) changed the title Fix make with LDFLAGSo Fix make with LDFLAGS Jun 3, 2026
$(KO) != $(GO)

ko does not support any way to pass build flags directly.  We have to
use GOFLAGS.  Go does not fully shell-parse GOFLAGS, it just does simple
whitespace splitting and whole-value quoting.

E.g. this fails for good reason:
    GOFLAGS="-ldflags=-X=foo=foo -X=bar=bar"
...becomes:
    [ "go", "build", "-ldflags=-X=foo=foo", "-X=bar=bar" ]
...where `-X` is passed to `go build`

We can try:
    GOFLAGS="-ldflags='-X=foo=foo -X=bar=bar'"
...but that becomes
    [ "go", "build", "-ldflags='-X=foo=foo", "-X=bar=bar'" ]
...which is unfortunate.

Go does some quote handling, but:
    GOFLAGS='-ldflags="-X=foo=foo -X=bar=bar"'
...still becomes:
    [ "go", "build", "-ldflags=\"-X=foo=foo", "-X=bar=bar\"" ]
...which is surprising.

We can try:
    GOFLAGS="-ldflags=-X=foo=foo -ldflags=-X=bar=bar"
...but go just takes the last value of that flag rather than the
accumulation.

What does work is:
    GOFLAGS='"-ldflags=-X=foo=foo -X=bar=bar"'
...with the WHOLE flag quoted.
@thockin
Copy link
Copy Markdown
Collaborator Author

Found a way to make GOFLAGS work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants