New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Analyzers fail on .NET Core projects #1028

Closed
Dresel opened this Issue May 25, 2016 · 39 comments

Comments

Projects
None yet
@Dresel
Copy link

Dresel commented May 25, 2016

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.

@mikes-gh

This comment has been minimized.

Copy link

mikes-gh commented May 26, 2016

+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.

@mavasani

This comment has been minimized.

Copy link
Contributor

mavasani commented May 26, 2016

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?

@mikes-gh

This comment has been minimized.

Copy link

mikes-gh commented May 26, 2016

@mavasani How do I find out my compiler toolset?

@mavasani

This comment has been minimized.

Copy link
Contributor

mavasani commented May 26, 2016

@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?

@mikes-gh

This comment has been minimized.

Copy link

mikes-gh commented May 26, 2016

@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"
}


@mavasani

This comment has been minimized.

Copy link
Contributor

mavasani commented May 26, 2016

@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?
@mavasani

This comment has been minimized.

Copy link
Contributor

mavasani commented May 26, 2016

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

@mikes-gh

This comment has been minimized.

Copy link

mikes-gh commented May 26, 2016

@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).

@mavasani

This comment has been minimized.

Copy link
Contributor

mavasani commented May 26, 2016

@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.

@mikes-gh

This comment has been minimized.

Copy link

mikes-gh commented Jun 1, 2016

@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.

@agocke

This comment has been minimized.

Copy link

agocke commented Jun 3, 2016

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

@agocke

This comment has been minimized.

Copy link

agocke commented Jun 3, 2016

@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 :)

@mikes-gh

This comment has been minimized.

Copy link

mikes-gh commented Jun 3, 2016

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

@agocke

This comment has been minimized.

Copy link

agocke commented Jun 3, 2016

@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.

@agocke

This comment has been minimized.

Copy link

agocke commented Jun 3, 2016

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.

@mikes-gh

This comment has been minimized.

Copy link

mikes-gh commented Jun 3, 2016

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.

@agocke

This comment has been minimized.

Copy link

agocke commented Jun 3, 2016

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.

@mikes-gh

This comment has been minimized.

Copy link

mikes-gh commented Jun 3, 2016

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

@mikes-gh

This comment has been minimized.

Copy link

mikes-gh commented Jun 4, 2016

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

@roji

This comment has been minimized.

Copy link

roji commented Jun 5, 2016

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.

@agocke

This comment has been minimized.

Copy link

agocke commented Jun 6, 2016

@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

@mikes-gh

This comment has been minimized.

Copy link

mikes-gh commented Jun 8, 2016

@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.

@srivatsn

This comment has been minimized.

Copy link
Contributor

srivatsn commented Jun 13, 2016

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?

@mikes-gh

This comment has been minimized.

Copy link

mikes-gh commented Jun 13, 2016

In fact most of the Analysers don't work.

@mikes-gh

This comment has been minimized.

Copy link

mikes-gh commented Jun 16, 2016

@mavasani

This comment has been minimized.

Copy link
Contributor

mavasani commented Jun 21, 2016

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.

@axelheer

This comment has been minimized.

Copy link

axelheer commented Jun 30, 2016

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

@srivatsn

This comment has been minimized.

Copy link
Contributor

srivatsn commented Jul 7, 2016

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.

@srivatsn srivatsn added this to the 1.2 milestone Jul 7, 2016

@srivatsn

This comment has been minimized.

Copy link
Contributor

srivatsn commented Jul 7, 2016

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

@axelheer

This comment has been minimized.

Copy link

axelheer commented Aug 24, 2016

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.

@RehanSaeed

This comment has been minimized.

Copy link

RehanSaeed commented Sep 7, 2016

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.

@srivatsn

This comment has been minimized.

Copy link
Contributor

srivatsn commented Sep 7, 2016

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.

@wpostma

This comment has been minimized.

Copy link

wpostma commented Sep 16, 2016

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?

@RehanSaeed

This comment has been minimized.

Copy link

RehanSaeed commented Sep 16, 2016

@mavasani

This comment has been minimized.

Copy link
Contributor

mavasani commented Nov 23, 2016

Analyzers should work with .NetCore projects in VS2017 RC.

@automagic

This comment has been minimized.

Copy link

automagic commented Jan 4, 2017

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

@mayeager22

This comment has been minimized.

Copy link

mayeager22 commented Jan 18, 2017

@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).

@mayeager22

This comment has been minimized.

Copy link

mayeager22 commented Jan 18, 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)?

@axelheer

This comment has been minimized.

Copy link

axelheer commented Jan 20, 2017

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment