Skip to content

Embed default --version as literal to avoid reflection in NativeAOT - #262

Merged
neuecc merged 1 commit into
masterfrom
fix/aot-version-literal
Nov 6, 2026
Merged

neuecc merged 1 commit into
masterfrom
fix/aot-version-literal

Conversation

@neuecc

@neuecc neuecc commented Nov 6, 2026

Copy link
Copy Markdown
Member

Fixes #261

What changed

  • ShowVersion() no longer reads AssemblyInformationalVersionAttribute / AssemblyVersionAttribute via GetCustomAttribute at runtime. It now just does Log(Version ?? DefaultVersion).
  • The generator resolves the version from the compilation's assembly attributes at compile time and emits it as const string DefaultVersion in ConsoleApp.Version.g.cs.
    • Order: AssemblyInformationalVersion (with the +SourceRevisionId suffix trimmed) → AssemblyVersion → "1.0.0", the same as the previous runtime logic.
    • The literal is escaped with SymbolDisplay.FormatLiteral.
    • The version comes from its own CompilationProvider.Select pipeline that returns only a string, so a version change regenerates only ConsoleApp.Version.g.cs and leaves the Run/Builder outputs cached.
  • ConsoleApp.Version still 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.Version didn't help, because the reflection fallback was still compiled in.

Measurement

The repro from #261 (net10.0 / win-x64 / Release, PublishAot):

EXE bytes
Before 1,853,440
After 1,117,184 (−736,256 / −719 KiB)

sizoscope-cli 1.1.4 (before.mstat → after.mstat):

Total accounted size difference: -595.0 kB
  -353.7 kB     System.Private.CoreLib
  -122.0 kB     System.Private.TypeLoader
  -15.4 kB      System.Private.Reflection.Execution
  Removed frozen objects: -15.7 kB

The AOT binary still prints the project's <Version> for --version, and normal command execution is unchanged.

Notes for reviewers

  • Version source changes slightly. It used to be the runtime entry assembly and is now the assembly that ConsoleApp is generated into. These are the same for a normal exe. They differ only in unusual setups, such as calling ConsoleApp.Run from a library, or test hosts, where the old code returned the testhost version.
  • AssemblyVersion-only projects now show their version. AssemblyVersionAttribute is a pseudo-attribute that GetCustomAttribute doesn't return at runtime, so the old fallback never ran. It can be read at compile time, so projects that set only AssemblyVersion now print it.
  • Not covered: tools that stamp the version into the binary after the build. Tools that generate the attributes before compilation (SDK AssemblyInfo, MinVer, Nerdbank.GitVersioning, GitVersion) work as before.

Tests

  • HelpTest.Version / VersionOnBuilder cover the cases with no attributes, the informational version with + trimmed, AssemblyVersion only, escaping, and the builder.
  • IncrementalGeneratorTest.DefaultVersion checks that the version output is cached across unrelated edits and regenerated when the version changes, and that a version change doesn't invalidate the ConsoleApp.Run output.
  • All 132 GeneratorTests pass.

🤖 Generated with Claude Code

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>
@neuecc
neuecc merged commit 6733eea into master Nov 6, 2026
7 checks passed
@neuecc
neuecc deleted the fix/aot-version-literal branch November 6, 2026 08:31
This was referenced Nov 6, 2026
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.

Native AOT: default version lookup adds ~719 KiB to a small executable

1 participant