Skip to content

AOT compile gives incorrect error about Roslyn source generator coming from a ProjectReference #37228

Open
@jasonmalinowski

Description

@jasonmalinowski

It appears there's a bug in the SDK's enforcement around project references and AOT compile. If you're doing an AOT compile, all the dependencies are checked for being a valid TFM for AOT, which 100% makes sense. However if a user is consuming a Roslyn source generator from another project (which folks often do), that project is also being checked even though we enforce that Roslyn source generators must be compiled as netstandard2.0. Note the generator in this case is being ran not as a part of deployed app but as a part of the build, so AOT shouldn't be applying in the first place.

I'd say the bug in this case would be that a project reference that's marked as ReferenceOutputAssembly="false" or OutputItemType="Analyzer" shouldn't be getting considered for purposes of AOT. I'm not sure exactly where that condition check should go but I'd expect it to be somewhere.

Discussed in dotnet/roslyn#70900

Originally posted by rudiv November 20, 2023
We have a test project that references our source generator:

<ProjectReference Include="..\..\src\Reaper.SourceGenerator\Reaper.SourceGenerator.csproj" ReferenceOutputAssembly="false" OutputItemType="Analyzer" />

This works fine until we try to compile as AOT, it is trying to compile the source generator too which obviously fails due to the netstandard2.0 target.

1.148 /usr/share/dotnet/sdk/8.0.100/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.FrameworkReferenceResolution.targets(90,5): error NETSDK1207: Ahead-of-time compilation is not supported for the target framework. [/src/src/Reaper.SourceGenerator/Reaper.SourceGenerator.csproj]
------

The Source Generator is marked as a Roslyn Component and works in other scenarios for AOT (ie. referenced from NuGet).

What is the correct way to test this? Our generator is designed to produce code that makes the project AOT friendly, but we would obviously like the ability to test this.

The workaround we're using now is to compile the source generator first, get the nupkg and create a target that references this local package. However that seems cumbersome.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions