Skip to content

Commit 43ffadf

Browse files
authored
Revert "Populate unproxied targets to avoid dropping requested targets (#9130)" (#9358)
This reverts commit 4256aed. Fixes AB#1906434.
1 parent 08494c7 commit 43ffadf

File tree

6 files changed

+36
-109
lines changed

6 files changed

+36
-109
lines changed

src/Build.UnitTests/BackEnd/Scheduler_Tests.cs

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@
44
using System;
55
using System.Collections.Generic;
66
using System.IO;
7-
using System.Linq;
87
using System.Xml;
98
using Microsoft.Build.BackEnd;
109
using Microsoft.Build.Evaluation;
1110
using Microsoft.Build.Execution;
1211
using Microsoft.Build.Experimental.ProjectCache;
1312
using Microsoft.Build.Framework;
1413
using Microsoft.Build.Shared;
15-
using Microsoft.Build.Unittest;
1614
using Shouldly;
1715
using Xunit;
1816
using TaskItem = Microsoft.Build.Execution.ProjectItemInstance.TaskItem;
@@ -21,6 +19,8 @@
2119

2220
namespace Microsoft.Build.UnitTests.BackEnd
2321
{
22+
using Microsoft.Build.Unittest;
23+
2424
/// <summary>
2525
/// Tests of the scheduler.
2626
/// </summary>
@@ -544,13 +544,7 @@ public void TestProxyAffinityIsInProc()
544544

545545
CreateConfiguration(1, "foo.csproj");
546546

547-
BuildRequest request1 = CreateBuildRequest(
548-
nodeRequestId: 1,
549-
configId: 1,
550-
targets: new[] { "foo" },
551-
NodeAffinity.Any,
552-
parentRequest: null,
553-
new ProxyTargets(new Dictionary<string, string> { { "foo", "bar" } }));
547+
BuildRequest request1 = CreateProxyBuildRequest(1, 1, new ProxyTargets(new Dictionary<string, string> { { "foo", "bar" } }), null);
554548

555549
BuildRequestBlocker blocker = new BuildRequestBlocker(-1, Array.Empty<string>(), new[] { request1 });
556550
List<ScheduleResponse> response = new List<ScheduleResponse>(_scheduler.ReportRequestBlocked(1, blocker));
@@ -812,6 +806,8 @@ private BuildRequest CreateBuildRequest(int nodeRequestId, int configId, string[
812806
/// </summary>
813807
private BuildRequest CreateBuildRequest(int nodeRequestId, int configId, string[] targets, NodeAffinity nodeAffinity, BuildRequest parentRequest, ProxyTargets proxyTargets = null)
814808
{
809+
(targets == null ^ proxyTargets == null).ShouldBeTrue();
810+
815811
HostServices hostServices = null;
816812

817813
if (nodeAffinity != NodeAffinity.Any)
@@ -820,26 +816,36 @@ private BuildRequest CreateBuildRequest(int nodeRequestId, int configId, string[
820816
hostServices.SetNodeAffinity(String.Empty, nodeAffinity);
821817
}
822818

823-
if (proxyTargets != null)
819+
if (targets != null)
824820
{
825-
parentRequest.ShouldBeNull();
826821
return new BuildRequest(
827822
submissionId: 1,
828823
nodeRequestId,
829824
configId,
830-
proxyTargets,
831-
targets.ToList(),
832-
hostServices);
825+
targets,
826+
hostServices,
827+
BuildEventContext.Invalid,
828+
parentRequest);
833829
}
834830

831+
parentRequest.ShouldBeNull();
835832
return new BuildRequest(
836833
submissionId: 1,
837834
nodeRequestId,
838835
configId,
839-
targets,
840-
hostServices,
841-
BuildEventContext.Invalid,
842-
parentRequest);
836+
proxyTargets,
837+
hostServices);
838+
}
839+
840+
private BuildRequest CreateProxyBuildRequest(int nodeRequestId, int configId, ProxyTargets proxyTargets, BuildRequest parentRequest)
841+
{
842+
return CreateBuildRequest(
843+
nodeRequestId,
844+
configId,
845+
null,
846+
NodeAffinity.Any,
847+
parentRequest,
848+
proxyTargets);
843849
}
844850

845851
/// <summary>

src/Build.UnitTests/ProjectCache/ProjectCacheTests.cs

Lines changed: 1 addition & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,7 @@ public void ProjectCacheByBuildParametersAndBottomUpBuildWorks(GraphCacheRespons
507507
}
508508
}
509509

510+
510511
AssertCacheBuild(graph, testData, mockCache, logger, nodesToBuildResults, targets: null);
511512
}
512513

@@ -765,59 +766,6 @@ public void RunningProxyBuildsOnOutOfProcNodesShouldIssueWarning(bool disableInp
765766
logger.AssertMessageCount("MSB4274", 1);
766767
}
767768

768-
// A common scenario is to get a request for N targets, but only some of them can be handled by the cache.
769-
// In this case, missing targets should be passed through.
770-
[Fact]
771-
public async Task PartialProxyTargets()
772-
{
773-
const string ProjectContent = """
774-
<Project>
775-
<Target Name="SomeTarget">
776-
<Message Text="SomeTarget running" />
777-
</Target>
778-
<Target Name="ProxyTarget">
779-
<Message Text="ProxyTarget running" />
780-
</Target>
781-
<Target Name="SomeOtherTarget">
782-
<Message Text="SomeOtherTarget running" />
783-
</Target>
784-
</Project>
785-
""";
786-
TransientTestFile project = _env.CreateFile($"project.proj", ProjectContent);
787-
788-
BuildParameters buildParameters = new()
789-
{
790-
ProjectCacheDescriptor = ProjectCacheDescriptor.FromInstance(
791-
new ConfigurableMockCache
792-
{
793-
GetCacheResultImplementation = (_, _, _) =>
794-
{
795-
return Task.FromResult(
796-
CacheResult.IndicateCacheHit(
797-
new ProxyTargets(
798-
new Dictionary<string, string>
799-
{
800-
{ "ProxyTarget", "SomeTarget" },
801-
})));
802-
}
803-
}),
804-
};
805-
806-
MockLogger logger;
807-
using (Helpers.BuildManagerSession buildSession = new(_env, buildParameters))
808-
{
809-
logger = buildSession.Logger;
810-
BuildResult buildResult = await buildSession.BuildProjectFileAsync(project.Path, new[] { "SomeTarget", "SomeOtherTarget" });
811-
812-
buildResult.Exception.ShouldBeNull();
813-
buildResult.ShouldHaveSucceeded();
814-
}
815-
816-
logger.BuildMessageEvents.Select(i => i.Message).ShouldNotContain("SomeTarget running");
817-
logger.BuildMessageEvents.Select(i => i.Message).ShouldContain("ProxyTarget running");
818-
logger.BuildMessageEvents.Select(i => i.Message).ShouldContain("SomeOtherTarget running");
819-
}
820-
821769
private void AssertCacheBuild(
822770
ProjectGraph graph,
823771
GraphCacheResponse testData,

src/Build/BackEnd/BuildManager/BuildManager.cs

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1783,30 +1783,15 @@ private static void AddBuildRequestToSubmission(BuildSubmission submission, int
17831783

17841784
private static void AddProxyBuildRequestToSubmission(
17851785
BuildSubmission submission,
1786-
BuildRequestConfiguration configuration,
1786+
int configurationId,
17871787
ProxyTargets proxyTargets,
17881788
int projectContextId)
17891789
{
1790-
IReadOnlyDictionary<string, string> realTargetsToProxyTargets = proxyTargets.RealTargetToProxyTargetMap;
1791-
1792-
ICollection<string> requestedTargets = submission.BuildRequestData.TargetNames.Count > 0
1793-
? submission.BuildRequestData.TargetNames
1794-
: configuration.Project.DefaultTargets;
1795-
List<string> targets = new(requestedTargets.Count);
1796-
foreach (string requestedTarget in requestedTargets)
1797-
{
1798-
string effectiveTarget = realTargetsToProxyTargets.TryGetValue(requestedTarget, out string proxyTarget)
1799-
? proxyTarget
1800-
: requestedTarget;
1801-
targets.Add(effectiveTarget);
1802-
}
1803-
18041790
submission.BuildRequest = new BuildRequest(
18051791
submission.SubmissionId,
18061792
BackEnd.BuildRequest.InvalidNodeRequestId,
1807-
configuration.ConfigurationId,
1793+
configurationId,
18081794
proxyTargets,
1809-
targets,
18101795
submission.BuildRequestData.HostServices,
18111796
submission.BuildRequestData.Flags,
18121797
submission.BuildRequestData.RequestedProjectState,
@@ -2309,7 +2294,7 @@ void HandleCacheResult()
23092294
{
23102295
// Setup submission.BuildRequest with proxy targets. The proxy request is built on the inproc node (to avoid
23112296
// ProjectInstance serialization). The proxy target results are used as results for the real targets.
2312-
AddProxyBuildRequestToSubmission(submission, configuration, cacheResult.ProxyTargets, projectContextId);
2297+
AddProxyBuildRequestToSubmission(submission, configuration.ConfigurationId, cacheResult.ProxyTargets, projectContextId);
23132298
IssueBuildRequestForBuildSubmission(submission, configuration, allowMainThreadBuild: false);
23142299
}
23152300
else if (cacheResult.ResultType == CacheResultType.CacheHit && cacheResult.BuildResult != null)

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

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,6 @@ public class ProxyTargets : ITranslatable
2727
/// </summary>
2828
public IReadOnlyDictionary<string, string> ProxyTargetToRealTargetMap => _proxyTargetToRealTargetMap;
2929

30-
internal IReadOnlyDictionary<string, string> RealTargetToProxyTargetMap
31-
{
32-
get
33-
{
34-
// The ProxyTargetToRealTargetMap is "backwards" from how most users would want to use it and doesn't provide as much flexibility as it could if reversed.
35-
// Unfortunately this is part of a public API so cannot easily change at this point.
36-
Dictionary<string, string> realTargetsToProxyTargets = new(ProxyTargetToRealTargetMap.Count, StringComparer.OrdinalIgnoreCase);
37-
foreach (KeyValuePair<string, string> kvp in ProxyTargetToRealTargetMap)
38-
{
39-
// In the case of multiple proxy targets pointing to the same real target, the last one wins. Another awkwardness of ProxyTargetToRealTargetMap being "backwards".
40-
realTargetsToProxyTargets[kvp.Value] = kvp.Key;
41-
}
42-
43-
return realTargetsToProxyTargets;
44-
}
45-
}
46-
4730
private ProxyTargets()
4831
{
4932
}

src/Build/BackEnd/Shared/BuildRequest.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,6 @@ private BuildRequest(
128128
/// <param name="nodeRequestId">The id of the node issuing the request</param>
129129
/// <param name="configurationId">The configuration id to use.</param>
130130
/// <param name="proxyTargets"><see cref="ProxyTargets"/></param>
131-
/// <param name="targets">The set of targets to execute</param>
132131
/// <param name="hostServices">Host services if any. May be null.</param>
133132
/// <param name="buildRequestDataFlags">Additional flags for the request.</param>
134133
/// <param name="requestedProjectState">Filter for desired build results.</param>
@@ -138,15 +137,14 @@ public BuildRequest(
138137
int nodeRequestId,
139138
int configurationId,
140139
ProxyTargets proxyTargets,
141-
List<string> targets,
142140
HostServices hostServices,
143141
BuildRequestDataFlags buildRequestDataFlags = BuildRequestDataFlags.None,
144142
RequestedProjectState requestedProjectState = null,
145143
int projectContextId = BuildEventContext.InvalidProjectContextId)
146144
: this(submissionId, nodeRequestId, configurationId, hostServices, buildRequestDataFlags, requestedProjectState, projectContextId)
147145
{
148146
_proxyTargets = proxyTargets;
149-
_targets = targets;
147+
_targets = proxyTargets.ProxyTargetToRealTargetMap.Keys.ToList();
150148

151149
// Only root requests can have proxy targets.
152150
_parentGlobalRequestId = InvalidGlobalRequestId;

src/Build/BackEnd/Shared/BuildRequestConfiguration.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,13 @@ public List<string> GetTargetsUsedToBuildRequest(BuildRequest request)
745745
ErrorUtilities.VerifyThrow(_projectInitialTargets != null, "Initial targets have not been set.");
746746
ErrorUtilities.VerifyThrow(_projectDefaultTargets != null, "Default targets have not been set.");
747747

748+
if (request.ProxyTargets != null)
749+
{
750+
ErrorUtilities.VerifyThrow(
751+
CollectionHelpers.SetEquivalent(request.Targets, request.ProxyTargets.ProxyTargetToRealTargetMap.Keys),
752+
"Targets must be same as proxy targets");
753+
}
754+
748755
List<string> initialTargets = _projectInitialTargets;
749756
List<string> nonInitialTargets = (request.Targets.Count == 0) ? _projectDefaultTargets : request.Targets;
750757

0 commit comments

Comments
 (0)