Roslyn-analyzers: Analyzers fail on .NET Core projects

Created on 25 May 2016  路  39Comments  路  Source: dotnet/roslyn-analyzers

Analyzer package

Text.Analyzers

Analyzer

CSharpIdentifiersShouldBeSpelledCorrectlyAnalyzer

Repro steps

  1. Create a new .NET Core class library project
  2. Add "Text.Analyzers": "1.2.0-beta2" into dependencies in project.json
  3. Build

    Expected behavior

Successful build.

Actual behavior

Compilation error:

c:\users\christopher\documents\visual studio 2015\Projects\ClassLibrary\src\ClassLibrary\warning CS8032:
An instance of analyzer Text.Analyzers.CSharpIdentifiersShouldBeSpelledCorrectlyAnalyzer cannot be created from
C:\Users\Christopher\.nuget\packages\Text.Analyzers\1.2.0-beta2\analyzers\dotnet\cs\Text.CSharp.Analyzers.dll: 
Could not load file or assembly 'Text.Analyzers, Version=1.2.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'
or one of its dependencies. The system cannot find the file specified.

The Text.Analyzers is just one example, other analyzers may fail too.

See also this issue in the dotnet/cli repository.

Area-Infrastructure Feature Request

Most helpful comment

@mavasani what does that mean? And what about .NetCore projects built without VS2017 RC (e.g. on linux).

All 39 comments

+1 I also have this issue.
If I add
"Microsoft.ApiDesignGuidelines.Analyzers": "1.2.0-beta2"
I don't have build errors but it doesn't work ie no compiler messages and it prevents StyleCop.Analysers from running.
I also tried adding analyserOptions to my project json.but that didn't seem to affect the issue.

The above error indicates that the compiler toolset on your machine is lower version than 1.2.0 or has missing binding redirect. Can you check the version of compiler toolset?

@mavasani How do I find out my compiler toolset?

@mikes-gh - I was referring to the initial comment from @Dresel. If you are seeing analyzer diagnostics from intellisense but not from build then the compiler toolset at %PROGRAM_FILES%\MSBuild\14.0\Bin might be out of date. You can check the file version of vbcscompiler.exe/csc.exe/vbc.exe at that location. Also verify the binding redirects in *.exe.config files in that folder.

@mikes-gh - Your comment seems to indicate you are not getting intellisense diagnostics as well. Is that correct?

@mavasani I am not getting intellisense to work and I get the same error as @Dresel for Text.Analysers

The Other Analyser I tried was
"Microsoft.ApiDesignGuidelines.Analyzers": "1.2.0-beta2"
Even though there were no build errors it doesn't work (no warnings) and it actually causes the StyleCop.Analyzers (that are working by themselves) to stop working.

The only thing that works is the package below by itself. But as I say that's not one from this repo.

"StyleCop.Analyzers": {
"version": "1.0.0",
"type": "build"
}


@mikes-gh Few questions:

  1. Do you have VS2015 Update2 installed? That is required for 1.2.0-beta2 analyzers.
  2. Did you add <Features>IOperation</Features> property to your msbuild project file? 1.2.0-beta2 analyzers include IOperation based analyzers which use an unshipped API from Microsoft.CodeAnalysis and hence you need to add the above feature flag to your project file.
  3. What does build output window show?

Tagging @agocke as he is the expert on .NET core side of things and added analyzer support for .NET core projects.

@mavasani

1.Do you have VS2015 Update2 installed? That is required for 1.2.0-beta2 analyzers.

Yes I have update 2

2.Did you add IOperation property to your msbuild project file? 1.2.0-beta2 analyzers include IOperation based analyzers which use an unshipped API from Microsoft.CodeAnalysis and hence you need to add the above feature flag to your project file.

Its xproj in dotnet core no csproj/msbuild

3.What does build output window show?

Nothing for some analyzers. As if it isn't running.
For Text.Analysers see @Dresel example. Mine is the same except for some minor user path differences

its dotnet build BTW in netcore (but apparently that is changing to msbuild xplat for RTM).

@mikes-gh Thanks for the info - sorry I did not read the title before my responses. I'll sync up with @agocke to get more info here.

@mavasani any update on this. Seems like something that will need to be working for aspnetcore RTM.
At the moment I have no working code analysis in my vs15 enterprise subscription for netcore projects without paying for third party tools.

@mikes-gh Sorry, I didn't see this as I was out on vacation. Lemme take a look.

@mikes-gh Looks like the analyzer requires the Roslyn Workspaces layer and MEF (System.Composition). Neither of those are available in .NET Core. Technically, they're not available to analyzers on desktop either but they probably just happen to be there and you're getting lucky :)

@agocke Im not using desktop only targeting core. Whats the plan for analysers on .NET core.
Can they be made core friendly?

@mikes-gh Yup, they should be by default -- analyzers should only reference Microsoft.CodeAnalysis.CSharp or Microsoft.CodeAnalysis.VisualBasic and other things in the NetStandard.Library.

If you use target framework Portable, .net4.5+Windows8 and only reference those two CodeAnalysis NuGet packages, that should guarantee that your code will work on both desktop and .NET Core.

My target framework is netcoreapp1.0 should I add imports?
I am still not clear on all this.
Could you post a sample project.json that would clear it all up.
I know there are others with similar confusion.

Sure, I'll try to publish a doc on that.

However, the question is, is your _target_ project (the one you're installing the analyzer into) netcoreapp1.0, or is the _analyzer project_ (the project that build the Text.Analyzers NuGet package) netcoreapp1.0?

Your target project is fine -- nothing wrong there. The problem is with the analyzer itself -- it referenced the Microsoft.CodeAnalysis.Workspaces.* NuGet package. That's not available on CoreCLR.

I'm sorry I still don't understand. I looked at Microsoft.ApiDesignGuidelines.Analyzers
The project.json is

{
  "dependencies": {
  },
  "frameworks": {
    ".NETPortable, Version=v4.5, Profile=Profile7": { }
  }
}

I don't see any reference to Microsoft.CodeAnalysis.Workspaces

After all this discussion I am still not clear. Are analysers supposed to work with dotnet core. I see the analysers all have a core directory in their source.
If they are supposed to work could someone post the smallest working sample core project with working analysers to a repo. It would only take 5 mins TIA

I have the same issue. My project (Npgsql) has a dual build system - both csproj and xproj/project.json. When building with csproj the analyzers properly emits warnings, when building with dotnet cli there's nothing.

@mikes-gh cli has a test project that uses the System.Runtime.Analyzers project as a test case: https://github.com/dotnet/cli/tree/rel/1.0.0/TestAssets/TestProjects/TestLibraryWithAnalyzer

Looking at Microsoft.ApiDesignGuidelines.Analyzers, it specifies dependencies via csproj, so you would have to look at the P2P references as well. One list of references: https://github.com/dotnet/roslyn-analyzers/blob/master/src/Dependencies/CodeAnalysis/project.json

@agocke I can get the System.Runtime.Analyzers to work by simply adding a reference.
re Microsoft.ApiDesignGuidelines.Analyzers I don't have a clue where to start.
I don't really understand the link in the above post.
Sorry for my lack of understanding.

I'll try to summarize this thread - Analyzers are supposed to work with .NET Core projects. The CLI tooling passes analyzers on to the compiler and it looks like System.Runtime.Analyzers works for @mikes-gh.

Given that, its interesting that Microsoft.ApiDesignGuidelines is causing problems since their authoring is pretty much the same. @mavasani or @agocke - can one of you try reproing this with RC2?

In fact most of the Analysers don't work.

Sorry for the delay in posting an update here. I was able to repro the issues you mentioned with help from @mattscheffer and following is the investigation summary:

  1. Text.Analyzers is a bogus analyzer package that doesn't contain any implemented rules - it was mistakenly published, so you should avoid trying to use it.
  2. Rest of the FxCop analyzer packages, such as Microsoft.ApiDesignGuidelines.Analyzers should work, however they are not working with the latest released bits of Microsoft.CodeAnalysis. The issue seems to specific to .NET core for analyzer assemblies which have dependencies. StyleCop analyzer assemblies are self contained and work fine, so do System.Runtime.Analyzers 1.0.0 that @agocke pointed out. All of the FxCop analyzer assemblies in 1.2.0-beta2 depend on AnalyzerUtilities.dll, and don't seem to be working. There are couple of recent bug fixes that should fix this issue:

    1. @tmat recently checked in a bug fix into Roslyn's analyzer assembly loader

    2. There was a bug in .NET core assembly loader that was uncovered by the above change, and a fix went in for that as well.

Above 2 changes should fix this issue. @tmat and I will verify the same and post an update here.

Any ETA for a working Microsoft.CodeAnalysis.FxCopAnalyzers .NET Core release?

Since the fixes that are needed to get the analyzer working are in the CLR and the compiler, they will only show up in .NET Core 1.1 which according to the roadmap here is in the fall of this year.

Keeping this issue active to track testing the analyzer with .NETCore 1.1

FYI, as a workaround it's possible to run the "old analyzers" (namely _FxCop_) using the project.json's _postcompile_ event.

See my post on stack overflow.

Is there support for .ruleset files? If so, how are these set using project.json? Thus far, I've been using the buildOptions.nowarn array to turn off rules.

No I don't think there's support for ruleset files in project.json. With the .NET Core tooling moving to csproj, this ability will be back.

I have read on Stackoverflow posts that there is a workaround that some people are using to get StyleCop.Analyzers to look at .net core target dlls, whether it's in the IDE or on commandline I'm not clear on. This issue would be a good place to link to any workarounds. Anyone got that?

Analyzers should work with .NetCore projects in VS2017 RC.

@mavasani what does that mean? And what about .NetCore projects built without VS2017 RC (e.g. on linux).

@mavasani It should also be noted that .NET Core projects must be migrated to 1.1 in order to be opened in VS2017 RC (not as straight forward as just opening the project in VS 2017).

@axelheer I am trying to implement your workaround to use the "old" FxCop but my compile fails. Would mind sharing the frameworks section of your project.json (or is what you have in your stackoverflow the entire section)?

@mayeager22 the posted section should be sufficient. Since some update or the like (I have no idea actually) I need to rebuild twice to get the analysis results instead of some "introspection engine errors".

Maybe that's it or you post more detailed error messages.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

paulomorgado picture paulomorgado  路  3Comments

paulomorgado picture paulomorgado  路  3Comments

onyxmaster picture onyxmaster  路  3Comments

fschlaef picture fschlaef  路  4Comments

paulomorgado picture paulomorgado  路  3Comments