Search code examples
c#azure-devopssourcegenerators

c# Source generator not working in pipeline


I have created a source generator that works perfectly on my local machine; however, when it's being built by our CI-CD pipeline (Azure devops - DotNetCoreCLI@2), it does not seem to work.

Here is the source generator's csproj file

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

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
  </PropertyGroup>

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

</Project>

And this is the project where it's being used

<PropertyGroup>
  <TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
  <ProjectReference Include="{project_path}.SourceGenerator.csproj"
                    OutputItemType="Analyzer"
                    ReferenceOutputAssembly="false" />
</ItemGroup>

We have a class with two variables set to null by default. The generated partial sets these variables in the constructer of that partial.

When running locally, I see the generated class under the analyzers, and the system works as expected. However, the released system does not work. We're getting an error that these variables are null.

Also, there is also a build warning that this variable is never assigned, we didn't get this warning locally, I believe because of the generated class that does actually assign it.

This is the part of the source generator.

public partial class TaxFilePropertyMapping
{{
    public TaxFilePropertyMapping()
    {{
        _usedProperties = new Dictionary<string, List<string>>
        {{
            {dictionaryEntries}
        }};

        _usedWageProperties = new Dictionary<string, List<string>>
        {{
            {wageDictionaryEntries}
        }};
    }}
}}

Any suggestions on why it's not working?


Solution

  • So, after spending hours figuring out the issue, I noticed the following warning

    The analyzer assembly references version '4.11.0.0' of the compiler, which is newer than the currently running version '4.8.0.0'

    Since this is only a warning, I did not think this would prevent the generator from being generated. However, with no other solution, I tried to downgrade the version, and it turns out that this was actually the problem. Now, it is being generated.