Roslyn: Unable to use Roslyn v2.3.0 built analyzers in visual Studio 2017 v15.3.5.

Created on 27 Sep 2017  Â·  10Comments  Â·  Source: dotnet/roslyn

Hi,

Version Used:
Microsoft.CodeAnalysis - v2.3.0
Visual Studio 2017 v15.3.5

Steps to Reproduce:

  1. Installed .Net Core 2.0 SDK so that we can work with .Net Standard v2.0 in Visual Studio 2017.
  2. Created a .Net Standard project. It created in new csproj structure with netstandard2.0
  3. I added reference to Microsoft.CodeAnalysis v2.3.0
  4. Created an analyzer and used that analyzer DLL in another project.
  5. I am getting an error IDE1003,
    Error IDE1003 Analyzer assembly 'XXX.dll' depends on 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' but it was not found. Analyzers may not run correctly unless the missing assembly is added as an analyzer reference as well.

I could also see other errors
CS8032 An instance of analyzer XXXAnalyzer cannot be created from XXXAnalyzer.dll : Exception has been thrown by the target of an invocation.

Expected Behavior:
Should be able to run analyzer when build using Visual Studio 2017. Analyzer should run and show violation if any.

Actual Behavior:
Shows above mentioned errors and it shows unable to create analyzer instance.

The same analyzer when I used older csproj format and Roslyn version 1.2.0.0 it worked fine.

I saw few other issues mentioned about Roslyn version incompatibility with Visual Studio 2017.
Whether we can use latest stable Roslyn version v2.3.0 for developing analyzers which will run in Visual Studio 2017 15.3.5? Please let me know Microsoft.CodeAnalysis version we should use for developing analyzers which will be running in Visual Studio 2017 v15.3.5.

Thanks
Basanth

Area-Analyzers

All 10 comments

Try targeting netstandard 1.3 instead of 2.0, or else making sure that you list all of the netstandard 2.0 dependencies as analyzer assemblies.

@jmarolf @jinujoseph @mavasani

Thanks @Pilchie ,

It works, I will target netstandard1.3 for now for my analyzers.

Regards
Basanth

In VS 15.4.0, targeting net47 works, as does targeting netstandard1.4. netstandard1.5 – netstandard2.0 do not work.

Comment from @Pilchie helped. For now decided to continue with netstandard1.3 until Visual Studio start deploying netstandard2.0 binaries.

Anyone knows whether VS2017 15.5.0 supports targeting analyzers to netstandard2.0 and run analyzers on v15.5.0?

@msbasanth until the roslyn nuget packages move forward to target netstandard 2.0 instead of 1.3 I would recommend staying on 1.3. VS2017 15.5.0 supports targeting analyzers to netstandard2.0 but you will need to package up the ~90 facade assemblies for it to load correctly in VS. What Apis are in netstandard2.0 that you need?

Thanks @jmarolf

I need netstandard2.0 mainly for retrieving Assembly.Location.
https://github.com/dotnet/coreclr/issues/1367

Do you know whether roslyn v2.6.0 is targeting netstandard2.0 or not?
Today I tried following cases with VS2017 15.5, most cases are working fine even with netstandard2.0.

  1. I updated all analyzer assemblies to netstandard2.0 and built - success

{
"supports": {},
"dependencies": {
"Microsoft.CodeAnalysis": "2.6.0",
"Microsoft.CodeAnalysis.CSharp.Workspaces": "2.6.0",
"Microsoft.CodeAnalysis.Workspaces.Common": "2.6.0",
"Microsoft.NETCore.Portable.Compatibility": "1.0.1",
"NETStandard.Library": "2.0.1",
"System.Runtime.Loader": "4.3.0"
},
"frameworks": {
"netstandard2.0": {}
}
}

  1. I am able to use the analyzers in projects (added as analyzer references) - Success
  2. Even the VSIX I could run and use the analyzers in VS2017 - Success
  3. Unit tests also updated accordingly as in (1) and built -Success
  4. Unit tests are not running with NUnit Adapter v3.9.0 - Failed
    Error:

Test Name: AbstractClassesPrefix_EmptyInput_NoDiagnosticsShown
Test FullName: Healthineers.CodeAnalysis.Analyzers.FxCop.Test.Naming.AbstractClassesPrefixTests.AbstractClassesPrefix_EmptyInput_NoDiagnosticsShown
Test Source: D:\TFS\SubmissionWS\Healthineers.CodeAnalysis\Test\Healthineers.CodeAnalysis.Analyzers.FxCop.Test\Naming\AbstractClassesPrefixTests.cs : line 24
Test Outcome: Failed
Test Duration: 0:00:00.0000001

Result Message:
OneTimeSetUp: System.TypeInitializationException : The type initializer for 'XXX.Test.Common.AbstractDiagnosticVerifier' threw an exception.
----> System.IO.FileLoadException : Could not load file or assembly 'System.Runtime, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
----> System.IO.FileLoadException : Could not load file or assembly 'System.Runtime, Version=4.0.20.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

It looks like NUnit Adapter that I am using is not supporting netstandard2.0 analyzers. Any other NUnit test runner that you know?

Thanks
Basanth

@msbasanth roslyn v2.6.0 targets netstandard1.3. Your success for netstandard2.0 analyzers will depend entirely on which version of the .NET framework you have installed. Based on your success I would assume you have .NET 4.7.1 installed. Users who have .NET 4.6 would need the facade assemblies deployed.

@Pilchie reminded me that there is another issue that I am forgetting about. If there is a case where you need binding redirects for your analyzer to run there is currently no way to author an analyzer such that the redirects will work on both .NET 4.6 and .NET 4.7.1

Thanks @jmarolf
In our case, our VS2017 code base can target 4.7.1 as we are not yet rolled out analyzers.

Was this page helpful?
0 / 5 - 0 ratings