-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
TargetBuilder.cs
859 lines (738 loc) · 44.7 KB
/
TargetBuilder.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using Microsoft.Build.Collections;
using Microsoft.Build.Eventing;
using Microsoft.Build.Execution;
using Microsoft.Build.Framework;
using Microsoft.Build.Shared;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using ProjectLoggingContext = Microsoft.Build.BackEnd.Logging.ProjectLoggingContext;
using ElementLocation = Microsoft.Build.Construction.ElementLocation;
using BuildAbortedException = Microsoft.Build.Exceptions.BuildAbortedException;
using TaskItem = Microsoft.Build.Execution.ProjectItemInstance.TaskItem;
namespace Microsoft.Build.BackEnd
{
/// <summary>
/// The Target Builder is responsible for building a single target within a given project.
/// </summary>
/// <remarks>
/// The Target Builder is a stack machine which builds project targets. Each time a target needs to be built, it is
/// pushed onto the stack. The main loop for the Target Builder simply evaluates the top item on the stack to determine
/// which action to take. These actions comprise the target state machine, as represented by the states of the
/// TargetEntry object.
///
/// When a target completes, all of its outputs are available in the Lookup contained in the TargetEntry. In fact, everything that it changed
/// in the global state is available by virtue of its Lookup being merged with the current Target's lookup.
///
/// For CallTarget tasks, this behavior is not the same. Rather the Lookup from a CallTarget call does not get merged until the calling
/// Target has completed. This is considered erroneous behavior and 'normal' version of CallTarget will be implemented which does not exhibit
/// this.
/// </remarks>
internal class TargetBuilder : ITargetBuilder, ITargetBuilderCallback, IBuildComponent
{
/// <summary>
/// The cancellation token.
/// </summary>
private CancellationToken _cancellationToken;
/// <summary>
/// The current stack of targets and dependents. The top-most entry on the stack is the target
/// currently being built.
/// </summary>
private ConcurrentStack<TargetEntry> _targetsToBuild;
/// <summary>
/// The component host.
/// </summary>
private IBuildComponentHost _componentHost;
/// <summary>
/// The BuildRequestEntry for which we are building targets.
/// </summary>
private BuildRequestEntry _requestEntry;
/// <summary>
/// The lookup representing the project's state.
/// </summary>
private Lookup _baseLookup;
/// <summary>
/// The callback interface used to invoke new project builds.
/// </summary>
private IRequestBuilderCallback _requestBuilderCallback;
/// <summary>
/// The project logging context
/// </summary>
private ProjectLoggingContext _projectLoggingContext;
/// <summary>
/// The aggregate build result from running the targets
/// </summary>
private BuildResult _buildResult;
/// <summary>
/// The project instance we are building
/// </summary>
private ProjectInstance _projectInstance;
/// <summary>
/// Flag indicating whether we are under the influence of the legacy CallTarget's ContinueOnError behavior.
/// </summary>
private bool _legacyCallTargetContinueOnError;
/// <summary>
/// Builds the specified targets.
/// </summary>
/// <param name="loggingContext">The logging context for the project.</param>
/// <param name="entry">The BuildRequestEntry for which we are building targets.</param>
/// <param name="callback">The callback to be used to handle new project build requests.</param>
/// <param name="targetNames">The names of the targets to build.</param>
/// <param name="baseLookup">The Lookup containing all current items and properties for this target.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to use when building the targets.</param>
/// <returns>The target's outputs and result codes</returns>
public async Task<BuildResult> BuildTargets(ProjectLoggingContext loggingContext, BuildRequestEntry entry, IRequestBuilderCallback callback, string[] targetNames, Lookup baseLookup, CancellationToken cancellationToken)
{
ErrorUtilities.VerifyThrowArgumentNull(loggingContext, "projectLoggingContext");
ErrorUtilities.VerifyThrowArgumentNull(entry, nameof(entry));
ErrorUtilities.VerifyThrowArgumentNull(callback, "requestBuilderCallback");
ErrorUtilities.VerifyThrowArgumentNull(targetNames, nameof(targetNames));
ErrorUtilities.VerifyThrowArgumentNull(baseLookup, nameof(baseLookup));
ErrorUtilities.VerifyThrow(targetNames.Length > 0, "List of targets must be non-empty");
ErrorUtilities.VerifyThrow(_componentHost != null, "InitializeComponent must be called before building targets.");
_requestEntry = entry;
_requestBuilderCallback = callback;
_projectLoggingContext = loggingContext;
_cancellationToken = cancellationToken;
// Clone the base lookup so that if we are re-entered by another request while this one in blocked, we don't have visibility to
// their state, and they have no visibility into ours.
_baseLookup = baseLookup.Clone();
_targetsToBuild = new ConcurrentStack<TargetEntry>();
// Get the actual target objects from the names
BuildRequestConfiguration configuration = _requestEntry.RequestConfiguration;
bool previousCacheableStatus = configuration.IsCacheable;
configuration.IsCacheable = false;
configuration.RetrieveFromCache();
_projectInstance = configuration.Project;
// Now get the current results cache entry.
IResultsCache resultsCache = (IResultsCache)_componentHost.GetComponent(BuildComponentType.ResultsCache);
BuildResult existingBuildResult = resultsCache.GetResultsForConfiguration(_requestEntry.Request.ConfigurationId);
_buildResult = new BuildResult(entry.Request, existingBuildResult, null);
if (existingBuildResult == null)
{
// Add this result so that if our project gets re-entered we won't rebuild any targets we have already built.
resultsCache.AddResult(_buildResult);
}
List<TargetSpecification> targets = new List<TargetSpecification>(targetNames.Length);
foreach (string targetName in targetNames)
{
var targetExists = _projectInstance.Targets.TryGetValue(targetName, out ProjectTargetInstance targetInstance);
// Ignore the missing target if:
// SkipNonexistentTargets is set
// -or-
// SkipNonexistentNonTopLevelTargets and the target is is not a top level target
if (!targetExists
&& entry.Request.BuildRequestDataFlags.HasFlag(BuildRequestDataFlags.SkipNonexistentTargets)
|| entry.Request.BuildRequestDataFlags.HasFlag(BuildRequestDataFlags.SkipNonexistentNonTopLevelTargets) && !entry.Request.Targets.Contains(targetName))
{
_projectLoggingContext.LogComment(Framework.MessageImportance.Low,
"TargetSkippedWhenSkipNonexistentTargets", targetName);
}
else
{
targets.Add(new TargetSpecification(targetName, targetExists ? targetInstance.Location : _projectInstance.ProjectFileLocation));
}
}
// Push targets onto the stack. This method will reverse their push order so that they
// get built in the same order specified in the array.
await PushTargets(targets, null, baseLookup, false, false, TargetBuiltReason.None);
// Now process the targets
ITaskBuilder taskBuilder = _componentHost.GetComponent(BuildComponentType.TaskBuilder) as ITaskBuilder;
try
{
await ProcessTargetStack(taskBuilder);
}
finally
{
// If there are still targets left on the stack, they need to be removed from the 'active targets' list
foreach (TargetEntry target in _targetsToBuild)
{
configuration.ActivelyBuildingTargets.Remove(target.Name);
}
((IBuildComponent)taskBuilder).ShutdownComponent();
}
if (_cancellationToken.IsCancellationRequested)
{
throw new BuildAbortedException();
}
// Gather up outputs for the requested targets and return those. All of our information should be in the base lookup now.
ComputeAfterTargetFailures(targetNames);
BuildResult resultsToReport = new BuildResult(_buildResult, targetNames);
// Return after-build project state if requested.
if (_requestEntry.Request.BuildRequestDataFlags.HasFlag(BuildRequestDataFlags.ProvideProjectStateAfterBuild))
{
resultsToReport.ProjectStateAfterBuild = _projectInstance;
}
if (_requestEntry.Request.RequestedProjectState != null)
{
resultsToReport.ProjectStateAfterBuild =
_projectInstance.FilteredCopy(_requestEntry.Request.RequestedProjectState);
}
configuration.IsCacheable = previousCacheableStatus;
return resultsToReport;
}
#region IBuildComponent Members
/// <summary>
/// Sets the component host.
/// </summary>
/// <param name="host">The component host.</param>
public void InitializeComponent(IBuildComponentHost host)
{
ErrorUtilities.VerifyThrowArgumentNull(host, nameof(host));
_componentHost = host;
}
/// <summary>
/// Shuts down the component.
/// </summary>
public void ShutdownComponent()
{
_componentHost = null;
}
#endregion
#region ITargetBuilderCallback Members
/// <summary>
/// Invokes the specified targets using Dev9 behavior.
/// </summary>
/// <param name="targets">The targets to build.</param>
/// <param name="continueOnError">True to continue building the remaining targets if one fails.</param>
/// <param name="taskLocation">The <see cref="ElementLocation"/> of the task.</param>
/// <returns>The results for each target.</returns>
/// <remarks>
/// Dev9 behavior refers to the following:
/// 1. The changes made during the calling target up to this point are NOT visible to this target.
/// 2. The changes made by this target are NOT visible to the calling target.
/// 3. Changes made by the calling target OVERRIDE changes made by this target.
/// </remarks>
async Task<ITargetResult[]> ITargetBuilderCallback.LegacyCallTarget(string[] targets, bool continueOnError, ElementLocation taskLocation)
{
List<TargetSpecification> targetToPush = new List<TargetSpecification>();
ITargetResult[] results = new TargetResult[targets.Length];
bool originalLegacyCallTargetContinueOnError = _legacyCallTargetContinueOnError;
_legacyCallTargetContinueOnError = _legacyCallTargetContinueOnError || continueOnError;
// Our lookup is the one used at the beginning of the calling target.
Lookup callTargetLookup = _baseLookup;
// We now record this lookup in the calling target's entry so that it may
// leave the scope just before it commits its own changes to the base lookup.
TargetEntry currentTargetEntry = _targetsToBuild.Peek();
currentTargetEntry.EnterLegacyCallTargetScope(callTargetLookup);
ITaskBuilder taskBuilder = _componentHost.GetComponent(BuildComponentType.TaskBuilder) as ITaskBuilder;
try
{
// Flag set to true if one of the targets we call fails.
bool errorResult = false;
// Now walk through the list of targets, invoking each one.
for (int i = 0; i < targets.Length; i++)
{
if (_cancellationToken.IsCancellationRequested || errorResult)
{
results[i] = new TargetResult(Array.Empty<TaskItem>(), new WorkUnitResult(WorkUnitResultCode.Skipped, WorkUnitActionCode.Continue, null));
}
else
{
targetToPush.Clear();
targetToPush.Add(new TargetSpecification(targets[i], taskLocation));
// We push the targets one at a time to emulate the original CallTarget behavior.
bool pushed = await PushTargets(targetToPush, currentTargetEntry, callTargetLookup, false, true, TargetBuiltReason.None);
ErrorUtilities.VerifyThrow(pushed, "Failed to push any targets onto the stack. Target: {0} Current Target: {1}", targets[i], currentTargetEntry.Target.Name);
await ProcessTargetStack(taskBuilder);
if (!_cancellationToken.IsCancellationRequested)
{
results[i] = _buildResult[targets[i]];
if (results[i].ResultCode == TargetResultCode.Failure)
{
errorResult = true;
}
}
else
{
results[i] = new TargetResult(Array.Empty<TaskItem>(), new WorkUnitResult(WorkUnitResultCode.Skipped, WorkUnitActionCode.Continue, null));
}
}
}
}
finally
{
// Restore the state of the TargetBuilder to that it was prior to the CallTarget call.
// Any targets we have pushed on at this point we need to get rid of since we aren't going to process them.
// If there were normal task errors, standard error handling semantics would have taken care of them.
// If there was an exception, such as a circular dependency error, items may still be on the stack so we must clear them.
while (!Object.ReferenceEquals(_targetsToBuild.Peek(), currentTargetEntry))
{
_targetsToBuild.Pop();
}
_legacyCallTargetContinueOnError = originalLegacyCallTargetContinueOnError;
((IBuildComponent)taskBuilder).ShutdownComponent();
}
return results;
}
#endregion
#region IRequestBuilderCallback Members
/// <summary>
/// Forwarding implementation of BuildProjects
/// </summary>
async Task<BuildResult[]> IRequestBuilderCallback.BuildProjects(string[] projectFiles, Microsoft.Build.Collections.PropertyDictionary<ProjectPropertyInstance>[] properties, string[] toolsVersions, string[] targets, bool waitForResults, bool skipNonexistentTargets)
{
return await _requestBuilderCallback.BuildProjects(projectFiles, properties, toolsVersions, targets, waitForResults, skipNonexistentTargets);
}
/// <summary>
/// Required for interface - this should never be called.
/// </summary>
Task IRequestBuilderCallback.BlockOnTargetInProgress(int blockingGlobalBuildRequestId, string blockingTarget, BuildResult partialBuildResult)
{
ErrorUtilities.ThrowInternalError("This method should never be called by anyone except the TargetBuilder.");
return Task.FromResult(false);
}
/// <summary>
/// Yields the node.
/// </summary>
void IRequestBuilderCallback.Yield()
{
_requestBuilderCallback.Yield();
}
/// <summary>
/// Reacquires the node.
/// </summary>
void IRequestBuilderCallback.Reacquire()
{
_requestBuilderCallback.Reacquire();
}
/// <summary>
/// Enters the MSBuild callback state for asynchronous processing of referenced projects.
/// </summary>
void IRequestBuilderCallback.EnterMSBuildCallbackState()
{
_requestBuilderCallback.EnterMSBuildCallbackState();
}
/// <summary>
/// Exits the MSBuild callback state.
/// </summary>
void IRequestBuilderCallback.ExitMSBuildCallbackState()
{
_requestBuilderCallback.ExitMSBuildCallbackState();
}
/// <summary>
/// Requests CPU resources from the scheduler.
/// </summary>
/// <remarks>This method is called from the <see cref="TaskHost"/>.</remarks>
int IRequestBuilderCallback.RequestCores(object monitorLockObject, int requestedCores, bool waitForCores)
{
return _requestBuilderCallback.RequestCores(monitorLockObject, requestedCores, waitForCores);
}
/// <summary>
/// Returns CPU resources to the scheduler.
/// </summary>
/// <remarks>This method is called from the <see cref="TaskHost"/>.</remarks>
void IRequestBuilderCallback.ReleaseCores(int coresToRelease)
{
_requestBuilderCallback.ReleaseCores(coresToRelease);
}
#endregion
/// <summary>
/// Class factory for component creation.
/// </summary>
internal static IBuildComponent CreateComponent(BuildComponentType type)
{
ErrorUtilities.VerifyThrow(type == BuildComponentType.TargetBuilder, "Cannot create components of type {0}", type);
return new TargetBuilder();
}
/// <summary>
/// Processes the target stack until its empty or we hit a recursive break (due to CallTarget etc.)
/// </summary>
private async Task ProcessTargetStack(ITaskBuilder taskBuilder)
{
// Keep building while we have targets to build and haven't been canceled.
bool stopProcessingStack = false;
while
(
!_cancellationToken.IsCancellationRequested &&
!stopProcessingStack &&
!_targetsToBuild.IsEmpty
)
{
TargetEntry currentTargetEntry = _targetsToBuild.Peek();
switch (currentTargetEntry.State)
{
case TargetEntryState.Dependencies:
// Ensure we are dealing with a target which actually exists.
ProjectErrorUtilities.VerifyThrowInvalidProject
(
_requestEntry.RequestConfiguration.Project.Targets.ContainsKey(currentTargetEntry.Name),
currentTargetEntry.ReferenceLocation,
"TargetDoesNotExist",
currentTargetEntry.Name
);
// If we already have results for this target which were not skipped, we can ignore it. In
// addition, we can also ignore its before and after targets -- if this target has already run,
// then so have they.
if (!CheckSkipTarget(ref stopProcessingStack, currentTargetEntry))
{
// Temporarily remove this entry so we can push our after targets
_targetsToBuild.Pop();
// Push our after targets, if any. Our parent is the parent of the target after which we are running.
IList<TargetSpecification> afterTargets = _requestEntry.RequestConfiguration.Project.GetTargetsWhichRunAfter(currentTargetEntry.Name);
bool didPushTargets = await PushTargets(afterTargets, currentTargetEntry.ParentEntry, currentTargetEntry.Lookup, currentTargetEntry.ErrorTarget, currentTargetEntry.StopProcessingOnCompletion, TargetBuiltReason.AfterTargets);
// If we have after targets, the last one to run will inherit the stopProcessing flag and we will reset ours. If we didn't push any targets, then we shouldn't clear the
// flag because it means we are still on the bottom of this CallTarget stack.
if ((afterTargets.Count != 0) && didPushTargets)
{
currentTargetEntry.StopProcessingOnCompletion = false;
}
// Put us back on the stack
_targetsToBuild.Push(currentTargetEntry);
// Determine which targets are dependencies. This will also test to see if the target should be skipped due to condition.
// If it is determined the target should skip, the dependency list returned will be empty.
IList<TargetSpecification> dependencies = currentTargetEntry.GetDependencies(_projectLoggingContext);
// Push our before targets now, unconditionally. If we have marked that we should stop processing the stack here, which can only
// happen if our current target was supposed to stop processing AND we had no after targets, then our last before target should
// inherit the stop processing flag and we will reset it.
// Our parent is the target before which we run, just like a depends-on target.
IList<TargetSpecification> beforeTargets = _requestEntry.RequestConfiguration.Project.GetTargetsWhichRunBefore(currentTargetEntry.Name);
bool pushedTargets = await PushTargets(beforeTargets, currentTargetEntry, currentTargetEntry.Lookup, currentTargetEntry.ErrorTarget, stopProcessingStack, TargetBuiltReason.BeforeTargets);
if (beforeTargets.Count != 0 && pushedTargets)
{
stopProcessingStack = false;
}
// And if we have dependencies to run, push them now.
if (dependencies != null)
{
await PushTargets(dependencies, currentTargetEntry, currentTargetEntry.Lookup, false, false, TargetBuiltReason.DependsOn);
}
}
break;
case TargetEntryState.Execution:
// It's possible that our target got pushed onto the stack for one build and had dependencies process, then a re-entrant build started actively building
// the target, encountered a legacy CallTarget, pushed new work onto the stack, and yielded back to here. Instead of starting the already-partially-
// built target, wait for the other one to complete. Then CheckSkipTarget will skip it here.
bool wasActivelyBuilding = await CompleteOutstandingActiveRequests(currentTargetEntry.Name);
// It's possible that our target got pushed onto the stack for one build and had its dependencies process, then a re-entrant build came in and
// actually built this target while we were waiting, so that by the time we get here, it's already been finished. In this case, just blow it away.
if (!CheckSkipTarget(ref stopProcessingStack, currentTargetEntry))
{
ErrorUtilities.VerifyThrow(!wasActivelyBuilding, "Target {0} was actively building and waited on but we are attempting to build it again.", currentTargetEntry.Name);
// This target is now actively building.
_requestEntry.RequestConfiguration.ActivelyBuildingTargets[currentTargetEntry.Name] = _requestEntry.Request.GlobalRequestId;
// Execute all of the tasks on this target.
MSBuildEventSource.Log.TargetStart(currentTargetEntry.Name);
await currentTargetEntry.ExecuteTarget(taskBuilder, _requestEntry, _projectLoggingContext, _cancellationToken);
MSBuildEventSource.Log.TargetStop(currentTargetEntry.Name);
}
break;
case TargetEntryState.ErrorExecution:
if (!CheckSkipTarget(ref stopProcessingStack, currentTargetEntry))
{
// Push the error targets onto the stack. This target will now be marked as completed.
// When that state is processed, it will mark its parent for error execution
var errorTargets = currentTargetEntry.GetErrorTargets(_projectLoggingContext);
try
{
await PushTargets(errorTargets, currentTargetEntry, currentTargetEntry.Lookup, true,
false, TargetBuiltReason.None);
}
catch
{
_requestEntry.RequestConfiguration.ActivelyBuildingTargets.Remove(currentTargetEntry.Name);
throw;
}
}
break;
case TargetEntryState.Completed:
// The target is complete, we can gather up the results and remove this target
// from the stack.
TargetResult targetResult = currentTargetEntry.GatherResults();
// If this result failed but we are under the influence of the legacy ContinueOnError behavior for a
// CallTarget, make sure we don't contribute this failure to the overall success of the build.
targetResult.TargetFailureDoesntCauseBuildFailure = _legacyCallTargetContinueOnError;
// This target is no longer actively building.
_requestEntry.RequestConfiguration.ActivelyBuildingTargets.Remove(currentTargetEntry.Name);
_buildResult.AddResultsForTarget(currentTargetEntry.Name, targetResult);
TargetEntry topEntry = _targetsToBuild.Pop();
if (topEntry.StopProcessingOnCompletion)
{
stopProcessingStack = true;
}
PopDependencyTargetsOnTargetFailure(topEntry, targetResult, ref stopProcessingStack);
break;
default:
ErrorUtilities.ThrowInternalError("Unexpected target state {0}", currentTargetEntry.State);
break;
}
}
}
/// <summary>
/// Determines if the current target should be skipped, and logs the appropriate message.
/// </summary>
/// <returns>True to skip the target, false otherwise.</returns>
private bool CheckSkipTarget(ref bool stopProcessingStack, TargetEntry currentTargetEntry)
{
if (_buildResult.HasResultsForTarget(currentTargetEntry.Name))
{
TargetResult targetResult = _buildResult[currentTargetEntry.Name] as TargetResult;
ErrorUtilities.VerifyThrowInternalNull(targetResult, "targetResult");
if (targetResult.ResultCode != TargetResultCode.Skipped)
{
// If we've already dealt with this target and it didn't skip, let's log appropriately
// Otherwise we don't want anything more to do with it.
var skippedTargetEventArgs = new TargetSkippedEventArgs(
ResourceUtilities.GetResourceString(targetResult.ResultCode == TargetResultCode.Success
? "TargetAlreadyCompleteSuccess"
: "TargetAlreadyCompleteFailure"),
currentTargetEntry.Name)
{
BuildEventContext = _projectLoggingContext.BuildEventContext,
TargetName = currentTargetEntry.Name,
TargetFile = currentTargetEntry.Target.Location.File,
ParentTarget = currentTargetEntry.ParentEntry?.Target.Name,
BuildReason = currentTargetEntry.BuildReason
};
_projectLoggingContext.LogBuildEvent(skippedTargetEventArgs);
if (currentTargetEntry.StopProcessingOnCompletion)
{
stopProcessingStack = true;
}
if (targetResult.ResultCode == TargetResultCode.Success)
{
_targetsToBuild.Peek().LeaveLegacyCallTargetScopes();
_targetsToBuild.Pop();
}
else
{
TargetEntry topEntry = _targetsToBuild.Pop();
// If this is a skip because of target failure, we should behave in the same way as we
// would if this target actually failed -- remove all its dependencies from the stack as
// well. Otherwise, we could encounter a situation where a failure target happens in the
// middle of execution once, then exits, then a request comes through to build the same
// targets, reaches that target, skips-already-failed, and then continues building.
PopDependencyTargetsOnTargetFailure(topEntry, targetResult, ref stopProcessingStack);
}
return true;
}
}
return false;
}
/// <summary>
/// When a target build fails, we don't just stop building that target; we also pop all of the other dependency targets of its
/// parent target off the stack. Extract that logic into a standalone method so that it can be used when dealing with targets that
/// are skipped-unsuccessful as well as first-time failures.
/// </summary>
private void PopDependencyTargetsOnTargetFailure(TargetEntry topEntry, TargetResult targetResult, ref bool stopProcessingStack)
{
if (targetResult.WorkUnitResult.ActionCode == WorkUnitActionCode.Stop)
{
// Pop down to our parent, since any other dependencies our parent had should no longer
// execute. If we encounter an error target on the way down, also stop since the failure
// of one error target in a set declared in OnError should not cause the others to stop running.
while ((!_targetsToBuild.IsEmpty) && (_targetsToBuild.Peek() != topEntry.ParentEntry) && !_targetsToBuild.Peek().ErrorTarget)
{
TargetEntry entry = _targetsToBuild.Pop();
entry.LeaveLegacyCallTargetScopes();
// This target is no longer actively building (if it was).
_requestEntry.RequestConfiguration.ActivelyBuildingTargets.Remove(topEntry.Name);
// If we come across an entry which requires us to stop processing (for instance, an aftertarget of the original
// CallTarget target) then we need to use that flag, not the one from the top entry.
if (entry.StopProcessingOnCompletion)
{
stopProcessingStack = true;
}
}
// Mark our parent for error execution when it is not Completed (e.g. Executing)
if (topEntry.ParentEntry != null && topEntry.ParentEntry.State != TargetEntryState.Completed)
{
topEntry.ParentEntry.MarkForError();
}
// In cases where we need to indicate a failure but the ParentEntry was Skipped (due to condition) it must be
// marked stop to prevent other targets from executing.
else if (topEntry.ParentEntry?.Result?.ResultCode == TargetResultCode.Skipped)
{
topEntry.ParentEntry.MarkForStop();
}
}
}
/// <summary>
/// Pushes the list of targets specified onto the target stack in reverse order specified, so that
/// they will be built in the order specified.
/// </summary>
/// <param name="targets">List of targets to build.</param>
/// <param name="parentTargetEntry">The target which should be considered the parent of these targets.</param>
/// <param name="baseLookup">The lookup to be used to build these targets.</param>
/// <param name="addAsErrorTarget">True if this should be considered an error target.</param>
/// <param name="stopProcessingOnCompletion">True if target stack processing should terminate when the last target in the list is processed.</param>
/// <param name="buildReason">The reason the target is being built by the parent.</param>
/// <returns>True if we actually pushed any targets, false otherwise.</returns>
private async Task<bool> PushTargets(IList<TargetSpecification> targets, TargetEntry parentTargetEntry, Lookup baseLookup, bool addAsErrorTarget, bool stopProcessingOnCompletion, TargetBuiltReason buildReason)
{
List<TargetEntry> targetsToPush = new List<TargetEntry>(targets.Count);
// Iterate the list in reverse order so that the first target in the list is the last pushed, and thus the first to be executed.
for (int i = targets.Count - 1; i >= 0; i--)
{
TargetSpecification targetSpecification = targets[i];
if (buildReason == TargetBuiltReason.BeforeTargets || buildReason == TargetBuiltReason.AfterTargets)
{
// Don't build any Before or After targets for which we already have results. Unlike other targets,
// we don't explicitly log a skipped-with-results message because it is not interesting.
if (_buildResult.HasResultsForTarget(targetSpecification.TargetName))
{
if (_buildResult[targetSpecification.TargetName].ResultCode != TargetResultCode.Skipped)
{
continue;
}
}
}
ElementLocation targetLocation = targetSpecification.ReferenceLocation;
// See if this target is already building under a different build request. If so, we need to wait.
int idOfAlreadyBuildingRequest = BuildRequest.InvalidGlobalRequestId;
if (_requestEntry.RequestConfiguration.ActivelyBuildingTargets.TryGetValue(targetSpecification.TargetName, out idOfAlreadyBuildingRequest))
{
if (idOfAlreadyBuildingRequest != _requestEntry.Request.GlobalRequestId)
{
// Another request elsewhere is building it. We need to wait.
await _requestBuilderCallback.BlockOnTargetInProgress(idOfAlreadyBuildingRequest, targetSpecification.TargetName, null);
// If we come out of here and the target is *still* active, it means the scheduler detected a circular dependency and told us to
// continue so we could throw the exception.
if (_requestEntry.RequestConfiguration.ActivelyBuildingTargets.ContainsKey(targetSpecification.TargetName))
{
ProjectErrorUtilities.ThrowInvalidProject(targetLocation, "CircularDependency", targetSpecification.TargetName);
}
}
else
{
if (buildReason == TargetBuiltReason.AfterTargets)
{
// If the target we are pushing is supposed to run after the current target and it is already set to run after us then skip adding it now.
continue;
}
// We are already building this target on this request. That's a circular dependency.
ProjectErrorUtilities.ThrowInvalidProject(targetLocation, "CircularDependency", targetSpecification.TargetName);
}
}
else
{
// Does this target exist in our direct parent chain, if it is a before target (since these can cause circular dependency issues)
if (buildReason == TargetBuiltReason.BeforeTargets || buildReason == TargetBuiltReason.DependsOn || buildReason == TargetBuiltReason.None)
{
if (HasCircularDependenceInTargets(parentTargetEntry, targetSpecification, out List<string> targetDependenceChain))
{
ProjectErrorUtilities.ThrowInvalidProject(targetLocation, "CircularDependencyInTargetGraph", targetSpecification.TargetName, parentTargetEntry.Name, buildReason, targetSpecification.TargetName, string.Join("<-", targetDependenceChain));
}
}
else
{
// For an after target, if it is already ANYWHERE on the stack, we don't need to push it because it is already going to run
// after whatever target is causing it to be pushed now.
bool alreadyPushed = false;
foreach (TargetEntry entry in _targetsToBuild)
{
if (String.Equals(entry.Name, targetSpecification.TargetName, StringComparison.OrdinalIgnoreCase))
{
alreadyPushed = true;
break;
}
}
if (alreadyPushed)
{
continue;
}
}
}
// Add to the list of targets to push. We don't actually put it on the stack here because we could run into a circular dependency
// during this loop, in which case the target stack would be out of whack.
TargetEntry newEntry = new TargetEntry(_requestEntry, this as ITargetBuilderCallback, targetSpecification, baseLookup, parentTargetEntry, buildReason, _componentHost, stopProcessingOnCompletion);
newEntry.ErrorTarget = addAsErrorTarget;
targetsToPush.Add(newEntry);
stopProcessingOnCompletion = false; // The first target on the stack (the last one to be run) always inherits the stopProcessing flag.
}
// Now push the targets since this operation cannot fail.
foreach (TargetEntry targetToPush in targetsToPush)
{
_targetsToBuild.Push(targetToPush);
}
bool pushedTargets = (targetsToPush.Count > 0);
return pushedTargets;
}
private async Task<bool> CompleteOutstandingActiveRequests(string targetName)
{
// See if this target is already building under a different build request. If so, we need to wait.
int idOfAlreadyBuildingRequest = BuildRequest.InvalidGlobalRequestId;
if (_requestEntry.RequestConfiguration.ActivelyBuildingTargets.TryGetValue(targetName, out idOfAlreadyBuildingRequest))
{
if (idOfAlreadyBuildingRequest != _requestEntry.Request.GlobalRequestId)
{
// Another request is building the target. Wait for that, sending partial results
// for this request, which may be required to unblock it.
await _requestBuilderCallback.BlockOnTargetInProgress(idOfAlreadyBuildingRequest, targetName, _buildResult);
return true;
}
}
return false;
}
private void ComputeAfterTargetFailures(string[] targetNames)
{
foreach (string targetName in targetNames)
{
if (_buildResult.ResultsByTarget.TryGetValue(targetName, out TargetResult targetBuildResult))
{
// Queue of targets waiting to be processed, seeded with the specific target for which we're computing AfterTargetsHaveFailed.
var targetsToCheckForAfterTargets = new Queue<string>();
targetsToCheckForAfterTargets.Enqueue(targetName);
// Set of targets already processed, to break cycles of AfterTargets.
// Initialized lazily when needed below.
HashSet<string> targetsChecked = null;
while (targetsToCheckForAfterTargets?.Count > 0)
{
string targetToCheck = targetsToCheckForAfterTargets.Dequeue();
IList<TargetSpecification> targetsWhichRunAfter = _requestEntry.RequestConfiguration.Project.GetTargetsWhichRunAfter(targetToCheck);
foreach (TargetSpecification afterTarget in targetsWhichRunAfter)
{
_buildResult.ResultsByTarget.TryGetValue(afterTarget.TargetName, out TargetResult result);
if (result?.ResultCode == TargetResultCode.Failure && !result.TargetFailureDoesntCauseBuildFailure)
{
// Mark the target as having an after target failed, and break the loop to move to the next target.
targetBuildResult.AfterTargetsHaveFailed = true;
targetsToCheckForAfterTargets = null;
break;
}
targetsChecked ??= new HashSet<string>(MSBuildNameIgnoreCaseComparer.Default)
{
targetName
};
// If we haven't seen this target yet, add it to the list to check.
if (targetsChecked.Add(afterTarget.TargetName))
{
targetsToCheckForAfterTargets.Enqueue(afterTarget.TargetName);
}
}
}
}
}
}
private bool HasCircularDependenceInTargets(TargetEntry parentTargetEntry, TargetSpecification targetSpecification, out List<string> circularDependenceChain)
{
TargetEntry currentParent = parentTargetEntry;
circularDependenceChain = new List<string>();
bool hasCircularDependence = false;
while (currentParent != null)
{
if (String.Equals(currentParent.Name, targetSpecification.TargetName, StringComparison.OrdinalIgnoreCase))
{
// We are already building this target on this request. That's a circular dependency.
hasCircularDependence = true;
// Cache the circular dependence chain only when circular dependency occurs.
currentParent = parentTargetEntry;
circularDependenceChain.Add(targetSpecification.TargetName);
while (!String.Equals(currentParent.Name, targetSpecification.TargetName, StringComparison.OrdinalIgnoreCase))
{
circularDependenceChain.Add(currentParent.Name);
currentParent = currentParent.ParentEntry;
}
circularDependenceChain.Add(currentParent.Name);
break;
}
currentParent = currentParent.ParentEntry;
}
return hasCircularDependence;
}
}
}