NuGet auto package restore does not work with MSBuild

后端 未结 12 2272
粉色の甜心
粉色の甜心 2020-12-02 08:51

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

相关标签:
12条回答
  • 2020-12-02 09:21

    Sometimes this occurs when you have the folder of the package you are trying to restore inside the "packages" folder (i.e. "Packages/EntityFramework.6.0.0/") but the "DLLs" are not inside it (most of the version control systems automatically ignore ".dll" files). This occurs because before NuGet tries to restore each package it checks if the folders already exist, so if it exists, NuGet assumes that the "dll" is inside it. So if this is the problem for you just delete the folder that NuGet will restore it correctly.

    0 讨论(0)
  • 2020-12-02 09:29

    I had an issue with nuget packages not being included in a scripted nightly build that builds the sln file using devenv.exe.

    I followed the advice from Microsoft, and the key step was updating the NuGet config in %AppData%/NuGet so that it contained:

    <configuration>
        <packageRestore>
            <add key="automatic" value="True" />
        </packageRestore>
    </configuration>
    
    0 讨论(0)
  • 2020-12-02 09:31

    Note that if you are using TeamCity as a build server, you get a "NuGet Installer" step that you can use to restore all the packages before the build step.

    0 讨论(0)
  • 2020-12-02 09:32

    If you are using Visual Studio 2017 or later which ships with MSBuild 15 or later, and your .csproj files are in the new PackageReference format, the simplest method is to use the new MSBuild Restore target.


    No-one has actually answered the original question, which is "how do I get NuGet packages to auto-restore when building from the command-line with MSBuild?" The answer is: unless you are using the "Enable NuGet package restore" option (which is now deprecated as per this reference), you can't (but see below). If you are trying to do e.g. automated builds on a CI server, this sucks.

    However there is a slightly roundabout way to get the desired behaviour:

    1. Download the latest NuGet executable from https://dist.nuget.org/win-x86-commandline/latest/nuget.exe and place it somewhere in your PATH. (You can do this as a pre-build step.)
    2. Run nuget restore which will auto-download all the missing packages.
    3. Run msbuild to build your solution.

    Aside: while the new and recommended way to do auto package restore involves less clutter in your version control, it also makes command-line package restore impossible unless you jump through the extra hoop of downloading and running nuget.exe. Progress?

    0 讨论(0)
  • 2020-12-02 09:32

    MSBuild 15 has a /t:restore option that does this. it comes with Visual Studio 2017.

    If you want to use this, you also have to use the new PackageReference, which means replacing the packages.config file with elements like this (do this in *.csproj):

    <ItemGroup>
      <!-- ... -->
      <PackageReference Include="Contoso.Utility.UsefulStuff" Version="3.6.0" />
      <!-- ... -->
    </ItemGroup>
    

    There is an automated migration to this format if you right click on 'References' (it might not show up if you just opened visual studio, rebuild or open up the 'Manage NuGet packages for solution' window and it will start appearing).

    0 讨论(0)
  • 2020-12-02 09:33

    UPDATED with latest official NuGet documentation as of v3.3.0

    Package Restore Approaches

    NuGet offers three approaches to using package restore.


    Automatic Package Restore is the NuGet team's recommended approach to Package Restore within Visual Studio, and it was introduced in NuGet 2.7. Beginning with NuGet 2.7, the NuGet Visual Studio extension integrates into Visual Studio's build events and restores missing packages when a build begins. This feature is enabled by default, but developers can opt out if desired.


    Here's how it works:

    1. On project or solution build, Visual Studio raises an event that a build is beginning within the solution.
    2. NuGet responds to this event and checks for packages.config files included in the solution.
    3. For each packages.config file found, its packages are enumerated and Checked for exists in the solution's packages folder.
    4. Any missing packages are downloaded from the user's configured (and enabled) package sources, respecting the order of the package sources.
    5. As packages are downloaded, they are unzipped into the solution's packages folder.

    If you have Nuget 2.7+ installed; it's important to pick one method for > managing Automatic Package Restore in Visual Studio.

    Two methods are available:

    1. (Nuget 2.7+): Visual Studio -> Tools -> Package Manager -> Package Manager Settings -> Enable Automatic Package Restore
    2. (Nuget 2.6 and below) Right clicking on a solution and clicking "Enable Package Restore for this solution".


    Command-Line Package Restore is required when building a solution from the command-line; it was introduced in early versions of NuGet, but was improved in NuGet 2.7.

    nuget.exe restore contoso.sln
    

    The MSBuild-integrated package restore approach is the original Package Restore implementation and though it continues to work in many scenarios, it does not cover the full set of scenarios addressed by the other two approaches.

    0 讨论(0)
提交回复
热议问题