w128
w128

Reputation: 4928

How to get NuGet packages to restore correctly for an existing project added from another solution?

I have two Visual Studio 2019 solutions, A and B, with both containing several projects. Solution A includes one of the projects from solution B, ProjectB, which was added to A as an existing project. This project uses several NuGet dependencies. Obviously, NuGet packages are not under source control. When a user freshly checks out the solutions from source control, here's what happens:

  1. If solution B is opened and built first, ProjectB's NuGet dependencies get restored as expected, and the project builds successfully.
  2. If solution A is opened and built after 1., everything gets built successfully. However, if step 1 is omitted, ProjectB fails to build within the scope of solution A because its referenced dependencies don't get recreated in the original path, despite NuGet having installed all the dependencies in A's packages folder. This results in plenty of "The referenced component could not be found" error messages, and a situation equivalent to this one.

Simply cleaning the NuGet cache doesn't help. The only thing that seems to resolve the issue is to call

Update-Package -reinstall -ProjectName ProjectB

from the solution A's Package Manager Console prior to triggering a (re)build.

I wanted to automate this by including the Update-Package command in a pre-build event, but apparently Package Manager Console is not accessible there, and I'd like to avoid installing any other software.

Is there a way to correctly get all project dependencies restored in such case, so that the entire solution A would build from scratch automatically?

Upvotes: 0

Views: 1682

Answers (2)

w128
w128

Reputation: 4928

It turns out that you can use PackageReference format inside a project instead of packages folder, and doing so resolves the original issue directly without any extra action needed. So the most elegant solution is to migrate from packages.config to PackageReference: right-click on the problematic project's References in solution explorer, then click Migrate packages.config to PackageReference in the context menu. From now on, NuGet dependencies will resolve correctly when a project is included in another solution.

Upvotes: 1

user2966445
user2966445

Reputation: 1344

Sounds like you need to add a Nuget.Config file as a Solution Item to each solution and add the "respositoryPath" config key to the file so the two solutions are resolving Nuget package references to the same folder:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <config>
     <add key="repositoryPath" value="../../Nuget/packages" />
  <config>
</configuration>

Obviously, you'll want to adjust the repositoryPath value for each Nuget.Config file so the same path is used for both solution (if Solution A is one folder lower in the file structure than Solution B, you'll need to add another ../ to the path).

After you add the Nuget.Config file to each solution, you need to close/re-open each solution and then run Update-Package -reinstall to ensure the reference paths are updated in each project file.

Upvotes: 1

Related Questions