SourceGenerator Analyzer软件包在Visual Studio 2022中工作,但在2019年不工作

发布于 2025-02-01 00:05:53 字数 3689 浏览 3 评论 0原文

最近,我们一直在测试产品中的源生成器,到目前为止,这还不错。但是,我的一位使用VS 2019的同事遇到了与分析仪一起运行的项目的问题,但似乎在VS 2022上运行良好。

我在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

我的生成器项目看起来像:

<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>

和生成的。运行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>

仅在Analyzers \ dotnet \ cs \下使用我们的分析仪DLL。消费者项目看起来像:

<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>

这一切看起来都正确,并在消费者项目中添加了正确的nuget依赖项,因此我不确定我还能做错什么。这是2019年至2022年之间不同的.NET目标问题吗?

到目前为止,我已经:

  1. 清除Nuget Cache
  2. 已删除了.VS文件夹
  3. 重新启动了与2019年

,但没有运气。任何帮助都将受到赞赏。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心在旅行 2025-02-08 00:05:53

如果您根据发电机中的Roslyn版本的4.1版,这意味着您的最小VS版本将为VS2022 17.1。如果您想支持VS2019,则需要参考一些3.* Roslyn的版本。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文