简体   繁体   中英

CS8032 Warnings when building solution with referenced analyzer projects in VS

The issue

For many weeks it worked fine, I was able to compile and test the projects. A few weeks ago, VS started displaying the following warning when building the solution or any project that is not the analyzers:

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

Important facts

  • All the libraries are using .NET 5 (tried .NET 6 preview as well, same warning).
  • The project are referencing the analyzers using Directory.Build.props:
  <ItemGroup>
    <ProjectReference Include="$(MSBuildThisFileDirectory)\Shared\Analyzers\Platform.Shared.Analyzers\Platform.Shared.Analyzers.csproj"
                      PrivateAssets="all"
                      ReferenceOutputAssembly="false"
                      OutputItemType="Analyzer"/>
  </ItemGroup>

What I tried

  • VS 16.10. - Warning
  • Updating to VS 16.11. - Warning
  • Installing VS 17 Preview (2022) and upgrading to .NET 6. - Warning
  • PowerShell dotnet build and dotnet msbuild . - No warning
  • JetBrains Rider. - No warning
  • Azure DevOps build agent. - No warning

Full warning

I enabled Fusion Log to get the full log. From the "Error List" (one is generated for each analyzer for each project referencing the analyzers project):

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\16.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.

-----

Workaround

As a temporary workaround, I added the following property to the solution 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>

I have seen this issue before and it happens because you use .Net 5.0 for the source generator project. Source generator' projects have to be only .Net standard 2.0 as per the documentation here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM