Karle
Karle

Reputation: 888

NuGet: Don't include reference from package

Summary

When I package a library using NuGet and reference it in another project the referring project will pull additonal files in to the build directory.

Working Case


Project: ReferenceLibrary
  Output: ReferenceLibrary.dll

Project: DerivedLibrary
  Output: DerivedLibrary.dll
  References: ReferenceLibrary (Copy Local = False)

Project: ConsoleApplication
  Output: ConsoleApplication.exe
  References: DerivedLibrary

Edit: The reference library is not copied because it is resolved at runtime. There's several versions depending on the target. The reference in derive proj. is so I can code against it.

If I build this then only DerivedLibrary.dll is copied to the ConsoleApplication build folder (i.e. bin/Release).

Non-working Case

Project: ConsoleApplication
  Output: ConsoleApplication.exe
  Package: DerivedLibrary.nupkg (depends on ReferenceLibrary.nupkg)

A project reference is added to DerivedLibray.dll. Both DerivedLibrary.dll and the ReferenceLibrary.dll are copied from their packages.

I can see it being copied in the MSBUILD log.

_CopyFilesMarkedCopyLocal:
    Copying file from "c:\...\ReferenceLibrary.dll" to "bin\Debug\ReferenceLibrary.dll"

Even though it's not referenced in the .csproj anywhere.

I can't tell if this is a NuGet problem (due to how it unpacks things) or a Visual Studio project (how it copies referenced assemblies and encodes the requirements in other assemblies).

Upvotes: 2

Views: 2365

Answers (1)

Karle
Karle

Reputation: 888

A possible solution I've found is to use a post build target to delete the offending references.

In the derived library add a DerivedLibrary.targets file.

<?xml version="1.0" encoding="utf-8" ?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <Target Name="RemoveUnwantedReferences" AfterTargets="Build">
      <Message Text="Removing unwanted references"/>
      <Delete Files="$(OutputPath)ReferenceLibrary.dll"/>
    </Target>
</Project>

Then in the .nuspec include it

<package>
  ...
  <files>
    <file src="Targets/DerivedLibrary.targets" target="/build/DerivedLibrary.targets" />
  </files>
</package>

Then when someone installs the package the post build hook will be added. When they build the files that are copied will then be deleted automatically.

Upvotes: 2

Related Questions