Note
About this fork. The local-shared branch is upstream main plus the changes proposed in microsoft#191: the open pull requests, the changes in Common they rely on, and a Microsoft.MSBuildCache.LocalShared plugin, a local cache that concurrent builds on one machine can share. It's the code we've run daily since November 2026, with concurrent CI jobs and several worktrees per developer machine sharing one cache. Each change has its own upstream issue or pull request, listed in microsoft#191, and this branch is rebased when they merge. Nothing here is published to nuget.org: build the packages yourself.
Try it
- Build the packages, with Visual Studio 2026 or the .NET SDK listed in
global.json:The packages are ingit clone -b local-shared https://github.com/djack42/MSBuildCache.git cd MSBuildCache dotnet build MSBuildCache.sln -c Releaseartifacts\Release\packages. Their version is in the file names, e.g.Microsoft.MSBuildCache.LocalShared.0.1.<n>-preview-g<commit>.nupkg. - In the repository to build, add that folder as a package source in
nuget.config, and map the MSBuildCache packages to it:<configuration> <packageSources> <add key="MSBuildCacheLocal" value="C:\src\MSBuildCache\artifacts\Release\packages" /> </packageSources> <packageSourceMapping> <packageSource key="nuget.org"> <package pattern="*" /> </packageSource> <packageSource key="MSBuildCacheLocal"> <package pattern="Microsoft.MSBuildCache.*" /> </packageSource> </packageSourceMapping> </configuration>
- Reference the plugin as in Usage below, with
MSBuildCachePackageNameset toMicrosoft.MSBuildCache.LocalSharedandMSBuildCachePackageVersionset to the exact version of the packages you built. Then build withmsbuild -graph -reportfileaccesses, and pointMSBuildCacheLocalCacheRootPathat a folder the builds share, on ReFS or a Dev Drive if you can, so files are cloned rather than copied.
This project provides plugin implementations for the experimental MSBuild Project Cache, which enables project-level caching within MSBuild.
This feature requires Visual Studio 17.9 or later.
To enable caching, simply add a <PackageReference> for the desired cache implementation and set various properties to configure it.
For repos which build C# code, add a <PackageReference> to Microsoft.MSBuildCache.SharedCompilation for shared compilation support.
Here is an example if you're using NuGet's Central Package Management and Azure Pipelines:
Directory.Packages.props:
<PropertyGroup>
<!-- In Azure pipelines, use Pipeline Caching as the cache storage backend. Otherwise, use the local cache. -->
<MSBuildCachePackageName Condition="'$(TF_BUILD)' != ''">Microsoft.MSBuildCache.AzurePipelines</MSBuildCachePackageName>
<MSBuildCachePackageName Condition="'$(MSBuildCachePackageName)' == ''">Microsoft.MSBuildCache.Local</MSBuildCachePackageName>
<!-- Replace this with the latest version -->
<MSBuildCachePackageVersion>...</MSBuildCachePackageVersion>
</PropertyGroup>
<ItemGroup>
<GlobalPackageReference Include="$(MSBuildCachePackageName)" Version="$(MSBuildCachePackageVersion)" />
<GlobalPackageReference Include="Microsoft.MSBuildCache.SharedCompilation" Version="$(MSBuildCachePackageVersion)" />
</ItemGroup>For repos using C++, you will need to add the projects to a packages.config and import the props/targets files directly.
Directory.Build.props:
<PropertyGroup>
<PackagesConfigFile>packages.config</PackagesConfigFile>
<PackagesConfigContents>$([System.IO.File]::ReadAllText("$(PackagesConfigFile)"))</PackagesConfigContents>
<MSBuildCachePackageVersion>$([System.Text.RegularExpressions.Regex]::Match($(PackagesConfigContents), 'Microsoft.MSBuildCache.*?version="(.*?)"').Groups[1].Value)</MSBuildCachePackageVersion>
<MSBuildCachePackageRoot>$(NugetPackageDirectory)\$(MSBuildCachePackageName).$(MSBuildCachePackageVersion)</MSBuildCachePackageRoot>
</PropertyGroup>
<Import Project="$(MSBuildCachePackageRoot)\build\$(MSBuildCachePackageName).props" />Directory.Build.targets:
<Import Project="$(MSBuildCachePackageRoot)\build\$(MSBuildCachePackageName).targets" />These settings are common across all plugins, although different implementations may ignore or handle them differently. Refer to the specific plugin documentation for details.
| MSBuild Property Name | Setting Type | Default value | Description |
|---|---|---|---|
$(MSBuildCacheLogDirectory) |
string |
"MSBuildCacheLogs" | Base directory to use for logging. If a relative path, it's assumed relative to the repo root. |
$(MSBuildCacheLogLevel) |
LogLevel |
Full | Which files to write to the log directory. Full writes every log, Minimal keeps only what explains a cache miss (for each project built: fingerprints with one entry per dependency, path sets, observed inputs and outputs, build result; for each cache hit, the fingerprints of the entry used, in hits.json; and the cache client log) and drops the raw file accesses and parser information, and None writes no log files. |
$(MSBuildCacheCacheUniverse) |
string |
"default" | The cache universe is used to isolate the cache. This can be used to bust the cache, or to isolate some types of builds from other types. |
$(MSBuildCacheMaxConcurrentCacheContentOperations) |
int |
64 | The maximum number of cache operations to perform concurrently |
$(MSBuildCacheLocalCacheRootPath) |
string |
"\MSBuildCache" (in repo's drive root) | Base directory to use for the local cache. |
$(MSBuildCacheLocalCacheSizeInMegabytes) |
int |
102400 (100 GB) | The maximum size in megabytes of the local cache |
$(MSBuildCacheIgnoredInputPatterns) |
Glob[] |
Files which are part of the repo which should be ignored for cache invalidation | |
$(MSBuildCacheIgnoredOutputPatterns) |
Glob[] |
*.assets.cache; *assemblyreference.cache |
Files to ignore for output collection into the cache. Note that if output are ignored, the replayed cache entry will not have these files. This should be used for intermediate outputs which are not properly portable |
$(MSBuildCacheIdenticalDuplicateOutputPatterns) |
Glob[] |
Files to allow duplicate outputs, with identical content, across projects. Projects which produce files at the same path with differing content will still produce an error. Note: this setting should be used sparingly as it impacts performance | |
$(MSBuildCacheRemoteCacheIsReadOnly) |
bool |
false | Whether the remote cache is read-only. This can be useful for scenarios where the remote cache should only be read from but not produced to. |
$(MSBuildCacheSkipCacheLookup) |
bool |
false | Whether to skip the cache lookup so that every project is built, while still adding the results to the cache. This can be useful for a periodic build which refreshes the cache and produces the fingerprint logs of every project. Existing cache entries are kept. |
$(MSBuildCacheAsyncCachePublishing) |
bool |
true | Whether files are published asynchronously to the cache as opposed to delaying the completion signal to MSBuild until publishing is complete. Note: Publishing will be awaited before the overall build completes. |
$(MSBuildCacheAsyncCacheMaterialization) |
bool |
true | Whether files are materialized on disk asynchronously from the cache as opposed to delaying the completion signal to MSBuild until publishing is complete. Note: Materialization will be awaited when a depending project requires the files and before the overall build completes. |
$(MSBuildCacheAllowFileAccessAfterProjectFinishProcessPatterns) |
Glob[] |
\**\vctip.exe |
Processes to allow file accesses after the project which launched it completes, ie accesses by a detached process. Note: these accesses will not be considered for caching. |
$(MSBuildCacheAllowFileAccessAfterProjectFinishFilePatterns) |
Glob[] |
Files to allow to be accessed by a process launched by a project after the project completes, ie accesses by a detached process. Note: these accesses will not be considered for caching. | |
$(MSBuildCacheLogAllowFileAccessAfterProjectFinishMatchesAsMessages) |
bool |
false | Whether late file accesses matching $(MSBuildCacheAllowFileAccessAfterProjectFinishProcessPatterns) or $(MSBuildCacheAllowFileAccessAfterProjectFinishFilePatterns) are logged as messages instead of warnings. This can keep these diagnostics from being promoted by /warnaserror; other warnings remain unaffected. |
$(MSBuildCacheAllowProcessCloseAfterProjectFinishProcessPatterns) |
Glob[] |
\**\mspdbsrv.exe |
Processes to allow to exit after the project which launched it completes, ie detached processes. |
$(MSBuildCacheGlobalPropertiesToIgnore) |
string[] |
CurrentSolutionConfigurationContents; ShouldUnsetParentConfigurationAndPlatform; BuildingInsideVisualStudio; BuildingSolutionFile; SolutionDir; SolutionExt; SolutionFileName; SolutionName; SolutionPath; _MSDeployUserAgent, as well as all proeprties related to plugin settings |
The list of global properties to exclude from consideration by the cache |
$(MSBuildCacheGetResultsForUnqueriedDependencies) |
bool |
false | Whether to try and query the cache for dependencies if they have not previously been requested. This option can help in cases where the build isn't done in graph order, or if some projects are skipped. |
$(MSBuildCacheTargetsToIgnore) |
string[] |
GetTargetFrameworks;GetNativeManifest;GetCopyToOutputDirectoryItems;GetTargetFrameworksWithPlatformForSingleTargetFramework |
The list of targets to ignore when determining if a build request matches a cache entry. This is intended for "information gathering" targets which do not have side-effect. eg. a build with /t:Build and /t:Build;GetTargetFrameworks should be considered to have equivalent results. Note: This only works "one-way" in that the build request is allowed to have missing targets, while the cache entry is not. This is to avoid a situation where a build request receives a cache hit with missing target results, where a cache hit with extra target results is acceptable. |
$(MSBuildCacheSkipUnchangedOutputFiles) |
bool |
false | Whether to avoid writing output files on cache hit if the file is unchanged, which can improve performance for incremental builds. A file is considered unchanged if it exists, the previously placed file and file to be placed have the same hash, and the the previously placed file and current file on disk have the same timestamp and file size. |
$(MSBuildCacheTouchOutputFiles) |
bool |
false | Whether to update the last write time for output files on cache hit. All files for a given cache entry will have the same timestamp. Note that outputs which skip materialization via MSBuildCacheSkipUnchangedOutputFiles are still touched. |
$(MSBuildCacheIgnoreDotNetSdkPatchVersion) |
bool |
false | Whether to ignore the patch version when doing cache lookups. This trades off some correctness for the sake of getting cache hits when the SDK version isn't exactly the same. The default behavior is to consider the exact SDK version, eg. "8.0.404". With this setting set to true, it will instead use something like "8.0.4XX". Note that the major version, minor version, and feature bands are still considered. |
$(MSBuildCacheSkipNodesWithLeftoverOutputs) |
bool |
true | Whether to not add to the cache a project which used files left in its output directories (OutDir, IntermediateOutputPath) by a previous build without writing them, e.g. an incremental build whose targets were up to date. Such a project's outputs would miss the files it skipped. Its result is still used by its dependents in the build. |
$(MSBuildCacheEnableProbeAndEnumerationFingerprinting) |
bool |
true | Whether file probes (existence checks) and directory enumerations contribute to the strong fingerprint, enabling correct caching for incremental builds — including cases where MSBuild source globs match different files. Requires an MSBuild that reports directory enumeration patterns; on older versions this is forced to false and a message is logged. |
Skipping unchanged outputs reduces disk I/O by relying on saved content identity and file metadata instead of rehashing existing files. Timestamp updates are a separate policy: they can affect downstream incrementality even when contents are unchanged or output replacement was skipped.
When configuring settings which are list types, you should always append to the existing value to avoid overriding the defaults:
<PropertyGroup>
<MSBuildCacheIdenticalDuplicateOutputPatterns>$(MSBuildCacheIdenticalDuplicateOutputPatterns);**\foo.txt</MSBuildCacheIdenticalDuplicateOutputPatterns>
</PropertyGroup>Once configured, to execute builds with caching, simply run:
msbuild /graph /m /reportfileaccesses
It's also recommended to set some parameters like /p:MSBuildCacheLogDirectory=$(LogDirectory)\MSBuildCache as part of the MSBuild call, as opposed to with MSBuild properties in your Directory.Build.props for example.
Arbitrary MSBuild targets can be cached, and with Microsoft.Build.RunVSTest, you can attach running vstest-based unit tests with the "Test" target.
Once Microsoft.Build.RunVSTest, or some other target hooked to the Test target, you can get cache hits for tests by adding (/t:Build;Test), eg:
msbuild /graph /m /reportfileaccesses /t:Build;Test
This not only provides the benefits of caching unit test execution, but also executes tests concurrently with other, unrelated, projects in the graph.
This implementation uses Azure Pipeline Caching as the cache storage, which is ideal for repos using Azure Pipelines for their builds.
Please refer to the cache isolation and security pipeline caching provides to understand the security around this cache implementation.
It is expected that this plugin is used within an Azure Pipeline. The SYSTEM_ACCESSTOKEN environment variable will need to be explicitly passed to the task calling MSBuild since it's considered a secret. In classic pipelines this is done by checking "Allow scripts to access the OAuth token" in the job. In yml pipelines, this is done by passing the env var explicitly using the $(System.AccessToken) variable:
- script: MSBuild /graph /restore /reportfileaccesses /bl:$(Build.ArtifactStagingDirectory)\Logs\msbuild.binlog /p:Configuration=Release
displayName: Build
env:
SYSTEM_ACCESSTOKEN: $(System.AccessToken)$(System.AccessToken) by default does not have the scope relevant to Pipeline Caching. The scope can be enabled by setting the EnablePipelineCache pipeline variable. Additionally, the scope will get automatically added if the Cache task is used somewhere in the pipeline.
This implementation uses the local file system for the cache. In particular, it uses BuildXL's LocalCache. This is recommended for locally testing and debugging caching for your repo. This implementation can also be useful if you have stateful build agents where the cache can be reused across builds.
This implementation also uses the local file system for the cache, but as plain files without any lock or database, so several builds can use the same cache at the same time (for example builds in several clones or worktrees of a repo on one machine, or concurrent jobs on a build agent). The cache is stored in a FileSystemCache.v1 folder under $(MSBuildCacheLocalCacheRootPath).
Files are cloned between the cache and the repo when the file system supports copy-on-write (ReFS, Dev Drive), and copied otherwise. Store errors make the affected node a cache miss rather than failing the build. At the end of a build, when the content exceeds $(MSBuildCacheLocalCacheSizeInMegabytes), the least recently used entries are deleted until the content they reference fits in 80% of it, and content no longer referenced is deleted by a later build, at least an hour after.
This implementation uses Azure Blob Storage as the cache storage.
Warning
This implementation does not yet have a robust security model. All builds using this will need write access to the storage resource, so for example an external contributor could send a PR which would write/overwrite arbitrary content which could then be used by CI builds. Builds using this plugin must be restricted to trusted team members. Use at your own risk.
These settings are available in addition to the Common Settings:
| MSBuild Property Name | Setting Type | Default value | Description |
|---|---|---|---|
$(MSBuildCacheBlobUri) |
Uri |
Specifies the uri of the Azure Storage Blob. | |
$(MSBuildCacheManagedIdentityClientId) |
string |
Specifies the managed identity client id when using the "ManagedIdentity" credential type | |
$(MSBuildCacheAllowInteractiveAuth) |
bool |
true, except when running in Azure Pipelines or GitHub Actions | Whether interactive auth is allowed |
$(MSBuildCacheInteractiveAuthTokenDirectory) |
string |
"%LOCALAPPDATA%\MSBuildCache\AuthTokenCache" | Specifies a token cache directory when using the "Interactive" credential type |
$(MSBuildCacheBuildCacheConfigurationFile) |
string |
The path to the 1ES Build Cache configuration file when. This scenario is typically when running in a 1ES Hosted Pool | |
$(MSBuildCacheBuildCacheResourceId) |
string |
The 1ES Build Cache resource ID. This scenario is typically when consuming 1ES Build Cache from a developer machine. |
Either $(MSBuildCacheBlobUri), $(MSBuildCacheBuildCacheConfigurationFile), or $(MSBuildCacheBuildCacheResourceId) must be provided.
The various methods of authenticating are as follows and attempted in priority order:
- If
$(MSBuildCacheBuildCacheConfigurationFile)is set, it will be used for both the blob endpoints and authentication. - If
$(MSBuildCacheBuildCacheResourceId)is set, an Azure credential will be used to authenticate to the resource (see below). - If the
MSBCACHE_CONNECTIONSTRINGenvironment variable is set, its value will be used as a connection string to connect. This connection string needs both read and write access to the resource. This is not recommended as it is difficult to propertly secure and should only be used for prototyping. - If
$(MSBuildCacheBlobUri)is set, an Azure credential will be used to authenticate to the blob (see below).
In the cases where an Azure credential is acquired, the following methods will be used:
- If the
MSBCACHE_ACCESSTOKENenvironment variable is set, its value will be used directly. - If a
TokenCredentialis provided directly in the plugin's constructor, it will be used. This only applies when using the programmatic project cache API. - If
$(MSBuildCacheManagedIdentityClientId)is set, it will be used as a user-assigned managed identity. This is recommended for non-interactive scenarios. - If
$(MSBuildCacheAllowInteractiveAuth)is true, credentials will be obtained interactively. This is recommended for developer scenarios.
This package enables accurate file access reporting for the Roslyn Compiler Server. Because the compiler server (vbcscompiler) launches as a detached process, its file accesses are not observed by MSBuild. This package manually reports these files accesses to the plugin.
In the future, this feature will be directly supported by Roslyn, at which point this package will no longer be needed.
This package contains much of the shared logic for the plugins, including handling file accesses and cache fingerprinting. For those wanting to use a different cache backend than the ones associated with the available plugins, referencing this package can be a good option to avoid having to duplicate the rest of the logic involved with caching.
See CONTRIBUTING.md
Design documentation:
Microsoft.MSBuildCache is licensed under the MIT license.
This project may contain trademarks or logos for projects, products, or services. Authorized use of Microsoft trademarks or logos is subject to and must follow Microsoft's Trademark & Brand Guidelines. Use of Microsoft trademarks or logos in modified versions of this project must not cause confusion or imply Microsoft sponsorship. Any use of third-party trademarks or logos are subject to those third-party's policies.