Skip to content

Use SDK NativeCompile hook for ILC publish integration#125629

Draft
sbomer wants to merge 6 commits intodotnet:mainfrom
sbomer:sdk-nativecompile-hook
Draft

Use SDK NativeCompile hook for ILC publish integration#125629
sbomer wants to merge 6 commits intodotnet:mainfrom
sbomer:sdk-nativecompile-hook

Conversation

@sbomer
Copy link
Member

@sbomer sbomer commented Mar 16, 2026

...instead of BeforeTargets/AfterTargets.

Redefine the SDK's empty NativeCompile placeholder target with the actual NativeAOT compilation pipeline, replacing the fragile BeforeTargets/AfterTargets hooks that previously sequenced ILC into the publish flow.

  • Rename ComputeLinkedFilesToPublish to NativeCompile in Publish.targets, removing AfterTargets="ComputeResolvedFilesToPublishList"
  • Remove BeforeTargets="Publish" from SetupProperties, ComputeIlcCompileInputs, and ImportRuntimeIlcPackageTarget (all reachable via DependsOnTargets chains)
  • Keep empty ComputeLinkedFilesToPublish and CopyNativeBinary for back-compat

…ILC publish integration

Redefine the SDK's empty NativeCompile placeholder target with the actual
NativeAOT compilation pipeline, replacing the fragile BeforeTargets/AfterTargets
hooks that previously sequenced ILC into the publish flow.

- Rename ComputeLinkedFilesToPublish to NativeCompile in Publish.targets,
  removing AfterTargets="ComputeResolvedFilesToPublishList"
- Remove BeforeTargets="Publish" from SetupProperties, ComputeIlcCompileInputs,
  and ImportRuntimeIlcPackageTarget (all reachable via DependsOnTargets chains)
- Keep empty ComputeLinkedFilesToPublish and CopyNativeBinary for back-compat
@dotnet-policy-service
Copy link
Contributor

Tagging subscribers to this area: @agocke, @dotnet/ilc-contrib
See info in area-owners.md if you want to be subscribed.

Copy link
Contributor

Copilot AI left a comment

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 updates NativeAOT publish integration to use the .NET SDK’s NativeCompile hook as the primary publish pipeline entry point, replacing prior sequencing via BeforeTargets/AfterTargets to make ordering more explicit and less fragile.

Changes:

  • Redefines the SDK NativeCompile placeholder target to run the NativeAOT pipeline and update ResolvedFileToPublish.
  • Renames the prior publish hook target (ComputeLinkedFilesToPublish) out of the execution path and keeps it as an empty back-compat target.
  • Removes BeforeTargets="Publish" from several targets, relying on DependsOnTargets chains instead.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.targets Removes BeforeTargets="Publish" from SetupProperties and ComputeIlcCompileInputs, relying on dependency chains to run them when needed.
src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.Publish.targets Implements NativeAOT publish integration by redefining NativeCompile; adds empty ComputeLinkedFilesToPublish for compatibility.
src/coreclr/nativeaot/BuildIntegration/Microsoft.DotNet.ILCompiler.SingleEntry.targets Removes BeforeTargets="Publish" from ImportRuntimeIlcPackageTarget, relying on dependency chains.

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings March 16, 2026 19:06
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Refactors NativeAOT publish integration to use the .NET SDK’s NativeCompile hook (instead of BeforeTargets/AfterTargets-based sequencing) so the ILC + native link pipeline runs at an explicit, stable point in the publish target graph.

Changes:

  • Replaces the previous ComputeLinkedFilesToPublish publish hook with an implementation of NativeCompile that runs NativeAOT compilation and updates ResolvedFileToPublish.
  • Removes BeforeTargets="Publish" from several preparatory targets so they execute via dependency chains rather than fragile publish hooks.
  • Keeps ComputeLinkedFilesToPublish (and existing CopyNativeBinary) as empty/back-compat targets.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.targets Stops forcing SetupProperties/ComputeIlcCompileInputs to run via BeforeTargets="Publish"; relies on existing dependency flow.
src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.Publish.targets Implements NativeCompile as the NativeAOT publish integration point; leaves ComputeLinkedFilesToPublish as an empty compatibility target.
src/coreclr/nativeaot/BuildIntegration/Microsoft.DotNet.ILCompiler.SingleEntry.targets Removes BeforeTargets="Publish" from runtime ILC pack import target to rely on dependency chains.

The test infrastructure directly imported SingleEntry.targets from
Directory.Build.targets, which is evaluated before Microsoft.NET.Sdk.targets.
This caused the SDK's empty NativeCompile placeholder (in Publish.targets)
to overwrite the NativeAOT redefinition (MSBuild last-definition-wins),
resulting in no native compilation during NativeAOT test runs.

Use AfterMicrosoftNETSdkTargets to defer the import to after all SDK
targets, matching the pattern already used for ReadyToRun targets in
tests.singlefile.targets.
Copy link
Member

@agocke agocke left a comment

Choose a reason for hiding this comment

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

Everything looks good except the stuff about AfterMicrosoftNETSdkTargets -- I'm not sure I understand well enough what's going on there to say whether or not it's correct.

@sbomer
Copy link
Member Author

sbomer commented Mar 16, 2026

The import order is:

  1. This repo's test build targets (which used to directly import the SingleEntry.targets, eventually defining the NativeCompile target)
  2. SDK targets that define empty NativeCompile target
  3. NuGet package targets

We needed a way to ensure the repo testing imports SingleEntry.targets after the empty NativeCompile target is defined, like what happens in the nuget flow. Hopefully using AfterMicrosoftNETSdkTargets to delay the import doesn't cause other ordering issues...

… load

Deferring the ILC import via AfterMicrosoftNETSdkTargets also deferred
the PublishTrimmed=true that Microsoft.NETCore.Native.targets sets at
evaluation time. This caused liveILLink.targets to compute
_RequiresLiveILLink=false (since PublishTrimmed was not yet set),
so ILLink targets were never loaded, and _PrepareTrimConfiguration
was missing at build time.

Set PublishTrimmed=true explicitly in the NativeAOT test PropertyGroups
before liveILLink.targets is imported. NativeAOT always enables trimming,
so this is semantically correct.
Copilot AI review requested due to automatic review settings March 16, 2026 23:32
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Switches NativeAOT publish integration to use the SDK’s NativeCompile placeholder hook instead of BeforeTargets/AfterTargets, improving sequencing reliability in the publish pipeline.

Changes:

  • Redefines NativeCompile to run the NativeAOT compilation/linking pipeline and updates publish items accordingly.
  • Removes BeforeTargets="Publish" hooks from several NativeAOT targets, relying on dependency chains instead.
  • Defers importing ILC targets via AfterMicrosoftNETSdkTargets and sets PublishTrimmed earlier to ensure trim configuration targets are available.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
src/tests/Directory.Build.targets Defers ILC target import via AfterMicrosoftNETSdkTargets and sets PublishTrimmed early for NativeAOT test builds.
src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.targets Removes BeforeTargets="Publish" from NativeAOT setup/inputs targets.
src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.Publish.targets Moves publish integration work into NativeCompile, leaving an empty ComputeLinkedFilesToPublish for back-compat.
src/coreclr/nativeaot/BuildIntegration/Microsoft.DotNet.ILCompiler.SingleEntry.targets Removes BeforeTargets="Publish" from runtime pack import target.
eng/testing/tests.singlefile.targets Defers ILC import via AfterMicrosoftNETSdkTargets and sets PublishTrimmed early for single-file NativeAOT tests.

@steveisok
Copy link
Member

@sbomer I believe this should fix the forward flow build failure in dotnet/dotnet#5506, correct?

@sbomer
Copy link
Member Author

sbomer commented Mar 17, 2026

No, that's not the intention (though it might help) - I think that needs a separate fix.

…ILC import

Replace the AfterMicrosoftNETSdkTargets hook with the SDK's ILCompilerTargetsPath
extension point to import ILC targets in the correct order (before ILLink),
fixing the DynamicCodeSupport property evaluation error. Gate
IlcSetupPropertiesDependsOn on _IlcReferencedAsPackage to skip NuGet package
resolution when using live ILC targets.
Copilot AI review requested due to automatic review settings March 18, 2026 18:35
@sbomer sbomer force-pushed the sdk-nativecompile-hook branch from 756e72e to 4a3adf6 Compare March 18, 2026 18:35
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Updates NativeAOT publish integration to use the .NET SDK’s NativeCompile hook (instead of BeforeTargets/AfterTargets), aligning the in-repo ILC pipeline with the SDK’s publish sequencing and making the integration less order-fragile.

Changes:

  • Redefines the SDK placeholder NativeCompile target to run the NativeAOT publish pipeline and keeps ComputeLinkedFilesToPublish as an empty back-compat target.
  • Removes BeforeTargets="Publish" hooks from key ILC setup/inputs targets so ordering is driven by dependency chains + SDK sequencing.
  • Updates test build infrastructure to use the SDK’s ILCompilerTargetsPath import hook (and removes direct imports of Microsoft.DotNet.ILCompiler.SingleEntry.targets).

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/tests/Directory.Build.targets Switches nativeaot test builds to use ILCompilerTargetsPath / PublishAot hook instead of directly importing ILC targets.
src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.targets Removes BeforeTargets="Publish" from SetupProperties / ComputeIlcCompileInputs to rely on dependency sequencing.
src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.Publish.targets Moves publish-time work from ComputeLinkedFilesToPublish to NativeCompile and keeps an empty ComputeLinkedFilesToPublish for back-compat.
src/coreclr/nativeaot/BuildIntegration/Microsoft.DotNet.ILCompiler.SingleEntry.targets Makes IlcSetupPropertiesDependsOn conditional on package-based toolchain usage and removes BeforeTargets="Publish" from runtime pack import.
eng/testing/tests.singlefile.targets Switches single-file test publish (NativeAOT) to use ILCompilerTargetsPath hook instead of directly importing ILC targets.

PublishAot=true (added for SDK import order) enables EnableSingleFileAnalyzer,
which promotes IL3000 (Assembly.Location usage) to an error in tracing tests.
Disable it alongside the existing EnableTrimAnalyzer and EnableAotAnalyzer
suppressions.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

4 participants