Search code examples
c#.netazure-devopscode-generationcicd

Azure Build Pipeline fails when having a Source-Generator


I created a source generator using the IIncrementalGenerator Interface provided since .NET6. When I run the build locally everything works fine and my files get generated. Even when running a local powershell instance and runningndotnet build on that everything works as it should. However once I put my source-generator in PR that has a build pipeline configured that runs as a PR check it fails. Judging form the error messages like "namespace XY does not exist in ...." and so on it seems like the source generator is not generating those files and therefore causing the errors.

Now I found a fix online that is three years old and it worked for me. The fix was that I added the follwing line of code in my csproj file in which I also reference my code generator.

<PackageReference Include="Microsoft.Net.Compilers.Toolset" Version="4.7.0" PrivateAssets="all">
          <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
        </PackageReference>

However, that fix is not considered clean and even in the nuget description of it, it is mentioned that you should only use this as a "hotfix" and not as a long term solution.

So I need to figure out ho to solve this problem in a robust way so that it can stay like that long term. Since I didn't manage to figure that out on my own, I am posting this question here.

I am expecting a solution that doesn't feel as hacky as the one above and is able to stay in the code base. Additionaly it would be cool to get to know what the problem actually is. Since what I read about it it is some kind of compiler issue, but I didn't manage to figure out the exact reason why that does not work.

I figured I would add one warning I was getting that I think may be related to the issue. (Since I don't want to put the actual project structure and name on here, I am going to type a shortened version of the error by hand in here)

[warning]: The analyzer assembly ../../X.dll references compiler version 4.7.0.0 which is newer than the currently running version 4.4.0.0


Solution

  • After some hours of trying to figure out the problem myself while waiting for a response on here, I actually managed to do so.

    I figured I'd post the answer here so other people can benefit from it if they are facing the same problem as me.

    Now, ultimately it came down to me trying to figure out why a newer compiler version was referenced than the one that was getting used by azure. --> So if you are facing the same problem look for the warning I posted in the question above and if you see that warning then this solution is for you.

    I figured out I was importing the following package:

    <PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.7.0" PrivateAssets="all" />
    

    After changing that to

    <PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.4.0" PrivateAssets="all" />
    

    It all worked.

    Important: I didn't upgrade azures compiler version here but that would also be a solution if you have the rights to do so.