Repository navigation
Embed default --version as literal to avoid reflection in NativeAOT - #262
Merged
Merged
Conversation
ShowVersion() read AssemblyInformationalVersion/AssemblyVersion via GetCustomAttribute at runtime, which pulls custom attribute parsing infrastructure into NativeAOT binaries (~719 KiB for a small app, #261). The generator now resolves the version from assembly attributes at compile time and emits it as a const in ConsoleApp.Version.g.cs, using an isolated pipeline so version changes do not invalidate other outputs. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This was referenced Nov 6, 2026
Merged
This was referenced Nov 13, 2026
This was referenced Nov 20, 2026
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.
Fixes #261
What changed
ShowVersion()no longer readsAssemblyInformationalVersionAttribute/AssemblyVersionAttributeviaGetCustomAttributeat runtime. It now just doesLog(Version ?? DefaultVersion).const string DefaultVersioninConsoleApp.Version.g.cs.AssemblyInformationalVersion(with the+SourceRevisionIdsuffix trimmed) →AssemblyVersion→"1.0.0", the same as the previous runtime logic.SymbolDisplay.FormatLiteral.CompilationProvider.Selectpipeline that returns only astring, so a version change regenerates onlyConsoleApp.Version.g.csand leaves the Run/Builder outputs cached.ConsoleApp.Versionstill overrides the default at runtime.Why
Reading custom attributes at runtime keeps the attribute-parsing infrastructure (CoreLib parts, TypeLoader, Reflection.Execution) in NativeAOT binaries. For a small CLI this costs more than the app itself, and setting
ConsoleApp.Versiondidn't help, because the reflection fallback was still compiled in.Measurement
The repro from #261 (net10.0 / win-x64 / Release,
PublishAot):sizoscope-cli 1.1.4 (before.mstat → after.mstat):
The AOT binary still prints the project's
<Version>for--version, and normal command execution is unchanged.Notes for reviewers
ConsoleAppis generated into. These are the same for a normal exe. They differ only in unusual setups, such as callingConsoleApp.Runfrom a library, or test hosts, where the old code returned the testhost version.AssemblyVersion-only projects now show their version.AssemblyVersionAttributeis a pseudo-attribute thatGetCustomAttributedoesn't return at runtime, so the old fallback never ran. It can be read at compile time, so projects that set onlyAssemblyVersionnow print it.Tests
HelpTest.Version/VersionOnBuildercover the cases with no attributes, the informational version with+trimmed,AssemblyVersiononly, escaping, and the builder.IncrementalGeneratorTest.DefaultVersionchecks that the version output is cached across unrelated edits and regenerated when the version changes, and that a version change doesn't invalidate theConsoleApp.Runoutput.🤖 Generated with Claude Code