[bug] C# Source generators + SonarQube scanner cause build to fail

We’re using AzureDevOps SonarScanner plugin, v5.7.0 to compile a C# project that contains source generators.

With the SonarScanner plugin activated, the build fails with these errors:

2022-08-23T02:39:00.2739590Z [.....]\_tool\dotnet\sdk\6.0.400\Microsoft.Common.CurrentVersion.targets(2302,5): warning MSB3245: Could not resolve this reference. Could not locate the assembly "ExampleNamespace.ExampleLibrary". Check to make sure the assembly exists on disk. If this reference is required by your code, you may get compilation errors. [[.....]\ExampleNamespace.Proj1.csproj]

2022-08-23T02:39:08.7016898Z CSC : warning CS8032: An instance of analyzer ExampleNamespace.SourceGenerators.GraphQLGenerator.GraphQLGenerator cannot be created from [.....]\GraphQLGenerator\bin\Debug\netstandard2.0\GraphQLGenerator.dll : Could not load file or assembly 'SourceGeneratorsCommon, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.. [[.....]\ExampleNamespace.Proj2.csproj]

2022-08-23T02:39:09.1457245Z CSC : warning CS8032: An instance of analyzer ExampleNamespace.SourceGenerators.ControllerGenerators.ControllerGenerator cannot be created from [.....]\ControllerGenerator\bin\Debug\netstandard2.0\ControllerGenerator.dll : Could not load file or assembly 'SourceGeneratorsCommon, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.. [[.....]\ExampleNamespace.Proj2\ExampleNamespace.Proj2\ExampleNamespace.Proj2\ExampleNamespace.Proj2.csproj]

Our source generators load the following assemblies:

<PackageReference Include="Microsoft.OpenApi" Version="1.2.3" GeneratePathProperty="true" PrivateAssets="all" />
<PackageReference Include="Microsoft.OpenApi.Readers" Version="1.2.3" GeneratePathProperty="true" PrivateAssets="all" />
<PackageReference Include="SharpYaml" Version="1.6.6" GeneratePathProperty="true" PrivateAssets="all" />
<PackageReference Include="GraphQL" Version="4.7.1" GeneratePathProperty="true" PrivateAssets="all" />
<PackageReference Include="GraphQL-Parser" Version="7.2.0" GeneratePathProperty="true" PrivateAssets="all" />

@Bosch-Eli-Black

Could you confirm that a non-analysis command line build works as expected please?

Also, please provide us with diagnostic logs for the MSBuild.

FYI this old post describes a problem with analyzers not being able to find dependencies. In that case the problem was that different versions of the dependent assembly were being used.

@duncanp Sorry, I think this was unrelated to SonarQube :sweat_smile:

Thank you for the link… it pointed me in the right direction :slightly_smiling_face:

Our solution has three projects, with a dependency chain that loos like this: MyProjectGraphQLSourceGeneratorSourceGeneratorCommon.

In MyProject, I was referencing GraphQLSourceGenerator but wasn’t referencing SourceGeneratorCommon:

<ProjectReference Include="..\GraphQLSourceGenerator.csproj" OutputItemType="Analyzer" />

Once I made MyProject reference both GraphQLSourceGenerator and SourceGeneratorCommon, everything worked!

<ProjectReference Include="..\GraphQLSourceGenerator.csproj" OutputItemType="Analyzer" />
<ProjectReference Include="..\SourceGeneratorCommon.csproj" OutputItemType="Analyzer" />

When building from within VS, it appears to be unnecessary for the main project to reference the analyzer’s dependent projects, but when building from the command-line, this is required. I believe this is because VS uses .NET Framework to load dependencies while building from the command line uses .NET core.

Thanks for the help, and sorry for the noise! :slightly_smiling_face:

1 Like

@Bosch-Eli-Black no problem. Thanks for the update.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.