documentation/wiki/Results-Cache.md
ResultsCacheMSBuild uses caching to speed up builds. It does this by remembering the outcomes of parts of the build it has already completed. If it needs to build the same part again with the same settings, it can often reuse the stored outcome instead of doing all the work from scratch. This is especially helpful for large projects or when you're making small changes and rebuilding.
ResultsCache (The Core Cache Component)ResultsCache is the primary storage mechanism where MSBuild keeps the outcomes of its build targets.
Stores and Retrieves Build Outcomes:
BuildResult objects. Each BuildResult is a record of the outcome for one or more build steps (targets) for a specific project setup (configuration).configurationId) that represents the unique way a project was defined (like list of targets, tools versions, etc).Checks if a Build Request Can Be Fulfilled from Cache:
Clears Out Stored Outcomes:
ResultsCache can remove all the outcomes it has stored.Prepares Cache Data for Transfer:
ResultsCache can convert the information it stores (either all of it or specific BuildResult records) into a special format.BuildRequestDataFlags Affect CachingWhen MSBuild decides to build something, the request (BuildRequest) carries special instructions called BuildRequestDataFlags. These flags tell MSBuild how to perform the build and what kind of result is expected. The ResultsCache pays close attention to these flags because they are crucial for determining if a cached outcome is truly a match for a new request.
It's an out-in feature that can be disabled with change wave 17.12. See more details in ChangeWaves.md
Here's how some important flags interact with caching:
Flags Affecting Build Behavior: (if any of the flags is presented only in request or cache) cache miss is returned:
IgnoreMissingEmptyAndInvalidImports: This flag changes how MSBuild handles project imports. If a cached outcome was generated with a different setting for this flag than the current request, the cache will consider it a mismatch.FailOnUnresolvedSdk: Similar to the imports flag, this affects SDK resolution.Flags Affecting What's Returned (ProjectInstance State):
ProvideProjectStateAfterBuild: If a build request includes this flag, it means the caller wants the complete ProjectInstance (all evaluated properties, items, etc.) along with the target outcomes.
BuildResult can satisfy this if it also contains the full ProjectInstance (meaning it was originally built with this flag).BuildResult doesn't have the full ProjectInstance, it cannot satisfy a request that demands it, even if the target outcomes match.ProvideSubsetOfStateAfterBuild: This flag is used when the caller only needs specific parts of the ProjectInstance, defined by a RequestedProjectStateFilter.
BuildResult can satisfy this if:
ProjectInstance (the full state naturally includes any subset).ProvideSubsetOfStateAfterBuild, and the subset it contains is a superset of (or identical to) what the current request is asking for.When a BuildResult is stored in the cache, the BuildRequestDataFlags active during its creation are saved with it. During a cache lookup, these stored flags are compared against the flags of the new BuildRequest to ensure compatibility. If the flags indicate a significant difference in how the build was or should be performed, or in what output is expected, a cache miss will occur even if target names and configurations seem to match.
Scheduler: This is the part of MSBuild that decides what to build and when. It communicates frequently with ResultsCache – asking if work can be satisfied from the cache and informing the cache about new outcomes.BuildResult: This is the BuildResult object that ResultsCache stores. ResultsCache uses features of BuildResult to combine outcomes and check if they are still valid.BuildRequest: When MSBuild needs to build something, it creates a BuildRequest. This request tells ResultsCache what to look for (which project setup, which targets, and what kind of outcome, including flags, is needed).