Nuget - Actual package local path location (after restore)

How do I tell what path msbuild/dotnet tools used to reference a nuget package?

Why would I need the local path? A down-stream tool, or a build server task (post build), may need an asset from the package.

Nuget packages could reside in numerous locations:

  • Local Cache
  • Global Cache (see globalPackagesFolder)
  • Project Packages (see repositoryPath)
  • Fallback Location (see fallbackPackageFolders)

NOTE: If found in a cache, they will not be copied to repositoryPath. Therefore, this path may NOT contain all packages once the restore step is complete.

Local Cache

λ nuget locals all -l
http-cache: C:\Users\SomeUserName\AppData\Local\NuGet\v3-cache
global-packages: C:\packages
temp: C:\Users\SomeUserName\AppData\Local\Temp\NuGetScratch
plugins-cache: C:\Users\SomeUserName\AppData\Local\NuGet\plugins-cache

nuget.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <config>
    <add key="repositoryPath" value=".\packages" />
    <add key="globalPackagesFolder" value="c:\packages" />
  </config>

  <packageRestore>
    <add key="enabled" value="True" />
    <add key="automatic" value="True" />
  </packageRestore>

  <packageSources>
    <clear />
    <add key="NuGet" value="https://api.nuget.org/v3/index.json" />
    <add key="Custom" value="https://server.on.internet/xxx" />    
  </packageSources>
	
   <fallbackPackageFolders>
        <add key="datastore" value="\\local-server\smb\share"/>
    </fallbackPackageFolders>

  <solution>
    <add key="disableSourceControlIntegration" value="true" />
  </solution>
</configuration>

How to deteministically get the local path for the package as used by msbuild

  • Add GeneratedPathProperty="true" to PackageReference
  • Reference / emmit generated value

NOTE: In the example below the reference Google.Protobuf is reference by the msbuild property $PkgGoogle_Protobuf (prefix Pkg and . => _)

<Project Sdk="Microsoft.NET.Sdk.Web">
    <ItemGroup>
        <PackageReference Include="Google.Protobuf" Version="1.2.3.4" GeneratePathProperty="true" />
    </ItemGroup>

    <Target Name="TakeAction" AfterTargets="Build">
        <Message IsCritical="true"  Importance="high" Text="OUTPUT:PackagePath__Google.Protobuf=$(PkgGoogle_Protobuf)" />
    </Target>
</Project>

References