How to resolve Nuget Package Version and Path in Pre-Build Event of a Project?

How to resolve Nuget Package Version and Path in Pre-Build Event of a Project?

If you want to reinstall package automatically in Pre-build event, I am afraid you can`t achieve it currently.

We could use the command Update-Package -Id <package_name> –reinstall to reinstall the packages to your project in the Package Manager Console, but it is impossible to automate that.

If you want to automate it in the build event, you have to call the NuGet CLI rather than Package Manager Console. Because NuGet CLI does not modify a project file or packages.config; in this way it’s similar to restore in that it only adds packages to disk but does not change a project’s dependencies. See NuGet CLI reference.

The operation Install packages on NuGet CLI:

enter image description here

Conversely, operation Install packages on Package Manager:

Installs a package and its dependencies into a project.

So we could not use NuGet CLI to reinstall NuGet packages for project.

Besides, we could not use the Package manager console powershell outside visual studio, because package manager console is providing is access to visual studio objects.

https://github.com/NuGet/Home/issues/1512

Similarly, we could not use Package manager console in the build event, build events are run by MSBuild so it needs to work when the build is run from the command line.

So it seems impossible to automate reinstall NuGet packages, or the alternative approach would be to write a console app that uses NuGet.Core.dll to do the same thing that the PowerShell script is doing.

Leave a Comment