Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
        /// <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
                if (topEntry.ParentEntry != null && topEntry.ParentEntry.State != TargetEntryState.Completed)
                {
                    topEntry.ParentEntry.MarkForError();
                }
            }
        }
        /// <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>
        /// 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);

                        // 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)
                    {
                        TargetEntry currentParent = parentTargetEntry;
                        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.
                                ProjectErrorUtilities.ThrowInvalidProject(targetLocation, "CircularDependency", targetSpecification.TargetName);
                            }

                            currentParent = currentParent.ParentEntry;
                        }
                    }
                    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);
        }
        /// <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 (null != dependencies)
                        {
                            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 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))
                    {
                        // This target is now actively building.
                        _requestEntry.RequestConfiguration.ActivelyBuildingTargets.Add(currentTargetEntry.Name, _requestEntry.Request.GlobalRequestId);

                        // Execute all of the tasks on this target.
                        await currentTargetEntry.ExecuteTarget(taskBuilder, _requestEntry, _projectLoggingContext, _cancellationToken);
                    }

                    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
                        {
                            if (_requestEntry.RequestConfiguration.ActivelyBuildingTargets.ContainsKey(
                                    currentTargetEntry.Name))
                            {
                                _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>
        /// 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);
        }