Skip to content

Commit

Permalink
Tolerate dependencies outside the repo when adding to cache (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
dfederm authored Aug 20, 2024
1 parent c840f40 commit f4bac71
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
10 changes: 9 additions & 1 deletion src/Common/Fingerprinting/FingerprintFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,22 @@ public sealed class FingerprintFactory : IFingerprintFactory
private readonly List<FingerprintEntry> _pluginSettingsFingerprintEntries;
private readonly PluginSettings _pluginSettings;
private readonly PathNormalizer _pathNormalizer;
private readonly string _repoRoot;

public FingerprintFactory(
IContentHasher contentHasher,
IInputHasher inputHasher,
INodeContextRepository nodeRepository,
PluginSettings pluginSettings,
PathNormalizer pathNormalizer)
PathNormalizer pathNormalizer,
string repoRoot)
{
_contentHasher = contentHasher;
_inputHasher = inputHasher;
_nodeContextRepository = nodeRepository;
_pluginSettings = pluginSettings;
_pathNormalizer = pathNormalizer;
_repoRoot = repoRoot;

_pluginSettingsFingerprintEntries = new List<FingerprintEntry>()
{
Expand Down Expand Up @@ -119,6 +122,11 @@ void AddSettingToFingerprint(IReadOnlyCollection<Glob>? patterns, string setting
{
if (!_nodeContextRepository.TryGetNodeContext(dependencyNode.ProjectInstance, out NodeContext? dependency))
{
if (!dependencyNode.ProjectInstance.FullPath.IsUnderDirectory(_repoRoot))
{
continue;
}

return null;
}

Expand Down
14 changes: 12 additions & 2 deletions src/Common/MSBuildCachePluginBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,13 @@ protected virtual IFingerprintFactory CreateFingerprintFactory()
|| InputHasher == null
|| _nodeContextRepository == null
|| Settings == null
|| _pathNormalizer == null)
|| _pathNormalizer == null
|| _repoRoot == null)
{
throw new InvalidOperationException();
}

return new FingerprintFactory(ContentHasher, InputHasher, _nodeContextRepository, Settings, _pathNormalizer);
return new FingerprintFactory(ContentHasher, InputHasher, _nodeContextRepository, Settings, _pathNormalizer, _repoRoot);
}

protected abstract Task<ICacheClient> CreateCacheClientAsync(PluginLoggerBase logger, CancellationToken cancellationToken);
Expand Down Expand Up @@ -442,6 +443,15 @@ private async Task HandleProjectFinishedInnerAsync(FileAccessContext fileAccessC
{
if (!_nodeContextRepository.TryGetNodeContext(dependencyNode.ProjectInstance, out NodeContext? dependencyNodeContext))
{
// If the dependency is outside the repository, we exclude it (see Parser) so we won't ever find a NodeContext.
// The choices at this point are to either to return and not add cached outputs (this project is not cacheable),
// or to just ignore the project and risk an under-build. We're choosing to accept the risk since we've already
// accepted it for input files outside the repo during fingerprinting.
if (!dependencyNode.ProjectInstance.FullPath.IsUnderDirectory(_repoRoot))
{
continue;
}

return;
}

Expand Down

0 comments on commit f4bac71

Please sign in to comment.