Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NuGet References invalid when changed to Release build configuration

I'm experiencing rather a strange issue with NuGet (most recent) and VS2012. I have a multi-project solution with NuGet package management enabled and as long as I'm using Debug|AnyCPU configuration, it all compiles and application runs correctly. As soon as I switch to Release|AnyCPU, it seems like all the NuGet assembly references were gone and the app obviously do not compile. Strangely, I can see all the references in NuGet Manager UI for all the correct projects.

Did you ever experienced similar issue and do you know how to fix it?

Rebuild did not helped. NuGet references were set up while Debug configuration was set.

like image 902
Miro Hudak Avatar asked Feb 12 '13 21:02

Miro Hudak


People also ask

How do I reference an existing NuGet package from a new project?

NET or . NET Core project. After you install a NuGet package, you can then make a reference to it in your code with the using <namespace> statement, where <namespace> is the name of package you're using. After you've made a reference, you can then call the package through its API.

How do I restore references in Visual Studio?

Restore by using MSBuild You can use msbuild -t:restore to restore packages in NuGet 4. x+ and MSBuild 15.1+, which are included with Visual Studio 2017 and higher. This command restores packages in projects that use PackageReference for package references.

How do I refresh a NuGet package in Visual Studio?

In Tools -> Options -> NuGet Package Manager -> General you need to select the "Allow NuGet to download missing packages" option which allows NuGet to restore and the "Automatically check for missing packages during build in Visual Studio" which enables on build restore.

How do I clean my NuGet package?

Open Visual Studio, go to Tools -> NuGet Package Manager -> Package Manager Settings menu. Click Clear All NuGet Cache(s) button in options dialog then clearing process is started. Once NuGet cache location is cleared, click ok button.


3 Answers

Once again, I'm answering my own question...

The point was, I was changing the structure of solution and moved some projects around. That means, you get invalid relative paths in .csproj files per project. Which is OK, as long as you don't change the depth of the structure, which I did.

For unknown reason, debug build went just fine with invalid paths (probably the assemblies are gathered some other way, as I manually deleted bin and obj folders and cleared solution, etc...). But for release build, it tried to obtain the assemblies from the (wrongly referenced) packages/ directory.

NO PART of NuGet process found/reported this issue and in all UI consoles and even in the NuGet PowerShell Console itself, everything seemed just OK.

The solution is obvious. Edit the .csproj files for affected projects (I was actually guided to the issue by filtering warnings and finding out, only some assemblies were affected and those were from outside of the main solution directory) and change the paths of assembly references to correct NuGet packages folder.

On the side note: This is THE problem of NuGet (and possibly VS) references handling. It is trying to use some relative paths instead of adding some variable like ${SOLUTION_ROOT} or ${NUGET_ROOT} or ${PACKAGES_FOLDER}. If it was done by variable replacement, one could use the same projects in multiple solutions, without breaking NuGet packages configuration. At least I haven't found the way to share the same project folder across multiple solutions.

like image 187
Miro Hudak Avatar answered Nov 04 '22 12:11

Miro Hudak


I was able to resolve this issue by uninstalling, then installing the Nuget packages my project was using.

like image 43
Baron Avatar answered Nov 04 '22 11:11

Baron


Late to the party, but it may help someone. This issue can occur if you change your nuget repositoryPath (perhaps via nuget.config), to a different path, mid project.

e.g.

  • Nuget will initially pull your packages into a default path
  • Nuget repositoryPath is changed.
  • New packages will be pulled into this path, however the existing path will not be cleared down (msbuild clean wont help either).
  • When you build, your hint paths, will point to the initial repo path, and will build, as the packages still exist in that location.
  • (Worse) Any nuget packages added now, will point to the new repo path.

As mentioned by @Baron, reinstalling will resolve all this mess. I'd also recommend deleting all bin/obj/packages from your working tree, to ensure you don't have any crud.

like image 1
jasper Avatar answered Nov 04 '22 13:11

jasper