Skip to content
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

Unable to create an Analyzer targeting Roslyn 2.0 #18414

Closed
yaakov-h opened this issue Apr 4, 2017 · 24 comments
Closed

Unable to create an Analyzer targeting Roslyn 2.0 #18414

yaakov-h opened this issue Apr 4, 2017 · 24 comments
Assignees
Milestone

Comments

@yaakov-h
Copy link
Member

yaakov-h commented Apr 4, 2017

Version Used:
Microsoft Visual Studio Enterprise 2017
VisualStudio/15.0.0+26228.10

Steps to Reproduce:

  1. Create a new Analyzer project from the "Analyzer with Code Fix (NuGet + VSIX)" template
  2. Notice that the Analyzer builds against Roslyn 1.0.1
  3. Attempt to upgrade all NuGet package.

Expected Behavior:

  • The Analyzer is now built against Roslyn 2.0

Actual Behavior:

An error message:

Could not install package 'Microsoft.CodeAnalysis.Common 2.0.0'. You are trying to install this package into a project that targets '.NETPortable,Version=v4.5,Profile=Profile7', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author.

It seems like this is because Microsoft.CodeAnalysis.Common is now only built for .NET Standard 1.3. If I manually reference this assembly from my existing analyzer, I get a Code Analysis error:

MSBUILD : error : CA0001 : Could not find type 'System.Runtime.CompilerServices.ConditionalWeakTable`2' in assembly 'Microsoft.CodeAnalysis, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.

@jinujoseph jinujoseph added this to the 15.3 milestone Apr 4, 2017
@jinujoseph jinujoseph assigned mavasani and jmarolf and unassigned mavasani Apr 4, 2017
@kiranbelekar
Copy link

+1
I am trying to do the same thing and I am getting the same error as well.

Could not install package 'Microsoft.CodeAnalysis.Common 2.0.0'. You are trying to install this package into a project that targets '.NETPortable,Version=v4.5,Profile=Profile7', but the package does not contain any assembly references or content files that are compatible with that framework.

I am using Visual Studios 2015 Community Edition for creating new Analyzer project from the "Analyzer with Code Fix (NuGet + VSIX)" template

@bkoelman
Copy link
Contributor

bkoelman commented Apr 5, 2017

Related to #15978 (comment).

@jmarolf
Copy link
Contributor

jmarolf commented Apr 5, 2017

Portable projects cannot reference Netstandard 1.3 libraries. I'll need to create a new set of project templates that target Netstandard 1.3. As an aside, 2.0 has no new apis @yaakov-h what was your reasoning for upgrading?

@brian-reichle
Copy link

@jmarolf, we were under the impression that support for the new C#7 syntax was provided through the Roslyn 2.0 libraries. was this incorrect?

@mavasani
Copy link
Member

mavasani commented Apr 5, 2017

Yes @brian-reichle is correct. I tried to write an analyzer/fixer that uses the Tuple syntax factory methods, and these are only available in 2.0.0 libraries.

@mavasani
Copy link
Member

mavasani commented Apr 5, 2017

@jmarolf Can you post a workaround on #15978 until we have published the new templates?

@yaakov-h
Copy link
Member Author

yaakov-h commented Apr 5, 2017

@mavasani @jmarolf does this imply that both the workaround and the solution are that Analyzers for 2.0/C#7/etc. must now be .NET Standard 1.3 libraries instead of PCL libraries?

@jmarolf
Copy link
Contributor

jmarolf commented Apr 5, 2017

@brian-reichle I was hoping the answer @yaakov-h had was that he wanted to write an analyzer against the new C#7 syntax, but if that was not the case then they could have continued without upgrading.
@mavasani I am not aware of anyway for Portable7 projects to reference netstandard 1.3 nuget packages.

@jmarolf
Copy link
Contributor

jmarolf commented Apr 5, 2017

does this imply that both the workaround and the solution are that Analyzers for 2.0/C#7/etc. must now be .NET Standard 1.3 libraries instead of PCL libraries?

@yaakov-h yes netdstandard has a much larger surface area than PCLs in terms of APIs. You cannot reference a netstandard library from a PCL (though the reverse is possible).

@yaakov-h
Copy link
Member Author

yaakov-h commented Apr 5, 2017

@jmarolf the answer @brian-reichle gave is mine as well, we're working on the same Analyzers. 😄

@jmarolf
Copy link
Contributor

jmarolf commented Apr 5, 2017

@yaakov-h @brian-reichle Ah, well I am sorry that I have no easy fix for you other than creating a new netstandard project. I don't think I'll have time this week to publish those new templates :( but here is an example project file on a netstandard project that should get you started.

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netstandard1.3</TargetFramework>
    <PackageTargetFallback>portable-net45+win8+wp8+wpa81</PackageTargetFallback>
    <IncludeBuildOutput>false</IncludeBuildOutput>
    <GeneratePackageOnBuild>True</GeneratePackageOnBuild>
  </PropertyGroup>

  <PropertyGroup>
    <PackageId>Analyzer3</PackageId>
    <PackageVersion>1.0.0.0</PackageVersion>
    <Authors>jmarolf</Authors>
    <PackageLicenseUrl>http://LICENSE_URL_HERE_OR_DELETE_THIS_LINE</PackageLicenseUrl>
    <PackageProjectUrl>http://PROJECT_URL_HERE_OR_DELETE_THIS_LINE</PackageProjectUrl>
    <PackageIconUrl>http://ICON_URL_HERE_OR_DELETE_THIS_LINE</PackageIconUrl>
    <RepositoryUrl>http://REPOSITORY_URL_HERE_OR_DELETE_THIS_LINE</RepositoryUrl>
    <PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
    <Description>Analyzer3</Description>
    <PackageReleaseNotes>Summary of changes made in this release of the package.</PackageReleaseNotes>
    <Copyright>Copyright</Copyright>
    <PackageTags>Analyzer3, analyzers</PackageTags>
    <NoPackageAnalysis>true</NoPackageAnalysis>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="2.0.0" PrivateAssets="all" />
    <PackageReference Update="NETStandard.Library" PrivateAssets="all" />
  </ItemGroup>

  <ItemGroup>
    <Compile Update="Resources.Designer.cs" DesignTime="True" AutoGen="True" DependentUpon="Resources.resx" />
    <EmbeddedResource Update="Resources.resx" Generator="ResXFileCodeGenerator" LastGenOutput="Resources.Designer.cs" />
  </ItemGroup>

  <ItemGroup>
    <None Update="tools\*.ps1" CopyToOutputDirectory="Always" Pack="true" PackagePath="tools" />
    <None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
  </ItemGroup>
</Project>

@yaakov-h
Copy link
Member Author

yaakov-h commented Apr 6, 2017

It doesn't seem like I can manually install the Microsoft.CodeAnalysis.CSharp.Workspaces package into a .NET Standard 1.3 project...

Restoring packages for C:\Blah\Analyzers.csproj...
Restoring packages for C:\Blah\Analyzers.csproj...
Package Microsoft.Composition 1.0.27 is not compatible with netstandard1.3 (.NETStandard,Version=v1.3). Package Microsoft.Composition 1.0.27 supports: portable-net45+win8+wp8+wpa81 (.NETPortable,Version=v0.0,Profile=Profile259)
One or more packages are incompatible with .NETStandard,Version=v1.3.
Package restore failed. Rolling back package changes for 'Analyzers'.
Time Elapsed: 00:00:01.0730152
========== Finished ==========

@yaakov-h
Copy link
Member Author

yaakov-h commented Apr 6, 2017

I tried to manually add the PackageReference element to the csproj and now I'm getting compiler errors relating to System.Composition not existing.

I'd guess it might be possible to build a 2.0-targeting Analyzer if I reference .NET 4.6, but it appears to be impossible with .NET Standard.

@jmarolf
Copy link
Contributor

jmarolf commented Apr 6, 2017

@yaakov-h you need <PackageTargetFallback>portable-net45+win8+wp8+wpa81</PackageTargetFallback> in your csproj file

@yaakov-h
Copy link
Member Author

yaakov-h commented Apr 6, 2017

@jmarolf aha, I missed that detail. Thanks!

@aKzenT
Copy link

aKzenT commented May 3, 2017

I tried the suggestions, but with the netstandard project format I'm having trouble using an external dependency (JSON.NET). Since PackageReferences are not copied to the bin directory, I cannot include them in the nuget-package. If they are not included in the nuget analyzers folder, VS will complain of course.

Is there a way to force the referenced dll to be placed into the output directory so that they can be included?

@yaakov-h
Copy link
Member Author

yaakov-h commented May 3, 2017

@aKzenT I'm pretty sure you can do that with Copy Local, even with the new SDK.

@aKzenT
Copy link

aKzenT commented May 3, 2017

Correct me if I'm wrong, but I don't think there is a "Copy Local" option when using PackageReference. I don't see any option for it in the VS IDE and I also don't see any option documented in https://docs.microsoft.com/en-us/nuget/consume-packages/package-references-in-project-files

@yaakov-h
Copy link
Member Author

yaakov-h commented May 4, 2017

I think you're right. I take that back then.

@jinujoseph jinujoseph modified the milestones: 15.6, 15.3 May 18, 2017
@LokiMidgard
Copy link

LokiMidgard commented Jun 15, 2017

I had a problem with the sample project file. In my case the nuget file was created before the dll's were created in the output folder. I head to build twice to get my nuget package without missing entry's. I then used IsTool, but then those files are not where they should be :/

@leftler
Copy link

leftler commented Aug 9, 2017

It has been a few months, @jmarolf, is there a issue tracking the progress of updating the templates that come in the .NET Compiler Platform SDK that I can follow? I just downloaded the SDK and the template in Visual Studio 2017 Update 2 still generates a 1.0.1 Roslyn project using a PCL library.

@leftler
Copy link

leftler commented Aug 11, 2017

Here is a similar issue about the templates, linking it to join them together #18954

@bkoelman
Copy link
Contributor

@jmarolf

<None Update="tools\*.ps1" CopyToOutputDirectory="Always" Pack="true" PackagePath="tools" />

should be:

<None Update="tools\*.ps1" CopyToOutputDirectory="Always" Pack="true" PackagePath="" />

to prevent creating tools\tools\install.ps1 inside the nupkg. See https://github.com/NuGet/Home/wiki/Adding-nuget-pack-as-a-msbuild-target for details.

@jmarolf
Copy link
Contributor

jmarolf commented Dec 15, 2017

Issue moved to dotnet/roslyn-sdk #73 via ZenHub

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

No branches or pull requests

10 participants