user1234567
user1234567

Reputation: 4311

Is it impossible to have NuGet-dependencies at .nuspec of a Roslyn analyzer package?

Scenario 1 (Good):

  1. Create new VisualStudio solution by Class-Library (.Net Standard 2.0) template.
  2. Install any nuGet-package to it (e.g. Newtonsoft.Json)
  3. Right click on project, Properties -> Package -> Mark "Generate NuGet package on build"
  4. Build solution
  5. Check produced .nupckg file (by NuGet package Explorer of just by 7-Zip) - you'll find, .nuspec contains section:
<dependencies>
  <group targetFramework=".NETStandard2.0">
    <dependency id="Newtonsoft.Json" version="13.0.1" exclude="Build,Analyzers" />
  </group>
</dependencies>

So, if you install this .nupckg to some project, Newtonsoft.Json will be installed also. Good!


BUT! lets see Scenaro 2 (Bad):

Lets move the similar way:

  1. Create new VisualStudio solution BUT by "Analyzer with Codefix" template. (if you examine the .Package project, you'll see, it's the same as our 1st example - it's Class-library with target framework .Net Standard 2.0)
  2. Install same nuGet-package to it (e.g. Newtonsoft.Json)
  3. Right click on project .Package, Properties -> Package -> You'll see, "Generate NuGet package on build" is already marked.
  4. Build solution
  5. Check produced .nupckg file (it is at .Package project bin/Debug folder) - you'll find, .nuspec DOES NOT contain section <dependencies>. So, when you install this .nupckg to some project, Newtonsoft.Json will NOT be installed to it.

My questions:

PS: I've found a solution, but I don't like it, `cos it depreciating the sense of packages management ('cos we just burn dependency dlls into our package). Is this the best solution?

Upvotes: 0

Views: 621

Answers (1)

Sara Liu - MSFT
Sara Liu - MSFT

Reputation: 6218

The issue is that you have added

<SuppressDependenciesWhenPacking>true</SuppressDependenciesWhenPacking>

under xxx.Package project.

It will prevent all project references and nuget refeferences being packed into nupkg(as nuget dependencies) when you pack the Analyzer.Package project. And the new Newtonsoft.Json nuget package is also be prevented.

In other words, the node will prevent all referenced projects, installed nuget packages. And that is designed by the author. The goal of it from the author is to prevent the project references as nuget dependencies for nupkg. Otherwrise, it will turn out an error.

To fix it, it is easy. Just modify Analyzer.Package.csproj file:

1) change the switch to false.

<SuppressDependenciesWhenPacking>false</SuppressDependenciesWhenPacking>

2)And then selectively block the project references to be packaged. Use <PrivateAssets>All</PrivateAssets> under that.

  <ItemGroup>
    <ProjectReference Include="..\Analyzer2.CodeFixes\Analyzer2.CodeFixes.csproj">
      <PrivateAssets>All</PrivateAssets>
    </ProjectReference>
    <ProjectReference Include="..\Analyzer2\Analyzer2.csproj">
      <PrivateAssets>All</PrivateAssets>
    </ProjectReference>
  </ItemGroup>

3) rebuild your project and then you will see the right behavior.

Upvotes: 2

Related Questions