Skip to content

Commit c67ce32

Browse files
authored
Merge pull request #9611 from dotnet/backport/pr-9569-to-vs17.9
[vs17.9] Add requested targets to CacheContext
2 parents bb6fadf + 62eecc5 commit c67ce32

File tree

4 files changed

+32
-7
lines changed

4 files changed

+32
-7
lines changed

eng/Versions.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<!-- Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the MIT license. See License.txt in the project root for full license information. -->
33
<Project>
44
<PropertyGroup>
5-
<VersionPrefix>17.9.2</VersionPrefix>
5+
<VersionPrefix>17.9.3</VersionPrefix>
66
<DotNetFinalVersionKind>release</DotNetFinalVersionKind>
77
<PackageValidationBaselineVersion>17.8.3</PackageValidationBaselineVersion>
88
<AssemblyVersion>15.1.0.0</AssemblyVersion>

src/Build/BackEnd/BuildManager/BuildManager.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1332,6 +1332,7 @@ internal void ExecuteSubmission(BuildSubmission submission, bool allowMainThread
13321332
_projectCacheService.InitializePluginsForVsScenario(
13331333
ProjectCacheDescriptors.Values,
13341334
resolvedConfiguration,
1335+
submission.BuildRequestData.TargetNames,
13351336
_executionCancellationTokenSource.Token);
13361337
}
13371338

@@ -1953,7 +1954,7 @@ private void ExecuteGraphBuildScheduler(GraphBuildSubmission submission)
19531954

19541955
if (submission.BuildRequestData.GraphBuildOptions.Build)
19551956
{
1956-
_projectCacheService.InitializePluginsForGraph(projectGraph, _executionCancellationTokenSource.Token);
1957+
_projectCacheService.InitializePluginsForGraph(projectGraph, submission.BuildRequestData.TargetNames, _executionCancellationTokenSource.Token);
19571958

19581959
var targetListTask = projectGraph.GetTargetLists(submission.BuildRequestData.TargetNames);
19591960

src/Build/BackEnd/Components/ProjectCache/CacheContext.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System;
45
using System.Collections.Generic;
56
using Microsoft.Build.FileSystem;
67
using Microsoft.Build.Graph;
@@ -22,12 +23,23 @@ public class CacheContext
2223
public IReadOnlyCollection<ProjectGraphEntryPoint>? GraphEntryPoints { get; }
2324
public string? MSBuildExePath { get; }
2425
public MSBuildFileSystemBase FileSystem { get; }
26+
public IReadOnlyCollection<string> RequestedTargets { get; }
2527

2628
public CacheContext(
2729
IReadOnlyDictionary<string, string> pluginSettings,
2830
MSBuildFileSystemBase fileSystem,
2931
ProjectGraph? graph = null,
3032
IReadOnlyCollection<ProjectGraphEntryPoint>? graphEntryPoints = null)
33+
: this(pluginSettings, fileSystem, requestedTargets: Array.Empty<string>(), graph, graphEntryPoints)
34+
{
35+
}
36+
37+
public CacheContext(
38+
IReadOnlyDictionary<string, string> pluginSettings,
39+
MSBuildFileSystemBase fileSystem,
40+
IReadOnlyCollection<string> requestedTargets,
41+
ProjectGraph? graph = null,
42+
IReadOnlyCollection<ProjectGraphEntryPoint>? graphEntryPoints = null)
3143
{
3244
ErrorUtilities.VerifyThrow(
3345
(graph != null) ^ (graphEntryPoints != null),
@@ -38,6 +50,7 @@ public CacheContext(
3850
GraphEntryPoints = graphEntryPoints;
3951
MSBuildExePath = BuildEnvironmentHelper.Instance.CurrentMSBuildExePath;
4052
FileSystem = fileSystem;
53+
RequestedTargets = requestedTargets;
4154
}
4255
}
4356
}

src/Build/BackEnd/Components/ProjectCache/ProjectCacheService.cs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,10 @@ public ProjectCacheService(
9595
/// <summary>
9696
/// Optimization which frontloads plugin initialization since we have an entire graph.
9797
/// </summary>
98-
public void InitializePluginsForGraph(ProjectGraph projectGraph, CancellationToken cancellationToken)
98+
public void InitializePluginsForGraph(
99+
ProjectGraph projectGraph,
100+
ICollection<string> requestedTargets,
101+
CancellationToken cancellationToken)
99102
{
100103
EnsureNotDisposed();
101104

@@ -111,7 +114,7 @@ public void InitializePluginsForGraph(ProjectGraph projectGraph, CancellationTok
111114
foreach (ProjectCacheDescriptor projectCacheDescriptor in GetProjectCacheDescriptors(node.ProjectInstance))
112115
{
113116
// Intentionally fire-and-forget to asynchronously initialize the plugin. Any exceptions will bubble up later when querying.
114-
_ = GetProjectCachePluginAsync(projectCacheDescriptor, projectGraph, buildRequestConfiguration: null, cancellationToken)
117+
_ = GetProjectCachePluginAsync(projectCacheDescriptor, projectGraph, buildRequestConfiguration: null, requestedTargets, cancellationToken)
115118
.ContinueWith(t => { }, TaskContinuationOptions.ExecuteSynchronously | TaskContinuationOptions.OnlyOnFaulted);
116119
}
117120
});
@@ -122,6 +125,7 @@ public void InitializePluginsForGraph(ProjectGraph projectGraph, CancellationTok
122125
public void InitializePluginsForVsScenario(
123126
IEnumerable<ProjectCacheDescriptor> projectCacheDescriptors,
124127
BuildRequestConfiguration buildRequestConfiguration,
128+
ICollection<string> requestedTargets,
125129
CancellationToken cancellationToken)
126130
{
127131
EnsureNotDisposed();
@@ -144,7 +148,7 @@ public void InitializePluginsForVsScenario(
144148
projectCacheDescriptor =>
145149
{
146150
// Intentionally fire-and-forget to asynchronously initialize the plugin. Any exceptions will bubble up later when querying.
147-
_ = GetProjectCachePluginAsync(projectCacheDescriptor, projectGraph: null, buildRequestConfiguration, cancellationToken)
151+
_ = GetProjectCachePluginAsync(projectCacheDescriptor, projectGraph: null, buildRequestConfiguration, requestedTargets, cancellationToken)
148152
.ContinueWith(t => { }, TaskContinuationOptions.ExecuteSynchronously | TaskContinuationOptions.OnlyOnFaulted);
149153
});
150154
},
@@ -155,12 +159,13 @@ private Task<ProjectCachePlugin> GetProjectCachePluginAsync(
155159
ProjectCacheDescriptor projectCacheDescriptor,
156160
ProjectGraph? projectGraph,
157161
BuildRequestConfiguration? buildRequestConfiguration,
162+
ICollection<string> requestedTargets,
158163
CancellationToken cancellationToken)
159164
=> _projectCachePlugins.GetOrAdd(
160165
projectCacheDescriptor,
161166
// The use of Lazy is because ConcurrentDictionary doesn't guarantee the value factory executes only once if there are multiple simultaneous callers,
162167
// so this ensures that CreateAndInitializePluginAsync is only called exactly once.
163-
descriptor => new Lazy<Task<ProjectCachePlugin>>(() => CreateAndInitializePluginAsync(descriptor, projectGraph, buildRequestConfiguration, cancellationToken)))
168+
descriptor => new Lazy<Task<ProjectCachePlugin>>(() => CreateAndInitializePluginAsync(descriptor, projectGraph, buildRequestConfiguration, requestedTargets, cancellationToken)))
164169
.Value;
165170

166171
private IEnumerable<ProjectCacheDescriptor> GetProjectCacheDescriptors(ProjectInstance projectInstance)
@@ -189,6 +194,7 @@ private async Task<ProjectCachePlugin> CreateAndInitializePluginAsync(
189194
ProjectCacheDescriptor projectCacheDescriptor,
190195
ProjectGraph? projectGraph,
191196
BuildRequestConfiguration? buildRequestConfiguration,
197+
ICollection<string> requestedTargets,
192198
CancellationToken cancellationToken)
193199
{
194200
BuildEventContext buildEventContext = BuildEventContext.Invalid;
@@ -241,6 +247,9 @@ private async Task<ProjectCachePlugin> CreateAndInitializePluginAsync(
241247
? GetGraphEntryPoints(buildRequestConfiguration)
242248
: null;
243249

250+
// In practice, the underlying type of the ICollection is a List<string> so attempt to cast first
251+
IReadOnlyList<string> requestedTargetsList = requestedTargets as List<string> ?? requestedTargets.ToList();
252+
244253
_loggingService.LogComment(buildEventContext, MessageImportance.High, "LoadingProjectCachePlugin", pluginTypeName);
245254
MSBuildEventSource.Log.ProjectCacheBeginBuildStart(pluginTypeName);
246255

@@ -250,6 +259,7 @@ await pluginInstance.BeginBuildAsync(
250259
new CacheContext(
251260
projectCacheDescriptor.PluginSettings,
252261
DefaultMSBuildFileSystem.Instance,
262+
requestedTargetsList,
253263
projectGraph,
254264
graphEntryPoints),
255265
pluginLogger,
@@ -517,7 +527,8 @@ private async Task<CacheResult> GetCacheResultAsync(BuildRequestData buildReques
517527
continue;
518528
}
519529

520-
ProjectCachePlugin plugin = await GetProjectCachePluginAsync(projectCacheDescriptor, projectGraph: null, buildRequestConfiguration, cancellationToken);
530+
ICollection<string> requestedTargetsList = buildRequestConfiguration.RequestedTargets as ICollection<string> ?? buildRequestConfiguration.RequestedTargets.ToList();
531+
ProjectCachePlugin plugin = await GetProjectCachePluginAsync(projectCacheDescriptor, projectGraph: null, buildRequestConfiguration, requestedTargetsList, cancellationToken);
521532
try
522533
{
523534
// Rethrow any initialization exception.

0 commit comments

Comments
 (0)