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



问题

好几个星期,它工作得很好,我能够编译和测试项目。几周前,当构建解决方案或任何不是分析器的项目时,VS开始显示以下警告:

Warning CS8032  An instance of analyzer Platform.Shared.Analyzers.Rules.StructuredMessageTemplateAnalyzer cannot be created from D:DevReposAzurePlatformsrcSharedAnalyzersPlatform.Shared.AnalyzersbinDebugnet5.0Platform.Shared.Analyzers.dll: Exception has been thrown by the target of an invocation

重要事实
  • 所有的库都在使用。net 5(也尝试过。net 6预览版,同样的警告)。
  • 项目使用Directory.Build.props:
  • 引用分析器。
<ItemGroup>
<ProjectReference Include="$(MSBuildThisFileDirectory)SharedAnalyzersPlatform.Shared.AnalyzersPlatform.Shared.Analyzers.csproj"
PrivateAssets="all"
ReferenceOutputAssembly="false"
OutputItemType="Analyzer"/>
</ItemGroup>

What I tried

  • 16.10 VS。警告——
  • 更新到VS 16.11。警告——
  • 安装VS 17预览版(2022)并升级到。net 6。警告——
  • PowerShelldotnet builddotnet msbuild。-无警告
  • JetBrains骑手。-无警告
  • Azure DevOps构建代理。-无警告

充分警告我启用了Fusion Log来获取完整的日志。从"错误列表"中(为引用分析器项目的每个项目的每个分析器生成一个分析器):

Warning CS8032  An instance of analyzer Platform.Shared.Analyzers.Rules.StructuredMessageTemplateAnalyzer cannot be created from D:DevReposAzurePlatformsrcSharedAnalyzersPlatform.Shared.AnalyzersbinDebugnet5.0Platform.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:UsersShayAppDataLocalMicrosoftVisualStudio16.0_0bee0d9bdevenv.exe.config
LOG: Using host configuration file: 
LOG: Using machine configuration file from C:WindowsMicrosoft.NETFrameworkv4.0.30319configmachine.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作为源生成器项目。源生成器的项目必须只有。Net standard 2.0根据这里的文档

最新更新