Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is NuGetPackageImportStamp for?

When adding certain NuGet packges to a project, sometimes a strange item appears in the first property group of the .csproj file.

<PropertyGroup>   ...   <NuGetPackageImportStamp>3d051ef3</NuGetPackageImportStamp> </PropertyGroup> 

The stamp is different every time, and doesn't appear to be sequential.

I can't find any reference to <NuGetPackageImportStamp> in the NuGet documentation. What is it for and how does it work? Is it necessary?

One package that appears to add this every time is StyleCop.MSBuild, at least with the current 4.7.49 version. Simply add that package to a new project and you'll see this mystery item appear in your project file.

like image 519
Matt Johnson-Pint Avatar asked Jun 05 '14 00:06

Matt Johnson-Pint


1 Answers

NuGetPackageImportStamp is a workaround for Visual Studio 2013 and later versions not being able to detect that a NuGet package added or removed an MSBuild import.

This workaround is not required for older versions of Visual Studio. Even in Visual Studio 2013, the property can safely be removed: if you close and re-open the solution, the imported MSBuild targets will be re-loaded by Visual Studio.

NuGet source code reveals that NuGet adds this property when a package adds or removes an MSBuild import. It uses a new GUID each time. Visual Studio 2013 detects that the project has changed and offers its reload. Just adding or removing an MSBuild import is not enough for Visual Studio 2013 to know the project has changed at runtime, which is what NuGet solves by this workaround.

The StyleCop.MSBuild NuGet package includes a custom MSBuild target so adding or removing this NuGet package will cause this property to be added.


UPDATE: Matt Ward's comment is correct and important.

There is still code in NuGet that adds the NuGet package stamp and the code no longer checks for Visual Studio 2013.

Here are my bottom line recommendations:

  • Create your new projects with either Visual Studio or Powershell.
  • When you update an old csproj, and it's not working, compare this old csproj to a new working csproj that you created with either Visual Studio or Powershell. If the new working csproj uses a <NuGetPackageImportStamp> directive, then add it into your old broken csproj and see if that fixes the issues; regardless of which Visual Studio version you're using.
  • If your old csproj still isn't working, consider the <TargetFrameworkProfile> directive. Does it exist in your new working csproj? Consider removing this.
like image 131
Matt Ward Avatar answered Sep 28 '22 07:09

Matt Ward