3
votes

I am authoring a nuget package that will contain MSBuild .targets and .props files as per nuget documentation (requires nuget 2.5+).

I intend for it to be used with automatic package restore.

When I add the resulting package to a csproj two things happen:

As Expected:

<Import Project="..\packages\MyPackage.1.0.0\build\net40\MyPackage.props" Condition="Exists('..\packages\MyPackage.1.0.0\build\net40\MyPackage.props')" />
...
...
<Import Project="..\packages\MyPackage.1.0.0\build\net40\MyPackage.targets" Condition="Exists('..\packages\MyPackage.1.0.0\build\net40\MyPackage.targets')" />

Not expected:

<PropertyGroup>
<RestorePackages>true</RestorePackages>
...
</PropertyGroup>
...
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
<Target Name="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>
  <Error Condition="!Exists('$(SolutionDir)\.nuget\NuGet.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)\.nuget\NuGet.targets'))" />
  ...
</Target>

That is the 'Not expected' behaviour is the MSBuild-integrated package restore scaffolding.

I have removed this scaffolding manually as per the documentation but when I submit the project to a build server the automaitc-package-restore appears to add the scaffolding back in, resulting in a build failure.

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 D:\TFSBuilds\...\.nuget\NuGet.targets.

Does anyone have a way of blocking/preventing this scaffolding from being included when the package includes msbuild targets?

update

In lieu of a solution I tried a workaround. That is, I created another nuget package to address the missing nuget.targets file. When added to the project, this new package itself includes a target that DependsOnTargets EnsureNuGetPackageBuildImports and creates a blank/valid nuget.targets. This satisfies the condition triggered from the csproj scaffolding, while allowing the nuget automatic package restore function to still be triggered on the build server.

<Target Name="NuGetPackAutomaticPackageRestorePreBuildWorkaround" BeforeTargets="EnsureNuGetPackageBuildImports"  Condition="!Exists('$(SolutionDir)\.nuget\NuGet.targets')">
  <ItemGroup>
    <PatchTargets Include="$(MSBuildThisFileDirectory)TargetPatches\*.targets"/>
  </ItemGroup>
  <Copy
      SourceFiles="@(PatchTargets)"
      DestinationFolder="$(SolutionDir)\.nuget\"/>
</Target>
1
In lieu of a solution, I came up with a workaround. That is, I created another nuget package to address the missing nuget.targets file. When added to the project, this new package itself includes a target that DependsOnTargets EnsureNuGetPackageBuildImports and creates a blank/valid nuget.targets. This satisfies the condition triggered from the csproj scaffolding, while allowing the nuget automatic package restore function to still be triggered on the build server.user1920322

1 Answers

0
votes

Does the folder that the solution lives in have a .nuget folder? If so, try deleting that.

I'm tweaking a solution that was using the old (pre nuget 2.7) way of package restore, and was having the same issue until I deleted that folder.