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


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

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

    <clear />
    <add key="NuGet" value="" />
    <add key="Custom" value="https://server.on.internet/xxx" />    
        <add key="datastore" value="\\local-server\smb\share"/>

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

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">
        <PackageReference Include="Google.Protobuf" Version="" GeneratePathProperty="true" />

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