CS8032 在 VS 中使用引用的分析器项目构建解决方案时出现警告

CS8032 Warnings when building solution with referenced analyzer projects in VS

问题

几个星期以来它运行良好,我能够编译和测试项目。几周前,VS 在构建解决方案或任何非分析器的项目时开始显示以下警告:

Warning CS8032  An instance of analyzer Platform.Shared.Analyzers.Rules.StructuredMessageTemplateAnalyzer cannot be created from D:\Dev\Repos\Azure\Platform\src\Shared\Analyzers\Platform.Shared.Analyzers\bin\Debug\net5.0\Platform.Shared.Analyzers.dll: Exception has been thrown by the target of an invocation

重要事实

  <ItemGroup>
    <ProjectReference Include="$(MSBuildThisFileDirectory)\Shared\Analyzers\Platform.Shared.Analyzers\Platform.Shared.Analyzers.csproj"
                      PrivateAssets="all"
                      ReferenceOutputAssembly="false"
                      OutputItemType="Analyzer"/>
  </ItemGroup>

我试过的

完全警告

我启用了 Fusion Log 以获取完整日志。 来自“错误列表”(为引用分析器项目的每个项目的每个分析器生成一个):

Warning CS8032  An instance of analyzer Platform.Shared.Analyzers.Rules.StructuredMessageTemplateAnalyzer cannot be created from D:\Dev\Repos\Azure\Platform\src\Shared\Analyzers\Platform.Shared.Analyzers\bin\Debug\net5.0\Platform.Shared.Analyzers.dll: Exception has been thrown by the target of an invocation..  Platform.Shared.Core        1   Active  System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.TypeInitializationException: The type initializer for 'Platform.Shared.Analyzers.Rules.StructuredMessageTemplateAnalyzer' threw an exception. ---> System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
   at Platform.Shared.Analyzers.Rules.StructuredMessageTemplateAnalyzer..cctor()
   --- End of inner exception stack trace ---
   at Platform.Shared.Analyzers.Rules.StructuredMessageTemplateAnalyzer..ctor()
   --- End of inner exception stack trace ---
   at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
   at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
   at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
   at System.Activator.CreateInstance(Type type, Boolean nonPublic)
   at System.Activator.CreateInstance(Type type)
   at Microsoft.CodeAnalysis.Diagnostics.AnalyzerFileReference.Extensions`1.GetAnalyzersForTypeNames(Assembly analyzerAssembly, IEnumerable`1 analyzerTypeNames, Boolean& reportedError)
-----
System.TypeInitializationException: The type initializer for 'Platform.Shared.Analyzers.Rules.StructuredMessageTemplateAnalyzer' threw an exception. ---> System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
   at Platform.Shared.Analyzers.Rules.StructuredMessageTemplateAnalyzer..cctor()
   --- End of inner exception stack trace ---
   at Platform.Shared.Analyzers.Rules.StructuredMessageTemplateAnalyzer..ctor()
-----
Could not load file or assembly 'System.Runtime, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
-----
=== Pre-bind state information ===
LOG: DisplayName = System.Runtime, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
 (Fully-specified)
LOG: Appbase = file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/Common7/IDE/
LOG: Initial PrivatePath = NULL
Calling assembly : Platform.Shared.Analyzers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null.
===
LOG: This bind starts in LoadFrom load context.
WRN: Native image will not be probed in LoadFrom context. Native image will only be probed in default load context, like with Assembly.Load().
LOG: Using application configuration file: C:\Users\Shay\AppData\Local\Microsoft\VisualStudio.0_0bee0d9b\devenv.exe.config
LOG: Using host configuration file: 
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/Common7/IDE/System.Runtime.DLL.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/Common7/IDE/System.Runtime/System.Runtime.DLL.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/Common7/IDE/PublicAssemblies/System.Runtime.DLL.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/Common7/IDE/PublicAssemblies/System.Runtime/System.Runtime.DLL.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/Common7/IDE/PrivateAssemblies/System.Runtime.DLL.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/Common7/IDE/PrivateAssemblies/System.Runtime/System.Runtime.DLL.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/Common7/IDE/CommonExtensions/Microsoft/TestWindow/System.Runtime.DLL.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/Common7/IDE/CommonExtensions/Microsoft/TestWindow/System.Runtime/System.Runtime.DLL.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/Common7/IDE/CommonExtensions/Platform/Debugger/System.Runtime.DLL.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/Common7/IDE/CommonExtensions/Platform/Debugger/System.Runtime/System.Runtime.DLL.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/Common7/IDE/PrivateAssemblies/DataCollectors/System.Runtime.DLL.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/Common7/IDE/PrivateAssemblies/DataCollectors/System.Runtime/System.Runtime.DLL.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/Common7/IDE/PrivateAssemblies/DataCollectors/x86/System.Runtime.DLL.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/Common7/IDE/PrivateAssemblies/DataCollectors/x86/System.Runtime/System.Runtime.DLL.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/Common7/IDE/System.Runtime.EXE.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/Common7/IDE/System.Runtime/System.Runtime.EXE.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/Common7/IDE/PublicAssemblies/System.Runtime.EXE.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/Common7/IDE/PublicAssemblies/System.Runtime/System.Runtime.EXE.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/Common7/IDE/PrivateAssemblies/System.Runtime.EXE.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/Common7/IDE/PrivateAssemblies/System.Runtime/System.Runtime.EXE.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/Common7/IDE/CommonExtensions/Microsoft/TestWindow/System.Runtime.EXE.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/Common7/IDE/CommonExtensions/Microsoft/TestWindow/System.Runtime/System.Runtime.EXE.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/Common7/IDE/CommonExtensions/Platform/Debugger/System.Runtime.EXE.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/Common7/IDE/CommonExtensions/Platform/Debugger/System.Runtime/System.Runtime.EXE.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/Common7/IDE/PrivateAssemblies/DataCollectors/System.Runtime.EXE.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/Common7/IDE/PrivateAssemblies/DataCollectors/System.Runtime/System.Runtime.EXE.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/Common7/IDE/PrivateAssemblies/DataCollectors/x86/System.Runtime.EXE.
LOG: Attempting download of new URL file:///C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/Common7/IDE/PrivateAssemblies/DataCollectors/x86/System.Runtime/System.Runtime.EXE.
LOG: Attempting download of new URL file:///C:/Users/Shay/AppData/Local/Temp/VS/AnalyzerAssemblyLoader/2dfa9a6f85874e39aff972af6188cb73/5/System.Runtime.DLL.
LOG: Attempting download of new URL file:///C:/Users/Shay/AppData/Local/Temp/VS/AnalyzerAssemblyLoader/2dfa9a6f85874e39aff972af6188cb73/5/System.Runtime/System.Runtime.DLL.
LOG: Attempting download of new URL file:///C:/Users/Shay/AppData/Local/Temp/VS/AnalyzerAssemblyLoader/2dfa9a6f85874e39aff972af6188cb73/5/System.Runtime.EXE.
LOG: Attempting download of new URL file:///C:/Users/Shay/AppData/Local/Temp/VS/AnalyzerAssemblyLoader/2dfa9a6f85874e39aff972af6188cb73/5/System.Runtime/System.Runtime.EXE.

-----

解决方法

作为临时解决方法,我将以下 属性 添加到解决方案 Directory.Build.props:

  <PropertyGroup Condition="'$(BuildingInsideVisualStudio)' == 'true'">
    <!-- Visual Studio compiler displays the CS8032 warning for unknown reason. Removing it manually to declutter the Error List -->
    <NoWarn>$(NoWarn),8032</NoWarn>
  </PropertyGroup>

我以前见过这个问题,它的发生是因为您将 .Net 5.0 用于源生成器项目。根据文档 here.

,源生成器的项目只能是 .Net 标准 2.0