Home > Software design >  SourceGenerator analyzer package works in Visual Studio 2022 but not in 2019
SourceGenerator analyzer package works in Visual Studio 2022 but not in 2019

Time:05-27

We've recently been testing Source generators in our product and it's been pretty good so far. However, one of my colleagues, who uses VS 2019, has issues getting the project to run with the analyzer, but it seems to run fine on VS 2022.

I receive this warning message on 2019:

Warning CS8032  An instance of analyzer 
...Generator cannot be created from analyzer\...\analyzers\dotnet\cs\Analyzer.dll:
Could not load file or assembly 
'Microsoft.CodeAnalysis, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' 
or one of its dependencies.
The system cannot find the file specified

My generator project looks like:

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

    <PropertyGroup>
        <TargetFramework>netstandard2.0</TargetFramework>
        <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
        <Authors>...</Authors>
        <Description>...</Description>
        <Copyright>$(Authors)</Copyright>
        <AssemblyVersion></AssemblyVersion>
        <FileVersion></FileVersion>
        <Deterministic>false</Deterministic>
        <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
        <AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
        <AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
        <Version />
        <PackageTags>Analyzer</PackageTags>
        <IncludeBuildOutput>false</IncludeBuildOutput>
    </PropertyGroup>

    <ItemGroup>
      <None Remove="config.xml" />
    </ItemGroup>

    <ItemGroup>
        <Content Include="config.xml" CopyToOutputDirectory="Always" Pack="false"/>
    </ItemGroup>

    <ItemGroup>
        <None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
    </ItemGroup>
    
    <ItemGroup>
        <PackageReference Include="Microsoft.CodeAnalysis.Common" Version="4.1.0" />
        <PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.1.0" />
    </ItemGroup>

</Project>

and the generated nuspec after running dotnet pack:

<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd">
  <metadata>
    <id>...Analyzer</id>
    <version>...</version>
    <authors>...</authors>
    <description>...</description>
    <copyright>...</copyright>
    <tags>Analyzer</tags>
    <dependencies>
      <group targetFramework=".NETStandard2.0">
        <dependency id="Microsoft.CodeAnalysis.CSharp" version="4.1.0" exclude="Build,Analyzers" />
        <dependency id="Microsoft.CodeAnalysis.Common" version="4.1.0" exclude="Build,Analyzers" />
      </group>
    </dependencies>
  </metadata>
</package>

with only our analyzer dll under analyzers\dotnet\cs\. The consumer project looks like:

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

    <PropertyGroup>
        <OutputType>WinExe</OutputType>
        <TargetFramework>net472</TargetFramework>
        <UseWPF>true</UseWPF>
        <UseWindowsForms>true</UseWindowsForms>
        <deterministic>false</deterministic>
        <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
    </PropertyGroup>

This all looks correct and adds the correct nuget dependencies in the consumer project, so I'm not sure what else I can be doing wrong. Is this a .NET target issue that is different between 2019 and 2022?

So far I've:

  1. Cleared the nuget cache
  2. deleted the .vs folder
  3. restarted VS 2019

And no luck. Any help is appreciated.

CodePudding user response:

If you're depending on version 4.1 of Roslyn in your generator, that means your minimum VS version is going to be VS2022 17.1. If you want to support VS2019, you need to reference some 3.* version of Roslyn.

  • Related