Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I work around adding these .dlls when installing NuGet package?

Tags:

c#

.net

nuget

dll

I started off by reading through this suggested question similar to mine, but there was no resolution: Why does MSTest.TestAdapter adds its DLLs into my NuGet package?

Quick Problem Description

I wrote a NuGet package, and each time I install it, NUnit and NUnit3TestAdapter .dll's get added to the project I installed on. I want to find a solution that fixes this issue.

Repro steps

I have pushed two git repositories that reproduce the issue I am describing.

ClientLibrary / MainFramework (project from which I generated NuGet package) - https://github.com/harbourc/client-library-repro-nuget-issue

TargetProject (project that package is to be installed on) - https://github.com/harbourc/target-project-repro-nuget-issue

You can clone both repositories, restore their NuGet packages, and reproduce the issue as follows:

  1. Locate ClientLibrary.1.0.0.nupkg in client-library-repro-nuget-issue/ClientLibrary/

  2. Open package manager console for target-project-repro-nuget-issue and run

Install-Package C:\Path\To\client-library-repro-nuget-issue\ClientLibrary\ClientLibrary.1.0.0.nupkg
  1. Note the NUnit and NUnit3TestAdapter .dll's that are added into TargetProject -- even though TargetProject already has NUnit and NUnit3TestAdapter installed.

Longer Overview

I have created my own NuGet package for internal use, called ClientLibrary, and I am attempting to install it on to another project, called TargetProject. Here is a quick breakdown of the structure:

  • FullSolution.sln
    • MainFramework.csproj
    • ClientLibrary.csproj --> .nupkg generated from this

Separate project:

  • TargetProject.sln
    • TargetProject.csproj --> install .nupkg onto this

ClientLibrary has a reference to MainFramework, and uses many methods from MainFramework.

When installing ClientLibrary.1.0.0.nupkg onto TargetProject, the following .dll's are getting added to TargetProject:

nunit.engine.api.dll
nunit.engine.dll
NUnit3.TestAdapter.dll
NUnit3.TestAdapter.pdb

If I delete these .dll's, everything works fine, because TargetProject already has those packages installed anyway. They are not necessary, it's just annoying to have to delete them when installing.

Here is how I am adding ClientLibrary NuGet package to TargetProject:

  1. Build ClientLibrary and MainFramework projects to generate their .dlls
  2. Change directory into ClientLibrary folder and run nuget spec

.nuspec file is generated:

<?xml version="1.0"?>
<package >
  <metadata>
    <id>ClientLibrary</id>
    <version>1.0</version>
    <title>Client Library</title>
    <authors>Myself</authors>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>Client library for interacting with my application.</description>
    <dependencies>
      <group targetFramework=".NETFramework4.7.2" />
    </dependencies>
  </metadata>
</package>
  1. Run nuget pack -IncludeReferencedProjects -- Because ClientLibrary has a dependency on MainFramework (and several other packages used by MainFramework)

  2. Navigate to TargetProject, open Package Manager Console

  3. Run Install-Package C:\Path\To\ClientLibrary.1.0.0.nupkg

Installation runs successfully, and then those .dll's I am complaining about get added.

Problem:

MainFramework has NUnit and NUnit3TestAdapter NuGet packages installed. ClientLibrary does not. So, the .dll's seem to be added because they are installed on MainFramework, but NOT installed on ClientLibrary. (Remember, ClientLibrary references MainFramework.dll.)

There are other packages installed on both MainFramework and ClientLibrary, and these do not have .dll's that get added to TargetProject upon installation, so I am assuming issue is caused by having packages present in MainFramework but NOT in ClientLibrary.

I believe I can "fix" this issue by installing NUnit and NUnit3TestAdapter onto ClientLibrary, but ClientLibrary doesn't actually use those packages at all, so it seems unnecessary.

How can I install ClientLibrary onto TargetProject without including the NUnit and NUnit3TestAdapter .dll's, and without having to install NUnit and NUnit3TestAdapter onto ClientLibrary? If possible, I would like to tell ClientLibrary.1.0.0.nupkg to use the NUnit and NUnit3TestAdapter packages that are already installed on TargetProject.

If the answer is "Not possible", that is fine, but I would like an explanation -- my overall goal for this question is to gain a better understanding of how NuGet and dependencies work, and understand why this has been an issue in the first place. Thank you for reading.

like image 741
CEH Avatar asked Dec 12 '19 20:12

CEH


1 Answers

In general, it's best practice to keep all of your tests and corresponding NuGet packages in their own project. Then make sure none of the projects reference the test project.

enter image description here

On the don't side, Client Library will include the NUnit dlls because they were added to a project that Client Library references.

Whereas on the do side, Client Library will not include the NUnit dlls because neither reference the test project.

like image 171
sspaniel Avatar answered Nov 07 '22 16:11

sspaniel