Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add better handling of AggregateExceptions in static graph-based restore #4809

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using Microsoft.Build.Definition;
using Microsoft.Build.Evaluation;
using Microsoft.Build.Evaluation.Context;
using Microsoft.Build.Exceptions;
using Microsoft.Build.Execution;
using Microsoft.Build.Framework;
using Microsoft.Build.Graph;
Expand Down Expand Up @@ -130,7 +131,7 @@ public async Task<bool> RestoreAsync(string entryProjectFilePath, IDictionary<st
}
catch (Exception e)
{
LoggingQueue.TaskLoggingHelper.LogErrorFromException(e, showStackTrace: true);
LogErrorFromException(e);

return false;
}
Expand Down Expand Up @@ -167,7 +168,7 @@ public bool WriteDependencyGraphSpec(string entryProjectFilePath, IDictionary<st
}
catch (Exception e)
{
LoggingQueue.TaskLoggingHelper.LogErrorFromException(e, showStackTrace: true);
LogErrorFromException(e);
}
return false;
}
Expand Down Expand Up @@ -690,13 +691,9 @@ private DependencyGraphSpec GetDependencyGraphSpec(string entryProjectPath, IDic
}
});
}
catch (AggregateException e)
catch (Exception e)
{
// Log exceptions thrown while creating PackageSpec objects
foreach (var exception in e.Flatten().InnerExceptions)
{
LoggingQueue.TaskLoggingHelper.LogErrorFromException(exception);
}
LogErrorFromException(e);

return null;
}
Expand Down Expand Up @@ -724,7 +721,7 @@ private DependencyGraphSpec GetDependencyGraphSpec(string entryProjectPath, IDic
}
catch (Exception e)
{
LoggingQueue.TaskLoggingHelper.LogErrorFromException(e, showStackTrace: true);
LogErrorFromException(e);
}

return null;
Expand Down Expand Up @@ -997,7 +994,7 @@ private ICollection<ProjectWithInnerNodes> LoadProjects(IEnumerable<ProjectGraph
}
catch (Exception e)
{
LoggingQueue.TaskLoggingHelper.LogErrorFromException(e, showStackTrace: true);
LogErrorFromException(e);

return null;
}
Expand Down Expand Up @@ -1037,5 +1034,42 @@ private static IEnumerable<IMSBuildItem> GetDistinctItemsOrEmpty(IMSBuildProject
{
return project.GetItems(itemName)?.Distinct(ProjectItemInstanceEvaluatedIncludeComparer.Instance) ?? Enumerable.Empty<IMSBuildItem>();
}

/// <summary>
/// Logs an error from the specified exception.
/// </summary>
/// <param name="exception">The <see cref="Exception" /> with details to be logged.</param>
private void LogErrorFromException(Exception exception)
jeffkl marked this conversation as resolved.
Show resolved Hide resolved
{
switch (exception)
{
case AggregateException aggregateException:
foreach (Exception innerException in aggregateException.Flatten().InnerExceptions)
{
LogErrorFromException(innerException);
}
break;

case InvalidProjectFileException invalidProjectFileException:
// Special case the InvalidProjectFileException since it has extra information about what project file couldn't be loaded
LoggingQueue.TaskLoggingHelper.LogError(
invalidProjectFileException.ErrorSubcategory,
invalidProjectFileException.ErrorCode,
invalidProjectFileException.HelpKeyword,
invalidProjectFileException.ProjectFile,
invalidProjectFileException.LineNumber,
invalidProjectFileException.ColumnNumber,
invalidProjectFileException.EndLineNumber,
invalidProjectFileException.EndColumnNumber,
invalidProjectFileException.Message);
break;

default:
LoggingQueue.TaskLoggingHelper.LogErrorFromException(
exception,
showStackTrace: true);
break;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,48 @@ public Task MsbuildRestore_WithStaticGraphRestore_MessageLoggedAtDefaultVerbosit
return Task.CompletedTask;
}

[PlatformFact(Platform.Windows)]
public void MsbuildRestore_StaticGraphEvaluation_HandlesInvalidProjectFileException()
{
// Arrange
using (var pathContext = new SimpleTestPathContext())
{
// Set up solution, project, and packages
var solution = new SimpleTestSolutionContext(pathContext.SolutionRoot);

var net461 = NuGetFramework.Parse("net461");

var projectA = new SimpleTestProjectContext("a", ProjectStyle.PackageReference, pathContext.SolutionRoot);

var projectB = new SimpleTestProjectContext("b", ProjectStyle.PackageReference, pathContext.SolutionRoot);

var projectAFrameworkContext = new SimpleTestProjectFrameworkContext(net461);

projectAFrameworkContext.ProjectReferences.Add(projectB);

var packageX = new SimpleTestPackageContext()
{
Id = "x",
Version = "1.0.0"
};
projectA.Frameworks.Add(projectAFrameworkContext);

projectA.AddPackageToAllFrameworks(packageX);

solution.Projects.Add(projectA);
solution.Create(pathContext.SolutionRoot);

File.Delete(projectB.ProjectPath);

var result = _msbuildFixture.RunMsBuild(pathContext.WorkingDirectory, $"/t:restore /p:RestoreUseStaticGraphEvaluation=true {projectA.ProjectPath}", ignoreExitCode: true);

// Assert
Assert.True(result.ExitCode == 1, result.AllOutput);

result.AllOutput.Should().Contain($"error MSB4025: The project file could not be loaded. Could not find file '{projectB.ProjectPath}'");
}
}

[PlatformTheory(Platform.Windows)]
[InlineData(true)]
[InlineData(false)]
Expand Down