NuGet logo

Nuget Package restore issues

After upgrading Nuget on VS2013, we’ve started hitting an issue where Nuget won’t restore packages. We get error messages similar to this:

Error 21 Could not find a part of the path 'C:\Dev\YourProject\ \packages\Microsoft.AspNet.WebApi.Core.5.2.3'.

I’ve found out that there is a change to the way Nuget picks up files. To get it to work you need to edit the file NuGet.targets changing the line:

<RestoreCommand>$(NuGetCommand) install "$(PackagesConfig)" -source "$(PackageSources)" $(NonInteractiveSwitch) $(RequireConsentSwitch) -solutionDir $(PaddedSolutionDir)</RestoreCommand>

To:

<RestoreCommand>$(NuGetCommand) install "$(PackagesConfig)" -source "$(PackageSources)" $(NonInteractiveSwitch) $(RequireConsentSwitch) -solutionDir $(SolutionDir)\</RestoreCommand>

This sorts the issue.

Advertisements
NuGet logo

Automatically building NuGet packages for your project–updated

As a follow on from my last post on this subject, I’ve come across a simplified way of doing this.

  1. Build your .nuspec file as usual. (use NuGet spec in a CMD window in the project folder to generate the basic file).
  2. Update the AssemblyInfo.cs file with a description and a company, or the NuGet build will throw errors.
  3. Edit the project file:

Add these two lines to the first <PropertyGroup>:

<RestorePackages>true</RestorePackages>
<BuildPackage>true</BuildPackage>

Add these lines at the end for the file:

<ImportProject=”$(SolutionDir)\.nuget\NuGet.targets”Condition=”Exists(‘$(SolutionDir)\.nuget\NuGet.targets’)”/><TargetName=”EnsureNuGetPackageBuildImports”BeforeTargets=”PrepareForBuild”>    <PropertyGroup>
      <ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.
      </ErrorText>
   </PropertyGroup>
      <ErrorCondition=”!Exists(‘$(SolutionDir)\.nuget\NuGet.targets’)”Text=”$([System.String]::Format(‘$(ErrorText)’, ‘$(SolutionDir)\.nuget\NuGet.targets’))”/></Target>

Automatically building NuGet packages for your project

I’ve been using the Nuget Package Explorer for ages, but recently found out how to automatically build  the NuGet for a project as part of your normal build.

  1. Enable NuGet Package Restore  for the solution. This will create the .nugget folder with NuGet.exe and a configuration file.
  2.  You need to edit the .csProj file and add the following lines:
    <Target Name="AfterBuild" Condition=" '$(Configuration)' == 'Release'">
       <GetAssemblyIdentity AssemblyFiles="$(TargetPath)"> 
          <Output TaskParameter="Assemblies" ItemName="AssemblyVersion" /> 
       </GetAssemblyIdentity> 
       <Exec Command="echo %(AssemblyVersion.Version)" /> 
       <Message Text="Released %(AssemblyVersion.Version)" Importance="high" /> 
       <Exec Command="nuget pack TG.DocumentManagement.RetrievalIntegration.csproj -Version %(AssemblyVersion.Version)">
       </Exec> 
    </Target>
  3. Create a .nuspec file:
    <?xml version="1.0"?>
    <package >
      <metadata>
        <id>$id$</id>
        <version>$version$</version>
        <title>$title$</title>
        <authors>$author$</authors>
        <owners>$author$</owners>
      <iconUrl>http://server//images/icon_32x32.png</iconUrl>
        <requireLicenseAcceptance>false</requireLicenseAcceptance>
        <description>$description$</description>
        <releaseNotes>Summary of changes made in this release of the package.</releaseNotes>
      <copyright>Copyright ©SilverCode Solutions Limited 2016</copyright>
        <tags>SilverCode  Project blah blah blah</tags>
      </metadata>
      <files>
       <file src="web.config.transform" target="content/web.config.transform"/>
      </files>
    </package>
  4. Reload the project, every build should create a nugget package for in the output folder.
NuGet logo

NuGet Package Restore problems

I was having some trouble with a new solution, I had added a few NuGet packages to various projects, and everything was working. I checked it into TFS and asked a colleague to review the code.
He got the code out of TFS and tried to run it, but ran into some problems, because not all of the NuGet packages had been restored. I had enabled the “Restore NuGet Packages on build” option, so I was a bit stumped.

After some investigation I discovered the problem. I had checked in the packages folder in the route of the solution. This obviously becomes write-protected, and interferes with the package restore. I closed the solution, removed the packages folder from source control, and reopened the solution and rebuilt. All the packages were downloaded and the solution ran without issue.