[Breaking change]: .NET SDK will skip full build for AOT publish
- Dominant language
- No language data
- Stars
- 4.8k
- Forks
- 6.1k
- Avg merge
- 19h 10m
- Merged PRs (30d)
- 268
Description
### Description
The .NET 11 SDK will no longer do a full build during AOT publish but rather only build the minimum needed to produce the AOT-compiled output.
### Version
.NET 11 RC 2
### Previous behavior
When publishing with `PublishAot=true`, the full MSBuild `Build` target executed as part of the publish pipeline, producing a complete self-contained managed deployment (apphost, DLLs, deps.json, runtimeconfig.json, and runtime pack files) in the output (`bin\`) directory. These artifacts are unused by the AOT pipeline, which reads from `@(IntermediateAssembly)` in `obj\`, not from `bin\`.
### New behavior
The .NET SDK runs only the targets needed to compile the intermediate assembly and produce the Native AOT publish output. It no longer runs the full `Build` target or produces the unused managed deployment in `bin\`.
Consequently, build extensibility points that aren't part of compilation don't run during Native AOT publish. These include `BeforeBuild`, `AfterBuild`, `PreBuildEvent`, `PostBuildEvent`, `BeforeTargets="Build"`, `AfterTargets="Build"`, and targets added to `BuildDependsOn`.
The change is scoped to Native AOT publish in .NET 11. Expansion to other publish modes is planned for early .NET 12 previews and is tracked by https://github.com/dotnet/sdk/issues/55911.
### Type of breaking change
- [ ] **Binary incompatible**: Existing binaries might encounter a breaking change in behavior, such as failure to load or execute, and if so, require recompilation.
- [ ] **Source incompatible**: When recompiled using the new SDK or component or to target the new runtime, existing source code might require source changes to compile successfully.
- [x] **Behavioral change**: Existing binaries might behave differently at run time.
### Reason for change
Reduce customer confusion about which binaries to deploy and improve Native AOT publish performance. After this change, the deployable binaries are only in the publish directory.
### Recommended action
Update custom targets to attach to the phase whose outputs they affect:
- For generated source or other compiler inputs, use `BeforeTargets="CoreCompile"`.
- For logic that consumes the compiled assembly, use `AfterTargets="Compile"` and consume `@(IntermediateAssembly)` rather than `$(TargetPath)`.
- For additional files that should be copied to the publish directory, declare them as `Content` items with `CopyToPublishDirectory`, or add them to `@(ResolvedFileToPublish)` before `ComputeResolvedFilesToPublishList`.
- For logic that processes the completed publish output, use `AfterTargets="Publish"`.
For example:
```xml
```
Targets that require the managed deployment previously produced in `bin\` can't use optimized publish. To restore the previous behavior, set `UseOptimizedPublish` to `false` in the project file:
```xml
false
```
Alternatively, set it on the command line:
```console
dotnet publish /p:UseOptimizedPublish=false
```
### Feature area
SDK
### Affected APIs
_No response_
Contributor guide
Assessment
This issue has not been assessed yet.