Need to build Roslyn Analyzer targeting .NET Standard \ Core rather than .NET Portable
Asked Answered
H

2

6

I'm using default Roslyn SDK templates that came with Visual Studio 2017. The projects they create target .NET Framework Portable. I'm assuming Roslyn extensibility projects can target .NET Standard \ Core instead of Portable and I'm looking for templates or a sample of Roslyn Analyzer \ Refactoring project that I could study.

Hat answered 27/8, 2017 at 0:46 Comment(4)
What's your issue here, either start a new project and reference the analyzers, and then call that from your vsix, or modify the CSProj file, and replace the Portable librariesLignify
I've tried that, but it is not as straight-forward. Do you know of an example project I could take a look at, where this was done successfully?Hat
The only project I know that I got successfully working is the one here, which I don't think my company would allow share. but I followed along with this answerLignify
Have you tried to 1) create a VSIX project, 2) do an "add item" and choose Analyzer. You'll have to fix some references manually using nuget packager manager. I kinda remember I build this like this github.com/smourier/EnumCaseGenerator (I don't have an Analyzer, but a CodeRefactoring, but the idea is the same)Raisaraise
H
5

I started working on a new Roslyn project and built things one by one instead of using template. https://github.com/IKoshelev/Roslyn.AutoLogging/commit/1f88e3e49141e0fa425c51fdcb3457a7c3d6dcaa

I managed to have the following targeting:

Refactoring project - .NET Standard 1.3 (this .dll will be distributed, version kept to minimum)

UnitTests project - .NET Core 2.0

VSIX project - .NET Framework 4.6 (I believe, only full Visual Studio supports VSIX, so that is okay)

Update Versioning of Roslyn is a bit more complicated right now, i.e. if you want to use your extensions with Visual Studio 2015 you will have to use PCL libraries. More info at the end of this article article on Roslyn

Hat answered 23/9, 2017 at 22:49 Comment(0)
P
10

Sample of converted analyzer from default analyzer template is available here. There is original analyzer for comparison along with TestAnalyzerStandard which targets .NET standard.

Steps to make it work:

  • Create new .NET Standard library
  • Library must target .NET Standard 1.3. This is required if you wish to run analyzer as extension inside VS (extensions target .NET 4.6). Mapping between standard versions and full framework versions is available here. Also if you try to target lower version than 1.3, you will not be able to include required analyzer packages.
  • Add nuget package for Microsoft.Composition latest version. This is needed by Microsoft.CodeAnalysis.CSharp.Workspaces. If you try to add workspaces first, you will get error that referenced composition package is not compatible.
  • Add nuget package for Microsoft.CodeAnalysis.CSharp (I'm using latest 1.* version)
  • Add nuget package for Microsoft.CodeAnalysis.Csharp.Workspaces (version should match the version of Microsoft.CodeAnalysis.CSharp).
  • At this point you can copy code from portable project and build it. There should be no errors (you may have to close and reopen solution if VS is still displaying red squiggles).
  • To make VS extension work, just open source.extension.vsixmanifest, go to assets tab and change reference to .NET standard library
  • To create .nuget package just execute nuget pack Diagnostic.nuspec .. Diagnostic.nuspec is valid for Nuget 2.x. If you are using nuget via package management console in VS 2017 you will have to change <file src="*.dll" ..." to <file src="bin\*\netstandard1.3\*.dll" ....

Those steps are result of my experimentation with analyzers (I previously played with creating DLL which targeted full framework instead of being portable library). They are not by any means official.

Paymaster answered 30/8, 2017 at 20:57 Comment(4)
I've downloaded the project referenced, removed the portable version of the analyzer leaving only core version and tried to build it. So far, VSIX does not work and nugget does not include dll in package.Hat
I was testing both vsix and nuget packages in Visual Studio 2017 (15.2) and they were both working fine. I removed references from portable analyzer and updated .csproj file to point to Diagnostic.nuspec for packaging. You can now build nuget package directly from Visual Studio by right clicking TestAnalyzerStandard -> Pack. Nuget package will be present in Debug (or Release) folder. I also included built vsix extension and nuget package in TestAnalyzerStandard\Publish folder so you can see what is produced on my machine.Paymaster
I'll have to check later (sorry, very loaded now). I awarded the bounty.Hat
The VSIX containing refactoring targeting .NET standard 1.3 works in Visual Studio 2017, but does not work with 2015. Do you know about any compatibility restrictions regarding VS versions?Hat
H
5

I started working on a new Roslyn project and built things one by one instead of using template. https://github.com/IKoshelev/Roslyn.AutoLogging/commit/1f88e3e49141e0fa425c51fdcb3457a7c3d6dcaa

I managed to have the following targeting:

Refactoring project - .NET Standard 1.3 (this .dll will be distributed, version kept to minimum)

UnitTests project - .NET Core 2.0

VSIX project - .NET Framework 4.6 (I believe, only full Visual Studio supports VSIX, so that is okay)

Update Versioning of Roslyn is a bit more complicated right now, i.e. if you want to use your extensions with Visual Studio 2015 you will have to use PCL libraries. More info at the end of this article article on Roslyn

Hat answered 23/9, 2017 at 22:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.