NuGet Package won't restore from VS2015 with GitLab

Mark Woodard

I have been searching for a few days now about how to get NuGet to automatically restore packages using VS2015 with GitLab, and I have had no luck.

Scenario: I cloned an empty repository from GitLab and used VS to add the default .gitignore and .getattribute files. I created a HelloWorld console app using .NET framework 4.5.2. I want auto-incrementing build numbers, so I installed the MSBuild Extension Pack (type "Install-Package MSBuild.Extension.Pack" in the NuGet console in VS). This modifies the CSPROJ file. I then modified the project file further to make the build and revision numbers update on each build. The net modified portion of the project file looks like this:

<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="..\packages\MSBuild.Extension.Pack.1.8.0\build\net40\MSBuild.Extension.Pack.targets" Condition="Exists('..\packages\MSBuild.Extension.Pack.1.8.0\build\net40\MSBuild.Extension.Pack.targets')" />
<Import Project="$(MSBuildProjectDirectory)\..\packages\MSBuild.Extension.Pack.1.8.0\tools\net40\MSBuild.ExtensionPack.VersionNumber.targets" />
<Target Name="BeforeBuild" Condition="'$(Configuration)' == 'Release'">
  <AssemblyInfo AssemblyInfoFiles="@(AssemblyInfoFiles)" />
</Target>
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
  <PropertyGroup>
    <ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them.  For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
  </PropertyGroup>
  <Error Condition="!Exists('..\packages\MSBuild.Extension.Pack.1.8.0\build\net40\MSBuild.Extension.Pack.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\MSBuild.Extension.Pack.1.8.0\build\net40\MSBuild.Extension.Pack.targets'))" />
</Target>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
     Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->

So I build it, it behaves as expected, and I check it all in. On another machine, I clone the repository and open the solution. The project will not load, and VS made no attempt to restore anything; this is the error:

C:\Users\mwoodard\Source\Repos\c-sharp-commons\HelloConsole\HelloConsole\HelloConsole.csproj : error  : The imported project "C:\Users\mwoodard\Source\Repos\c-sharp-commons\HelloConsole\packages\MSBuild.Extension.Pack.1.8.0\tools\net40\MSBuild.ExtensionPack.VersionNumber.targets" was not found. Confirm that the path in the <Import> declaration is correct, and that the file exists on disk.  C:\Users\mwoodard\Source\Repos\c-sharp-commons\HelloConsole\HelloConsole\HelloConsole.csproj

I see lots of other people having problems with NuGet restores, and lots of solutions, but none of them appear to pertain to projects that were first started using VS2015. (There are many solutions that say to alter something in the project's or solution's .nuget subdirectory...VS2015 creates no such directory.)

On top of all this, the NuGet restore needs to work from MSBuild, since our build machine won't be running Visual Studio.

starian chen-MSFT

To deal with the issue of cannot open solution:

Using this code instead

<Import Project="..\packages\MSBuild.Extension.Pack.1.8.0\tools\net40\MSBuild.ExtensionPack.VersionNumber.targets" Condition="Exists('..\packages\MSBuild.Extension.Pack.1.8.0\tools\net40\MSBuild.ExtensionPack.VersionNumber.targets')" />

After that, the packages will be restored when build in Visual Studio. (Enabling and disabling package restore in VS)

Regarding the issue of restore:

With MSBuild-integrated restore, there is .nuget folder in the solution. Since you don’t want to include .nuget folder to solution folder and do build on build machine (e.g. CI), you need to restore the package before build.

For example, you can add NuGet restore build step if do build through TFS/VSTS vNext build. For gitlab CI build, you can restore package in before_script section in .gitlab-ci.yml file.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related