I\'m trying to build a solution with packages
content missing (except repositories.config
inside) with MSBuild 12.0. I expect it to auto restore al
Nuget's Automatic Package Restore is a feature of the Visual Studio (starting in 2013), not MSBuild. You'll have to run nuget.exe restore
if you want to restore packages from the command line.
You can also use the Enable Nuget Package Restore feature, but this is no longer recommended by the nuget folks because it makes intrusive changes to the project files and may cause problems if you build those projects in another solution.
Ian Kemp has the answer (have some points btw..), this is to simply add some meat to one of his steps.
The reason I ended up here was that dev's machines were building fine, but the build server simply wasn't pulling down the packages required (empty packages folder) and therefore the build was failing. Logging onto the build server and manually building the solution worked, however.
To fulfil the second of Ians 3 point steps (running nuget restore), you can create an MSBuild target running the exec command to run the nuget restore command, as below (in this case nuget.exe is in the .nuget folder, rather than on the path), which can then be run in a TeamCity build step (other CI available...) immediately prior to building the solution
<Target Name="BeforeBuild">
<Exec Command="..\.nuget\nuget restore ..\MySolution.sln"/>
</Target>
For the record I'd already tried the "nuget installer" runner type but this step was hanging on web projects (worked for DLL's and Windows projects)
You can also use
Update-Package -reinstall
to restore the NuGet packages on the Package Management Console in Visual Studio.
There is a packages.config file with the project, it contains the package details.
Also there is a .nuget folder which contains the NuGet.exe and NuGet.targets. if any one of the file is missing it will not restore the missing package and cause "are you missing a using directive or an assembly reference?" error
It took me some time to figure out the whole picture and I'd like to share here.
Visual Studio has two approaches to use package restore: Automatic Package Restore and MSBuild-Integrated package restore. The 'MSBuild-Integrated Package Restore' restores packages DURING the building process that might cause issues in some scenarios. The 'Automatic Package Restore' is the recommended approach by the NuGet team.
There are several steps to to make 'Automatic Package Restore' work:
In Visual Studio, Tools -> Extensions and Updates, Upgrade NuGet if there is a newer version (Version 2.7 or later)
If you use TFS, in your solution's .nuget folder, remove the NuGet.exe and NuGet.targes files. Then edit NuGet.Config to not check in NuGet packages:
<configuration>
<solution>
<add key="disableSourceControlIntegration" value="true" />
</solution>
</configuration>
If you checked in the solution's packages folder to TFS before, delete the folder and check in the deletion of package folder deletion.
If you don't use TFS, delete the .nuget folder.
In each project file (.csproj or .vbproj) in your solution, remove the line that references NuGet.targets file. The reference looks like this:
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
Remove this line in every project file in your solution.
In Visual Studio menu, either through
Tools -> Options -> Package Manager -> General or Tools -> NuGet Package Manager -> Package Manager Settings
please enable the following two options 1) 'Allow NuGet to download missing packages' 2) 'Automatically check for missing packages during build in Visual Studio'
Test your package restore configuration by the following steps
In Visual Studio 2017 - When you compile using IDE - It will download all the missing nuget packages and save in the folder "packages".
But on the build machine compilation was done using msbuild.exe. In that case, I downloaded nuget.exe and kept in path.
During each build process before executing msbuild.exe. It will execute -> nuget.exe restore NAME_OF_SLN_File (if there is only one .SLN file then you can ignore that parameter)