How does nuget handle project references when publishing packages?

Viewed 442

So for example lets say I have 2x csproj called A and B.

We have a project reference in B to A, so there are a few scenarios here and I cannot find information on how its handled:

Scenario 1 - Publish B when A isnt a published package

I assume in this case it would fail when trying to publish B because it cannot find a suitable dependency on nuget for A, although I am guessing here so clarification would be good.

Scenario 2 - Publish both packages independently

This is one of the more interesting scenarios, so lets say nothing has changed in A but we want to publish B, how does it translate the project reference from A to a nuget dependency on A? Does it just look for the latest build of A in nuget and add a dependency to that one?

Scenario 3 - Both packages are published together

This is one of the more common scenarios and in this case I can see that whenever you publish one package in the solution you also package new versions for everything else so their version numbers stay in line, this seems to create dependencies to all the same versions as expected here.


So scenario 1 and 2 are the main bits I am interested in as we started a discussion around if we should have project references vs package references between projects in the same solution, and if you are going to have package references to local projects it seems pointless, they may as well be in their own repo. So I just wanted to know how nuget handles the actual publishing so we can continue the debate with some facts around versioning etc.

1 Answers

It depends on the options you supply to the command to create the nuget package. You can either create it independently or create including referenced projects.

To include those that are referenced inside your project e.g for B to include A you can run the pack command with the following option:

nuget pack -Prop Configuration=Release -IncludeReferencedProjects

The option -Prop gives you the ability to set Configuration you want to apply when building the .nupkg. The option -IncludeReferencedProjects does include all the referenced project dependencies to the .nupkg file.

If your project is dependent on a another project/class library I would include it in the created nuget package. Otherwise why would you publish a broken package. If you publish them both separately then nuget should be able to resolve the dependency by installing it from the nuget source with the version you specified inside the B package. If it cannot find the specific reference version then it is a broken package, as Jon mentioned you would have to be careful to publish your package in proper sequence or all together. If your nuget cannot resolve it from nuget dependencies it will automatically include the dll if they exist in your solution.

Related