Skip to content

Commit

Permalink
Fix hang if build does not start (#272)
Browse files Browse the repository at this point in the history
  • Loading branch information
slang25 authored May 13, 2024
1 parent e1c24ec commit 65950f7
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/Buildalyzer/ProjectAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Buildalyzer.Logger;
using Buildalyzer.Logging;
using Microsoft.Build.Construction;
using Microsoft.Build.Framework;
using Microsoft.Build.Logging;
using Microsoft.Extensions.Logging;
using MsBuildPipeLogger;
Expand Down Expand Up @@ -153,6 +154,14 @@ private IAnalyzerResults BuildTargets(
using (CancellationTokenSource cancellation = new CancellationTokenSource())
{
using var pipeLogger = new AnonymousPipeLoggerServer(cancellation.Token);
bool receivedAnyEvent = false;

void OnPipeLoggerOnAnyEventRaised(object o, BuildEventArgs buildEventArgs)
{
receivedAnyEvent = true;
}

pipeLogger.AnyEventRaised += OnPipeLoggerOnAnyEventRaised;
using var eventProcessor = new EventProcessor(Manager, this, BuildLoggers, pipeLogger, results != null);

// Run MSBuild
Expand All @@ -170,8 +179,24 @@ private IAnalyzerResults BuildTargets(
GetEffectiveEnvironmentVariables(buildEnvironment),
Manager.LoggerFactory))
{
void OnProcessRunnerExited()
{
if (!receivedAnyEvent && processRunner.ExitCode != 0)
{
pipeLogger.Dispose();
}
}

processRunner.Exited += OnProcessRunnerExited;
processRunner.Start();
pipeLogger.ReadAll();
try
{
pipeLogger.ReadAll();
}
catch (ObjectDisposedException)
{
// Ignore
}
processRunner.WaitForExit();
exitCode = processRunner.ExitCode;
}
Expand Down
17 changes: 17 additions & 0 deletions tests/Buildalyzer.Tests/Integration/SimpleProjectsFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,23 @@ public void GetsAdditionalFile()
.Should().BeEquivalentTo("message.txt");
}

[Test]
public void HandlesProcessFailure()
{
// Given
StringWriter log = new StringWriter();
IProjectAnalyzer analyzer = GetProjectAnalyzer(@"SdkNet6Exe\SdkNet6Exe.csproj", log);

// When
IAnalyzerResults results = analyzer.Build(new EnvironmentOptions
{
Arguments = { "/unknown" } // This argument will cause msbuild to immediately fail
});

// Then
results.OverallSuccess.ShouldBeFalse();
}

private static IProjectAnalyzer GetProjectAnalyzer(string projectFile, StringWriter log)
{
IProjectAnalyzer analyzer = new AnalyzerManager(
Expand Down

0 comments on commit 65950f7

Please sign in to comment.