We use some source generators (local projects added as analyzer project references to the consuming projects) and our Azure DevOps CI pipeline (YAML) builds started failing because the source generators started throwing a TypeInitializationException exception.
There were no code changes to the source generator between successful and failing builds and if the SonarCloud tasks are removed from the pipeline the build works again, the solution also builds locally for all of the devs.
From the logs of the SonarCloudPrepare@1 task I can see that C# analyzer was updated between successful and failing builds from version 8.24.0.32949 to 8.25.0.33663.
I have also tried both Windows and Ubuntu agents and have the same results, with SonarCloud the build fails without and it succeeds.
This has the same behaviour where the build succeeds locally and in DevOps without SonarCloud but fails in DevOps with SonarCloud. Although with this repro the exception is different as it throws a FileNotFoundException with the message Could not load file or assembly 'Newtonsoft.Json...'
Both UseDotnet CLIs that you have cannot be put one after the other. I would suggest to let only the UseDotnet version 2.1, then do the SonarCloudPrepare, then if you want move back to .NET 5 for restore and build.
Please note also that you will need the SonarCloudAnalyze task after the build.
Thanks for taking a look at this. Iāve tried your suggestion unfortunately moving the UseDotNet task had no effect. Also the same issue occurs on a Windows agent with only 1 UseDotNet task as the first one is for the runtime only which is not needed on a Windows agent.
Also yes, I am aware that an analysis task is needed but I left it out for the repro because the pipeline never gets that far.
My colleague is now experiencing the same issue on a local install of SonarQube, sonarqube-8.9.1.44547 works and sonarqube-9.0.0.45539 fails.
We have also notice that this PR added a reference to Newtonsoft.Json to the analyzer:
Could it be possible that this is causing the issue? We have tried downgrading the version of Newtonsoft.Json we reference to match that used in the analyzer but the build still fails.
If I change the generator to use the same version of NewtonSoft then build with MSBuild, the build works locally.
If I build with dotnet, it fails with a FileNotFoundException for System.Security.Permissions, Version=0.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51. I havenāt worked out why.
This is just a guess but could this issue be because the Newtonsoft.Json reference in SonarAnalyzer.CSharp.csproj needs the PrivateAssets="all" attribute?
I do think this should be raised as a bug in the relevant Sonar Source repository though as it should not be necessary to make these kinds of changes in order to fix a previously working build especially when added to the fact that the standard dotnet cli task fails.
Thanks a lot for the details and for creating the reproducer project. Indeed, the problem seems to be related to our dependency on Newtonsoft.Json.dll.
Iāve created an issue on our side to take care of that.