C# source generation - IIncrementalGenerator - not triggered

148 views Asked by At

I have my source generator project file:

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <AnalyzerRoslynVersion>4.4</AnalyzerRoslynVersion>
        <TargetFramework>netstandard2.0</TargetFramework>
        <Nullable>enable</Nullable>
        <EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
        <CompilerGeneratedFilesOutputPath>Generated</CompilerGeneratedFilesOutputPath>
        <EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
        <IsRoslynComponent>true</IsRoslynComponent>
        <LangVersion>latest</LangVersion>
        <ImplicitUsings>enable</ImplicitUsings>
        <EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
    </PropertyGroup>

    <ItemGroup>
        <PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.7.0" PrivateAssets="all" />
        <PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" PrivateAssets="all" />
        <PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.7.0" PrivateAssets="all" />
    </ItemGroup>

    <ItemGroup>
        <None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
    </ItemGroup>

</Project>

In the assembly Domain.Core I have defined the marker attribute.

[AttributeUsage(AttributeTargets.Class)]
public class EventApplyAttribute : Attribute
{
    public string FullyQualifiedAggregateName { get; }

    public EventApplyAttribute(string fullyQualifiedAggregateName)
    {
        FullyQualifiedAggregateName = fullyQualifiedAggregateName;
    }
}

The Domain.Core project references the source generator project.

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <TargetFramework>net7.0</TargetFramework>
    </PropertyGroup>

    <ItemGroup>
        <PackageReference Include="Jaeger" Version="1.0.3" />
        <PackageReference Include="OpenTracing.Contrib.NetCore" Version="0.9.0" />
        <PackageReference Include="Scrutor" Version="4.2.0" />
    </ItemGroup>

    <ItemGroup>
      <ProjectReference Include="..\SourceGenerator.csproj" ReferenceOutputAssembly="false" PackAsAnalyzer="true" />
    </ItemGroup>
</Project>

These projects are packed as nuget packages.

In my code I am using the marker attribute:

[EventApply("SampleAggregate.Sample")]
public class SampleAdded : DomainEvent

If I reference both packages:

<ItemGroup>
    <PackageReference Include="Domain.Core" Version="3.0.43-beta" />
    <PackageReference Include="SourceGenerator" Version="3.0.43-beta" />
</ItemGroup>

it works fine. But I would expect that I only need to reference Domain.Core as Domain.Core references the source generator.

What do I need to change that I don't need to reference the source generator itself?

1

There are 1 answers

0
Dani On BEST ANSWER

I didn't find a possibility to that with project references. But it works find with package references. Here all it needs to create the nuget package.

Source generator csproj:

<Project Sdk="Microsoft.NET.Sdk">
    
    <PropertyGroup>
        <TargetFramework>netstandard2.0</TargetFramework>
        <Nullable>enable</Nullable>
        <ImplicitUsings>enable</ImplicitUsings>
        <EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
        <IncludeBuildOutput>false</IncludeBuildOutput>
        <GeneratePackageOnBuild>True</GeneratePackageOnBuild>
        <EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
        <CompilerGeneratedFilesOutputPath>Generated</CompilerGeneratedFilesOutputPath>
        <IsRoslynComponent>true</IsRoslynComponent>
        <LangVersion>latest</LangVersion>
    </PropertyGroup>
    
    <ItemGroup>
        <PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.7.0" PrivateAssets="all" />
        <PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4">
            <PrivateAssets>all</PrivateAssets>
            <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
        </PackageReference>
        <PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.7.0" PrivateAssets="all" />
    </ItemGroup>
    
    <ItemGroup>
        <None Remove="bin\Debug\netstandard2.0\\SourceGenerator.dll" />
        <None Remove="bin\Release\netstandard2.0\\SourceGenerator.dll" />
    </ItemGroup>
    
    <ItemGroup>
        <None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
    </ItemGroup>

</Project>

Using csproj (nuget package):

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <TargetFramework>net6.0</TargetFramework>
    </PropertyGroup>

    <ItemGroup>
        <PackageReference Include="Jaeger" Version="1.0.3" />
        <PackageReference Include="OpenTracing.Contrib.NetCore" Version="0.9.0" />
        <PackageReference Include="Scrutor" Version="4.2.0" />
    </ItemGroup>

    <ItemGroup>
        <ProjectReference Include="..\SourceGenerator\SourceGenerator.csproj" Pack="false">
            <ReferenceOutputAssembly>false</ReferenceOutputAssembly>
            <OutputItemType>Content</OutputItemType>
            <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
        </ProjectReference>
    </ItemGroup>

    <ItemGroup>
        <!-- Package the generator in the analyzer directory of the nuget package -->
        <None Remove="$(OutputPath)/SourceGenerator.dll" />
        <None Include="$(OutputPath)/SourceGenerator.dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
    </ItemGroup>
    
</Project>