builder,interp: fix -ldflags -X not overriding variables with default values#5344
Closed
abgoyal wants to merge 1 commit intotinygo-org:devfrom
Closed
builder,interp: fix -ldflags -X not overriding variables with default values#5344abgoyal wants to merge 1 commit intotinygo-org:devfrom
abgoyal wants to merge 1 commit intotinygo-org:devfrom
Conversation
… values
Currently, -ldflags "-X pkg.Var=value" fails to override string variables
that have a default value in source code:
var Version = "dev" // -X override is ignored, stays "dev"
var Version string // -X override works correctly
In standard Go, -X works in both cases. This patch brings TinyGo's
behavior in line with Go.
The root cause is the timing of when -X values are applied relative to
the interp pass. The previous approach:
1. Erase global, make it undefined
2. Run interp (executes `Version = "dev"` from init code)
3. Link in -X value via makeGlobalsModule
By step 2, interp had already stored the default value, and the linking
step couldn't reliably override it.
This patch changes the approach:
1. Set -X value as initializer before interp runs
2. Run interp, but skip stores to -X globals
3. Global retains the -X value
This also means dependent compile-time computations work correctly:
var Version = "dev"
var VersionLen = len(Version) // now evaluates with -X value
With this fix, makeGlobalsModule becomes redundant - we no longer need
a separate module to work around type renaming issues since we set the
value directly on the global using its existing type during per-package
compilation.
I've removed makeGlobalsModule as part of this change. If there are edge
cases I haven't considered where keeping it would be safer, happy to
restore it as a fallback.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Currently, -ldflags "-X pkg.Var=value" fails to override string variables that have a default value in source code:
In standard Go, -X works in both cases. This patch brings TinyGo's behavior in line with Go.
The root cause is the timing of when -X values are applied relative to the interp pass. The previous approach:
Version = "dev"from init code)By step 2, interp had already stored the default value, and the linking step couldn't reliably override it.
This patch changes the approach:
This also means dependent compile-time computations work correctly:
With this fix, makeGlobalsModule becomes redundant - we no longer need a separate module to work around type renaming issues since we set the value directly on the global using its existing type during per-package compilation.
I've removed makeGlobalsModule as part of this change. If there are edge cases I haven't considered where keeping it would be safer, happy to restore it as a fallback.