Skip to content

Commit 8658fe4

Browse files
authored
Use filtered global properties for node ids (#61)
* Use filtered global properties for node ids * globalProperties -> filteredGlobalProperties
1 parent 79afdf8 commit 8658fe4

File tree

5 files changed

+28
-20
lines changed

5 files changed

+28
-20
lines changed

src/Common/Fingerprinting/FingerprintFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ void AddSettingToFingerprint(IReadOnlyCollection<Glob>? patterns, string setting
8686
CreateFingerprintEntry(nodeContext.ProjectFileRelativePath)
8787
};
8888

89-
foreach (KeyValuePair<string, string> property in nodeContext.GlobalProperties)
89+
foreach (KeyValuePair<string, string> property in nodeContext.FilteredGlobalProperties)
9090
{
9191
entries.Add(CreateFingerprintEntry($"{property.Key}={property.Value}"));
9292
}

src/Common/MSBuildCachePluginBase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ private async Task BeginBuildInnerAsync(CacheContext context, PluginLoggerBase l
298298
}
299299

300300
NodeDescriptor nodeDescriptor = _nodeDescriptorFactory.Create(node.ProjectInstance);
301-
NodeContext nodeContext = new(Settings.LogDirectory, node, parserInfo.ProjectFileRelativePath, nodeDescriptor.GlobalProperties, inputs, targetNames);
301+
NodeContext nodeContext = new(Settings.LogDirectory, node, parserInfo.ProjectFileRelativePath, nodeDescriptor.FilteredGlobalProperties, inputs, targetNames);
302302

303303
dumpParserInfoTasks.Add(Task.Run(() => DumpParserInfoAsync(logger, nodeContext, parserInfo), cancellationToken));
304304
nodeContexts.Add(nodeDescriptor, nodeContext);
@@ -733,7 +733,7 @@ async Task DumpNodeContextAsync(NodeContext nodeContext)
733733
jsonWriter.WriteString("projectFileRelativePath", nodeContext.ProjectFileRelativePath);
734734

735735
jsonWriter.WriteStartObject("globalProperties");
736-
foreach (KeyValuePair<string, string> kvp in nodeContext.GlobalProperties)
736+
foreach (KeyValuePair<string, string> kvp in nodeContext.FilteredGlobalProperties)
737737
{
738738
jsonWriter.WriteString(kvp.Key, kvp.Value);
739739
}

src/Common/NodeContext.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ public NodeContext(
2222
string baseLogDirectory,
2323
ProjectGraphNode node,
2424
string projectFileRelativePath,
25-
IReadOnlyDictionary<string, string> globalProperties,
25+
IReadOnlyDictionary<string, string> filteredGlobalProperties,
2626
IReadOnlyList<string> inputs,
2727
HashSet<string> targetNames)
2828
{
29-
Id = GenerateId(projectFileRelativePath, node);
29+
Id = GenerateId(projectFileRelativePath, filteredGlobalProperties);
3030
_logDirectory = Path.Combine(baseLogDirectory, Id);
3131
Node = node;
3232
ProjectFileRelativePath = projectFileRelativePath;
33-
GlobalProperties = globalProperties;
33+
FilteredGlobalProperties = filteredGlobalProperties;
3434
Inputs = inputs;
3535
TargetNames = targetNames;
3636
}
@@ -56,7 +56,7 @@ public string LogDirectory
5656

5757
public string ProjectFileRelativePath { get; }
5858

59-
public IReadOnlyDictionary<string, string> GlobalProperties { get; }
59+
public IReadOnlyDictionary<string, string> FilteredGlobalProperties { get; }
6060

6161
public IReadOnlyList<string> Inputs { get; }
6262

@@ -85,9 +85,17 @@ public void SetBuildResult(NodeBuildResult buildResult)
8585
/// <summary>
8686
/// Generate a stable Id which we can use for sorting and comparison purposes across builds.
8787
/// </summary>
88-
private static string GenerateId(string projectFileRelativePath, ProjectGraphNode node)
88+
private static string GenerateId(string projectFileRelativePath, IReadOnlyDictionary<string, string> filteredGlobalProperties)
8989
{
90-
SortedDictionary<string, string> sortedProperties = new(node.ProjectInstance.GlobalProperties, StringComparer.OrdinalIgnoreCase);
90+
// In practice, the dictionary we're given is SortedDictionary<string, string>, so try casting.
91+
if (filteredGlobalProperties is not SortedDictionary<string, string> sortedProperties)
92+
{
93+
sortedProperties = new(StringComparer.OrdinalIgnoreCase);
94+
foreach (KeyValuePair<string, string> kvp in filteredGlobalProperties)
95+
{
96+
sortedProperties.Add(kvp.Key, kvp.Value);
97+
}
98+
}
9199

92100
#pragma warning disable CA5351 // Do Not Use Broken Cryptographic Algorithms. This is not used for crypto.
93101
using MD5 hasher = MD5.Create();

src/Common/NodeDescriptor.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@ namespace Microsoft.MSBuildCache;
1010
{
1111
private readonly string _projectFullPath;
1212

13-
private readonly SortedDictionary<string, string> _globalProperties;
13+
private readonly SortedDictionary<string, string> _filteredGlobalProperties;
1414

15-
public NodeDescriptor(string projectFullPath, SortedDictionary<string, string> globalProperties)
15+
public NodeDescriptor(string projectFullPath, SortedDictionary<string, string> filteredGlobalProperties)
1616
{
1717
_projectFullPath = projectFullPath;
18-
_globalProperties = globalProperties;
18+
_filteredGlobalProperties = filteredGlobalProperties;
1919
}
2020

2121
/// <summary>
2222
/// Sorted by StringComparison.OrdinalIgnoreCase.
2323
/// </summary>
24-
public IReadOnlyDictionary<string, string> GlobalProperties => _globalProperties;
24+
public IReadOnlyDictionary<string, string> FilteredGlobalProperties => _filteredGlobalProperties;
2525

2626
public bool Equals(NodeDescriptor other)
2727
{
@@ -30,14 +30,14 @@ public bool Equals(NodeDescriptor other)
3030
return false;
3131
}
3232

33-
if (_globalProperties.Count != other._globalProperties.Count)
33+
if (_filteredGlobalProperties.Count != other._filteredGlobalProperties.Count)
3434
{
3535
return false;
3636
}
3737

38-
foreach (KeyValuePair<string, string> kvp in _globalProperties)
38+
foreach (KeyValuePair<string, string> kvp in _filteredGlobalProperties)
3939
{
40-
if (!other._globalProperties.TryGetValue(kvp.Key, out string? otherValue))
40+
if (!other._filteredGlobalProperties.TryGetValue(kvp.Key, out string? otherValue))
4141
{
4242
return false;
4343
}
@@ -62,7 +62,7 @@ public override int GetHashCode()
6262
HashCode hashCode = new();
6363
hashCode.Add(_projectFullPath, StringComparer.OrdinalIgnoreCase);
6464

65-
foreach (KeyValuePair<string, string> kvp in _globalProperties)
65+
foreach (KeyValuePair<string, string> kvp in _filteredGlobalProperties)
6666
{
6767
hashCode.Add(kvp.Key, StringComparer.OrdinalIgnoreCase);
6868
hashCode.Add(kvp.Value, StringComparer.OrdinalIgnoreCase);

src/Common/NodeDescriptorFactory.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public NodeDescriptor Create(string projectFullPath, IReadOnlyDictionary<string,
2929
{
3030
// Sort to ensure a consistent hash for equivalent sets of properties.
3131
// This allocates with the assumption that the hash code is used multiple times.
32-
SortedDictionary<string, string> sortedGlobalProperties = new(StringComparer.OrdinalIgnoreCase);
32+
SortedDictionary<string, string> filteredGlobalProperties = new(StringComparer.OrdinalIgnoreCase);
3333
foreach (KeyValuePair<string, string> kvp in globalProperties)
3434
{
3535
if (kvp.Key.StartsWith(MSBuildProjectInstancePrefix, StringComparison.OrdinalIgnoreCase))
@@ -42,10 +42,10 @@ public NodeDescriptor Create(string projectFullPath, IReadOnlyDictionary<string,
4242
continue;
4343
}
4444

45-
sortedGlobalProperties.Add(kvp.Key, kvp.Value);
45+
filteredGlobalProperties.Add(kvp.Key, kvp.Value);
4646
}
4747

48-
return new NodeDescriptor(projectFullPath, sortedGlobalProperties);
48+
return new NodeDescriptor(projectFullPath, filteredGlobalProperties);
4949
}
5050

5151
// Adapts IDictionary to IReadOnlyDictionary

0 commit comments

Comments
 (0)