dotnet / roslyn-analyzers

MIT License
1.6k stars 468 forks source link

Could not load file or assembly 'Microsoft.CodeAnalysis, Version=2.3.0.0,... #1215

Closed cristianmad closed 7 years ago

cristianmad commented 7 years ago

Analyzer package

Microsoft.CodeAnalysis.FxCopAnalyzers Version="2.3.0-beta1"

Analyzer

All the analyzers in the FxCop package.

Repro steps

  1. Create a new standard .NET library project using Visual Studio 2017 (fully updated)
  2. Add the Microsoft.CodeAnalysis.FxCopAnalyzers Version="2.3.0-beta1" set of analyzers
  3. Build the solution/project. - optionally force a code analysis
  4. Observe the Output window's content

Expected behavior

It should indicate the result of the analysis.

Actual behavior

Tons of errors similar to the one shown below for each of the analyzers.

1>CSC : warning CS8032: An instance of analyzer Microsoft.NetCore.CSharp.Analyzers.InteropServices.CSharpMarkBooleanPInvokeArgumentsWithMarshalAsAnalyzer cannot be created from C:\Users\Cristian.nuget\packages\microsoft.netcore.analyzers\2.3.0-beta1\analyzers\dotnet\cs\Microsoft.NetCore.CSharp.Analyzers.dll : Could not load file or assembly 'Microsoft.CodeAnalysis, Version=2.3.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified..

While the error indicates that the excepted analyzer(s) version is 2.3.0.0, the actually packaged and deployed version is 2.3.0.61725

mavasani commented 7 years ago

Error is about incorrect compiler version. 2.3.0-beta1 version of the analyzer needs 2.3.0 compiler. Have you installed Dev15.3 Preview1 bits? Can you confirm the version of your csc.exe?

cristianmad commented 7 years ago

Thank you for the quick reply.

On the system where I was experiencing the issue I do not have VS 15.3 preview 1 installed. The csc.exe version on that system is 4.7.2046.0

Since you mentioned the VS preview, I have tried the same on a system where I do have it installed side by side with the released version. There, the csc.exe version is 4.6.1586.0. On that system, when using VS 2017 I do encounter the issue, but, when using the Preview 1 version of the IDE (same system) I do not.

I guess the new FxCop nuget package only works with the 15.3 release (preview atm,)? If so, would you, by any chance, know when the 15.3 version would be officially released?

Thanks, Cristian

mavasani commented 7 years ago

@cristianmad Thanks for confirming - the behavior you described is by design. You will need to use the 2.0.0-beta2 version of the analyzer packages on VS2017 RTW. @jinujoseph for the 15.3 release date.

cristianmad commented 7 years ago

Sounds good,

Thanks again!

jinujoseph commented 7 years ago

Official release dates for 15.3 has not been announced yet, You can download the preview and watch out for the next releases at link

cristianmad commented 7 years ago

Thank you to both of you.

cristian-gavril commented 7 years ago

I encounter the same issue on VS 2015 Enterprise (14.0.25431.01 Update 3) Warning CS8032 An instance of analyzer Microsoft.CodeQuality.Analyzers.ApiDesignGuidelines.NestedTypesShouldNotBeVisibleAnalyzer cannot be created from C:\Projects\Git\Research\packages\Microsoft.CodeQuality.Analyzers.2.3.0-beta1\analyzers\dotnet\cs\Microsoft.CodeQuality.Analyzers.dll: Could not load file or assembly 'Microsoft.CodeAnalysis, Version=2.3.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.

tahir-hassan commented 7 years ago

I have found a semi-solution for VS 2017:

http://tahirhassan.blogspot.co.uk/2017/06/getting-code-analysis-2017-to-work.html

sharwell commented 7 years ago

@tahir-hassan The team went to great lengths to make sure those files don't end up in the GAC. I have a suspicion that your proposed solution will cause long-term problems but I'll let someone else follow up with details. I have relatively limited knowledge of the specific implications of GAC because I've followed a policy for many years of no-GAC, no-exceptions.

tahir-hassan commented 7 years ago

@sharwell I understand that, but the solution seems to be as simple as 1. ensure that the DLL's end up in the right folder, or 2. GAC them.

I am not sure why others are recommending an earlier version of the Analyzers because it just has to do with DLLs not being there/or not visible.

Best, Tahir

sharwell commented 7 years ago

@tahir-hassan Even if the assemblies are found, the analyzers aren't going to be functional in such a situation. Instead, you'll just end up with the build-time overhead of loading a bunch of extra assemblies that don't end up used for anything.

The ideal solution would be creating the <Analyzer Include="[analyzer assembly]" /> lines in such a way that the build won't even attempt to load the analyzer assemblies (this is much earlier than the failure reported for this issue) if the analyzer requires a newer version of the compiler than is currently executing. I'm not exactly sure how that would be accomplished though (either something in NuGet, something in the project system, or something in the analyzer infrastructure).

tahir-hassan commented 7 years ago

@sharwell The analyzers, at least the VB.NET one seemed to work -- I am working on a legacy VB.Net application, and there are a lot of try...catch blocks where in the try it is closing a connection, and in the catch block is it closing the same connection.

The analyser is reporting this as a problem because the connection could be closed twice. My solution is to move the closing of the connection to a a single place -- in a finally block. Surely the reporting of this problem is the work of the VB.Net Analyzer, and not Visual Studio?

Thanks, Tahir

sharwell commented 7 years ago

@tahir-hassan For any specific analyzer/scenario combination, I would expect one of the following:

  1. The analyzers work, and the GAC is not needed
  2. The analyzers don't work, even if the GAC is used

For example, if CSharpMarkBooleanPInvokeArgumentsWithMarshalAsAnalyzer is reporting a warning about inability to load version 2.3.0.0 (first post in this topic), then placing the assemblies in the GAC will not cause CSharpMarkBooleanPInvokeArgumentsWithMarshalAsAnalyzer to start working.

tahir-hassan commented 7 years ago

@sharwell I am not sure about that. The error above, as I have also experienced the same error, to me reads that Microsoft.NetCore.CSharp.Analyzers.dll, which is already loaded, has a dependency on Microsoft.CodeAnalysis DLL, which cannot be found.

I am sure that the helpful suggestions VS gave me about not closing the same connection twice in VB.Net is the result of the Analyzer -- so it can be loaded once you GAC the right dependencies.

sharwell commented 7 years ago

@tahir-hassan I think we're talking about different analyzers. The VB.Net warning you mention wasn't reported by CSharpMarkBooleanPInvokeArgumentsWithMarshalAsAnalyzer. I'm starting to think the message you saw for the VB.Net analyzer actually wasn't the one reported in this issue, but instead a failure to load Microsoft.CodeAnalysis.Workspaces, which would be a very different issue from the one reported here, and instead be closer to DotNetAnalyzers/StyleCopAnalyzers#1659.

sharwell commented 7 years ago

I filed dotnet/roslyn#20316 to fix the originally-reported issue above.

rikimaru0345 commented 7 years ago

I'm affected by this as well, so what are my options now? Revert to the non preview versions?

sharwell commented 7 years ago

@rikimaru0345 You should reference a version no greater than the minimum version of the build tools you would like to support. For example, StyleCop Analyzers 1.0.0 references Roslyn 1.0.0, which means the version of Roslyn that shipped with Visual Studio 2015 (original release) and newer can use it. StyleCop Analyzers 1.1.0 references Roslyn 1.2.0, which means the version of Roslyn that shipped with Visual Studio 2015 Update 2 and newer can use it.

RLashofRegas commented 4 years ago

For anyone coming across this later you can figure out what version you need to install from the documentation here.