Search code examples
c#.net-corenugetnuget-packagenuspec

NuGet: explicitly list package dependencies as package references in project so they can be uninstalled


I'm creating a NuGet package (let's call it Lib) that also has analyzers in a separate package (Lib.Analyzers).

By default I want Lib.Analyzers to be installed to the the project when Lib is installed. I can achieve this by listing Lib.Analyzers in the dependencies of Lib (in the .nuspec file).

But I also want users to be able to uninstall Lib.Analyzers again when they don't want them.

AFAIK package dependencies are not listed as individual packages in the package manager and the following wouldn't work in the package manager console:

PM> Install-Package Lib
...
PM> Uninstall-Package Lib.Analyzers
Uninstall-Package : Package 'Lib.Analyzers' to be uninstalled could not be found in project 'ClassLibrary1'

Is there a way to achieve this? I couldn't find anything in the docs.

Rationale

I want to minimize friction for new users, i.e. they should get the help Lib.Analyzers provides without having to remember to install the analyzers every time they install Lib.

But I also want Lib.Analyzers to not be forced onto experienced users that might not want them anymore.

Workaround

My current workaround would be to rename Lib to Lib.Core, remove its dependency to Lib.Analyzers and create a third meta-package named Lib that bundles the two, arriving at the following dependency graph:

Lib
|- Lib.Core
'- Lib.Analyzers

New users would then always install Lib and experienced ones could install only Lib.Core instead.

However, Lib is actually a rather small library so this solution feels a little "over-engineered".


Solution

  • There is a GitHub Issue about optional dependencies here, which is closed.

    So unfortunately it is not supported currently, and does not seems to be in the future.

    rohit21agrawal gave the following close reason:

    Couple of reasons - we are closing bugs that have not been active for over a year, and two, this doesn’t fit in with the plans we have for NuGet in the near future.

    You can open a new issue if you have a strong case for this feature and we would consider it.

    Maybe your issue is a "strong case".

    That being said, your workaround seems pretty good. More a documentation/introduction problem.


    There is also an option to suppress specific analyzers, if that is the root of your question.

    light bulb menu