Skip to content

Enable cached file-based run in Native AOT CLI - #55529

Merged
JeremyKuhne merged 7 commits into
dotnet:mainfrom
JeremyKuhne:aot-run-file-cached-launch
Aug 6, 2026
Merged

JeremyKuhne merged 7 commits into
dotnet:mainfrom
JeremyKuhne:aot-run-file-cached-launch

Conversation

@JeremyKuhne

@JeremyKuhne JeremyKuhne commented Jul 29, 2026 •

Copy link
Copy Markdown
Member

Executive summary

This PR enables the Native AOT-compiled dotnet CLI to run warm file-based applications without hosting the managed CLI when it can prove that an existing launch contract is safe to reuse.

It implements native cached launch for:

  • explicit dotnet run --file app.cs
  • positional dotnet run app.cs when the current directory does not contain a project
  • shorthand dotnet app.cs after external-command resolution confirms no command takes precedence
  • --no-build reuse of complete synthetic CSC output
  • unchanged replayed-CSC and MSBuild-derived RunProperties caches
  • Project and no-build Executable launch profiles

The native path preserves application arguments, environment-variable precedence, working directory, DOTNET_ROOT_*, exit codes, Ctrl-C handling, project-discovery compatibility, and external-command precedence. Unsupported options, stale or ambiguous inputs, incomplete outputs, and uncertain directive content fall back to the managed CLI before launch or other non-idempotent side effects.

Development measurements on Windows x64 Release showed the implemented cached paths at roughly 56-65 ms p50 versus 243-276 ms through managed fallback, reducing repeat-run latency by about 180-210 ms. The reusable benchmark and activity-instrumentation changes are intentionally split from this product PR.

Implementation

  • Extract the file-based cache model and build decision from VirtualProjectBuildingCommand into a shared AOT-safe FileBasedAppRunPlan.
  • Share source-generated JSON metadata and RunProperties between managed and Native AOT paths without bringing the MSBuild object model into the native closure.
  • Add a conservative streaming directive probe: only a proven absence of #: bytes is eligible; unsupported encodings, read failures, concurrent changes, and possible directives defer.
  • Add AotRunCommand for cache analysis, launch-profile application, and terminal process launch.
  • Preserve shorthand external-command precedence by reparsing as run --file only after the AOT resolver set finds no external command.
  • Validate SDK/runtime versions, global properties, source and cache timestamps, implicit inputs, tracked additional sources, recorded build results, and launch outputs before cached reuse.
  • Centralize synthetic apphost, assembly, and runtimeconfig paths and repair missing-artifact behavior in the managed path.
  • Correct platform-native library names in the dn harness and explicitly disable AOT in managed comparison mode.
  • Update the Native AOT design and contributor workflow documentation.

This builds on the Native AOT infrastructure added in #55441.

Validation

  • Full build.cmd -c Release redist build
  • NativeAOT publish of dotnet-aot.Tests for win-x64
  • 19 focused NativeAOT planner, action, parser, and entrypoint tests
  • 2 end-to-end cached-launch tests through the Release product muxer
  • 8 focused managed directive and missing-artifact regression cases
  • git diff --check

Linux and macOS product validation remain required before merge; ambiguous or unsupported shapes continue to use managed fallback in this change.

Executive summary: additional functionality plan

The next increment is direct compilation through Roslyn's BuildClient protocol, preserving compiler-server analyzer and generator behavior without linking the compiler implementation into the native CLI. It will include explicit handling for unavailable servers, cancellation, compiler-version mismatch, and CS0006 handoff to a forced full managed build without duplicate compilation.

After direct compilation, a restricted path (A) can evaluate default virtual projects and recover ComputeRunArguments launch contracts when no build is requested and imported customization is conservatively ruled out. Full in-process Native AOT MSBuild execution (B) will remain behind managed fallback until it passes trimming/AOT warning, custom-task, cross-platform, binary-size, and latency gates; direct invocation of the MSBuild Run target remains out of scope because it changes stream, exit-code, and target-side-effect semantics. A separate follow-up PR will carry the benchmark harness and activity instrumentation, and each expansion will be validated on Windows, Linux, and macOS before broadening eligibility.

Copilot AI review requested due to automatic review settings July 29, 2026 19:21
@JeremyKuhne
JeremyKuhne requested review from a team, lbussell and mthalman as code owners July 29, 2026 19:21
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 2 pipeline(s).
1 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR enables a NativeAOT dotnet fast path for file-based apps by reusing validated cached launch contracts (and synthetic CSC output for --no-build) so warm runs can avoid hosting the managed CLI when safe.

Changes:

  • Introduces shared, AOT-safe planning + cache model (FileBasedAppRunPlan) and a conservative directive-byte probe to decide between native cached launch vs managed fallback.
  • Adds AotRunCommand and wires NativeAOT dispatch to preserve external-command precedence, launch profiles, environment/args semantics, and fallback behavior.
  • Adds/updates comprehensive tests across managed + AOT paths, plus harness/docs updates for the native library naming.

Reviewed changes

Copilot reviewed 25 out of 25 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
test/dotnet.Tests/CommandTests/Run/RunFileTests_CscOnlyAndApi.cs Adds managed regression tests for missing CSC launch artifacts (recompile vs fail under --no-build).
test/dotnet.Tests/CommandTests/Run/RunFileTestBase.cs Switches implicit-build-file collection to the new shared FileBasedAppRunPlan helper.
test/dotnet.Tests/CommandTests/Run/FileBasedAppDirectiveProbeTests.cs New managed tests ensuring the probe’s “None” result is one-way safe vs Roslyn directive discovery.
test/dotnet-aot.Tests/NativeEntryPointTests.cs Updates/extends entrypoint tests for fallback behavior and external-command precedence over shorthand file-based apps.
test/dotnet-aot.Tests/FileBasedAppRunPlanTests.cs New unit tests covering run-plan analysis and cache validation behavior.
test/dotnet-aot.Tests/FileBasedAppDirectiveProbeTests.cs New unit tests for the directive byte probe behavior and edge cases.
test/dotnet-aot.Tests/AotRunCommandTests.cs New unit tests for native run invocation planning, launch-profile application, and env/arg handling.
test/dotnet-aot.Tests/AotParserTests.cs Extends AOT parser tests for shorthand reparse-as-run and run help/fallback shapes.
test/dotnet-aot.Tests/AotIntegrationTests.cs Adds end-to-end integration tests exercising native cached launch and conservative fallbacks via dn.
src/Cli/dotnet/Program.cs Uses TryParseFileBasedAppAsRun() to re-dispatch shorthand file-based invocations to run --file.
src/Cli/dotnet/Parser.cs Wires AotRunCommand into the AOT action configuration for run.
src/Cli/dotnet/Extensions/ParseResultExtensions.cs Adds TryParseFileBasedAppAsRun() helper for consistent shorthand reparse behavior.
src/Cli/dotnet/Commands/Run/VirtualProjectBuildingCommand.cs Refactors build-level/cache logic to use FileBasedAppRunPlan and shared cache serialization.
src/Cli/dotnet/Commands/Run/RunProperties.cs Makes RunProperties JSON-deserializable for shared cache usage and gates MSBuild-only APIs under !CLI_AOT.
src/Cli/dotnet/Commands/Run/RunCommand.cs Improves --no-build behavior for missing synthetic CSC launch artifacts and centralizes apphost path derivation.
src/Cli/dotnet/Commands/Run/FileBasedAppRunPlan.cs New shared cache model + run planning logic used by managed and AOT paths.
src/Cli/dotnet/Commands/Run/FileBasedAppDirectiveProbe.cs New conservative streaming probe for potential #: directive bytes.
src/Cli/dotnet/Commands/Run/CSharpCompilerCommand.cs Centralizes CSC output artifact paths via FileBasedAppRunPlan.
src/Cli/dotnet/Commands/Run/AotRunCommand.cs New NativeAOT run gate/launcher that performs cache analysis + launch profile application + process execution.
src/Cli/dotnet-aot/NativeEntryPoint.cs Enables shorthand file-based run after external resolution fails, using AotRunCommand for eligible shapes.
src/Cli/dotnet-aot/DESIGN.md Documents the new narrow AOT file-based run paths and decision points.
src/Cli/dotnet-aot/AotSourceFiles.props Adds shared run/cache/probe sources to the AOT compilation closure.
src/Cli/dn/run-dn.ps1 Updates harness docs/behavior and native library naming; explicitly sets DOTNET_CLI_ENABLEAOT=false for managed mode.
src/Cli/dn/Program.cs Updates comments and native library name resolution for dotnet-aot on macOS/Linux.
.github/skills/add-dotnet-aot-command/SKILL.md Updates contributor guidance to include the new AOT run fast path and new test/harness knobs.

@JeremyKuhne
JeremyKuhne force-pushed the aot-run-file-cached-launch branch 2 times, most recently from 9a6f6b9 to 3c6bbc9 Compare July 29, 2026 20:35
@JeremyKuhne

Copy link
Copy Markdown
Member Author

CI follow-up for the original failed run (build 1531989):

  • Every completed build leg failed on the same CS0103: VirtualProjectBuilder was unavailable in the clean CLI_AOT compile, at the new product/test call sites. Local incremental outputs had masked this.
  • The branch is now rebased onto the current FileBasedPrograms source-package move, and all AOT call sites use the canonical Microsoft.DotNet.FileBasedPrograms.VirtualProjectBuilder; the temporary duplicate path helper was removed.
  • Clean dotnet-aot and dotnet-aot.Tests builds pass, both product/test NativeAOT publishes pass, the full Release redist build passes, and the focused native/managed/product integration suites pass.
  • The PR update also puts each added/moved type in its own file, adds XML documentation to all PR-added/moved types and non-private members, and addresses both resolved review threads.

Updated commit: 3c6bbc9f8e. The replacement CI run is 1532128.

@marcpopMSFT
marcpopMSFT requested a review from jjonescz July 29, 2026 21:12
@JeremyKuhne
JeremyKuhne force-pushed the aot-run-file-cached-launch branch from 3c6bbc9 to 49e4054 Compare July 29, 2026 21:27
@JeremyKuhne

Copy link
Copy Markdown
Member Author

Linux x64 CI failure follow-up (task log):

  • All Linux/Windows build and AOT compile legs passed. The lone failure was two rows of ValidatedCachedRunPropertiesProduceLaunchInvocation in the Linux NativeAOT test host.
  • The review regression only verifies argument joining, but it traversed mutable process-global SDK-root state, fake runtimeconfig resolution, cache timestamps, and filesystem state. That made the test order/environment dependent on Linux.
  • Argument composition is now extracted into the documented AotRunArguments type and the regression tests the pure function directly under NativeAOT. Existing planner tests and product-muxer integrations continue to cover authoritative cached launch end to end.

Exact Linux validation for commit 49e405418a: published dotnet-aot.Tests for linux-x64 in WSL using native clang/bfd, then ran the complete suite with the CI SDK/host environment. Result: 164 total, 153 passed, 11 expected skips, 0 failed.

@JeremyKuhne
JeremyKuhne force-pushed the aot-run-file-cached-launch branch from 49e4054 to 3427f5b Compare July 29, 2026 23:15
@JeremyKuhne

Copy link
Copy Markdown
Member Author

CI follow-up for build 1532210:

  • Every platform build leg, including Linux/macOS/Windows AOT, passed. The aggregate failure was the same managed test on four Helix jobs: Linux x64, macOS arm64, Windows x64, and Windows x64 Full Framework.
  • CscOnly_AfterMSBuild_CompilationFailure_NoCopyToBin deleted both the intermediate obj DLL and the cached BuildResultFile in bin. This PR intentionally treats a missing recorded build result as requiring full MSBuild, so the test never entered its expected CSC-only path and the skipped-binlog assertion failed.
  • The regression now keeps the authoritative bin output, removes only the intermediate compiler output, triggers a failing replayed CSC compile, and verifies that obj remains absent while the existing bin DLL remains byte-for-byte unchanged.

Focused local result: 1 passed, 0 failed. Updated commit: 3427f5b665. Replacement checks have started.

@jjonescz jjonescz added the Area-run-file Items related to the "dotnet run <file>" effort label Jul 30, 2026
@jjonescz
jjonescz requested a review from a team July 30, 2026 14:03
@JeremyKuhne

Copy link
Copy Markdown
Member Author

Replacement CI follow-up for build 1532389:

  • Every Linux/macOS/Windows build leg, including all NativeAOT legs, passed.
  • Azure recorded 0 failed tests. The only failed work item was unrelated dotnet-new.IntegrationTests.dll.1 on Linux x64.
  • Its streaming TRX recovered 161/161 passed, 0 failed/error/timeout/aborted. The next FeaturesSupport("classlib", true, "netstandard2.1", ...) case blocked while waiting for a child dotnet build; after 48 minutes the hang-dump watchdog killed the host (137), which Helix surfaced as work-item exit 7.
  • This PR changes no dotnet-new or template files, and the prior PR-related run-file regression is no longer failing.

This is an unrelated Helix test hang, so I am requesting a clean rerun without changing commit 3427f5b665.

@JeremyKuhne

Copy link
Copy Markdown
Member Author

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
1 pipeline(s) were filtered out due to trigger conditions.

@JeremyKuhne

Copy link
Copy Markdown
Member Author

Reruns are now attached to the unchanged commit:

Comment thread src/Cli/dotnet-aot/DESIGN.md Resolved
@JeremyKuhne
JeremyKuhne force-pushed the aot-run-file-cached-launch branch from 99a7a19 to fb598a6 Compare August 3, 2026 21:21

@jjonescz jjonescz left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost LGTM, just a couple of nits/questions. Thanks!

@JeremyKuhne

Copy link
Copy Markdown
Member Author

Almost LGTM, just a couple of nits/questions. Thanks!

Appreciate all of your effort. :)

@jjonescz jjonescz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM but it looks like there are some test failures in CI

@JeremyKuhne

Copy link
Copy Markdown
Member Author

CI follow-up for build 1541457:

  • Every platform build leg passed, including Linux, macOS, and Windows NativeAOT.
  • The prior dotnet run - failure is fixed. CI recorded one failure out of 24,955 tests: unrelated macOS arm64 dotnet-watch.Tests test ProjectAndSourceFileChange_AddPackageReference.
  • The test waits for app output Newtonsoft.Json.Linq.JToken, then immediately snapshots process output and expects the asynchronous resolver trace Resolving 'Newtonsoft.Json, Version=13.0.0.0'. The Helix log shows that exact expected trace arriving immediately after the assertion began teardown, so this is the test's known ordering race (Build Analysis reports a 0.33% historical failure rate), not a product failure.
  • This PR changes no dotnet-watch source. All other 203 tests in the shard completed, and the expected dependency was deployed and used successfully.

Requesting a clean rerun on unchanged commit 9dff7d2ee3.

@JeremyKuhne

Copy link
Copy Markdown
Member Author

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
1 pipeline(s) were filtered out due to trigger conditions.

@JeremyKuhne
JeremyKuhne merged commit 13778d5 into dotnet:main Aug 6, 2026
34 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-rc1 milestone Aug 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area-run-file Items related to the "dotnet run <file>" effort

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants