Trimming Error When Publishing Project Using Source Generator
- Dominant language
- C#
- Stars
- 3.2k
- Forks
- 1.3k
- PR merge metrics
- PR metrics pending
Description
## Issue Summary
When publishing a project that uses a source generator with trimming enabled, the build fails with an error suggesting .NET Core 3.0+ is required, despite the project targeting .NET 9.0 and the source generator being marked as non-referenced build-time component.
## Version Used
Visual Studio 17.12.0 / .NET 9.0 / C# 13
## Steps to Reproduce
1. Create a .NET Standard 2.0 source generator project (.NET Standard 2.0 being required for analyzers)
2. Create a .NET 9.0 project that references this source generator, using code that depends on the generated output,
generator referenced in the consuming project like so:
```xml
```
4. Build project normally with generated code
5. Attempt to _publish_ the consuming project, e.g., to "Folder", with trimming enabled by checking "Trim unused code" under "File publish options" in the "Folder" profile settings.
## Expected Behavior
The project should build / publish successfully with trimming enabled. Since the source generator is only used at build time and is marked with `ReferenceOutputAssembly="false"`, it should not be considered during the trimming process.
## Actual Behavior
Build fails with error `NETSDK1124`:
```
C:\Program Files\dotnet\sdk\9.0.100\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.FrameworkReferenceResolution.targets(90,5):
Error NETSDK1124: Trimming assemblies requires .NET Core 3.0 or higher.
```
The error occurs despite:
1. The consuming project targeting .NET 9.0
2. The analyzer being marked as not referenced in the output
3. The analyzer being a build-time-only component
## Workaround Option 1 (Easiest Option)
Manually update publish profile (`.pubxml` file) and remove `` option altogether:
```xml
Release
Any CPU
bin\Release\net9.0\publish\win-x64\
FileSystem
<_TargetId>Folder
net9.0
win-x64
true
true
```
Now manually add `true` to main `.csproj` file that references source generator. Note that `` option defaults to `false` for source generator projects, so trimming will now be skipped for analyzer projects.
## Workaround Option 2
Manually update publish profile (`.pubxml` file) to use a conditional TFM:
```xml
Release
Any CPU
bin\Release\net9.0\publish\win-x64\
FileSystem
<_TargetId>Folder
net9.0
win-x64
true
true
true
```
## Workaround Option 3
The issue can be worked around by conditionally by first building project normally, emitting generated files into a local folder. Folder needs to be excluded for normal builds, but available when publishing trimmed, then also excluding the source generator during trimmed publish. For example:
```xml
true
generated
```
This works because the source generator has already generated the necessary files during the initial normal build phase, then they are available during the trimmed publish even without the generator reference.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.