Skip to content
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
13 changes: 13 additions & 0 deletions CodeConv/Util/ProcessRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ private static async Task<int> GetExitCodeAsync(this ProcessStartInfo psi, TextW
psi.UseShellExecute = false;
psi.RedirectStandardError = true;
psi.RedirectStandardOutput = true;
DisableMsBuildNodeReuse(psi);
using var process = new Process { StartInfo = psi };
var stdOutComplete = new TaskCompletionSource<object?>();
var stdErrComplete = new TaskCompletionSource<object?>();
Expand All @@ -61,4 +62,16 @@ private static async Task<int> GetExitCodeAsync(this ProcessStartInfo psi, TextW

return process.ExitCode;
}

/// <summary>
/// We wait for the redirected stdout/stderr to close, which only happens once every process holding them has exited.
/// MSBuild keeps worker nodes alive after the build for reuse, and launches them without redirecting their streams when
/// MSBUILDENSURESTDOUTFORTASKPROCESSES=1 (which `dotnet test` sets for its children), so they inherit our pipes and we'd wait
/// until they time out. Without node reuse, the nodes exit with the build.
/// Streams only redirected when that variable isn't set:
/// https://github.com/dotnet/msbuild/blob/2bccd27c1733ff52893ff41e3b4046e51d0334ee/src/Build/BackEnd/Components/Communications/NodeLauncher.cs#L65-L67
/// https://github.com/dotnet/msbuild/blob/2bccd27c1733ff52893ff41e3b4046e51d0334ee/src/Framework/Traits.cs#L487-L490
/// MSBUILDDISABLENODEREUSE: https://github.com/dotnet/msbuild/blob/2bccd27c1733ff52893ff41e3b4046e51d0334ee/documentation/wiki/MSBuild-Environment-Variables.md
/// </summary>
private static void DisableMsBuildNodeReuse(ProcessStartInfo psi) => psi.Environment["MSBUILDDISABLENODEREUSE"] = "1";
}
46 changes: 46 additions & 0 deletions Tests/LanguageAgnostic/ProcessRunnerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
using ICSharpCode.CodeConverter.DotNetTool.Util;
using Xunit;

namespace ICSharpCode.CodeConverter.Tests.LanguageAgnostic;

public class ProcessRunnerTests
{
private static readonly TimeSpan BuildTimeout = TimeSpan.FromMinutes(1);

/// <summary>
/// Two SDK-less projects built in parallel make MSBuild launch a worker node, without the cost of a real restore.
/// </summary>
[Fact]
public async Task GetOutputAsyncReturnsWhenBuildLeavesNewWorkerNodesRunningAsync()
{
var dir = Directory.CreateTempSubdirectory("ProcessRunnerTests");
try {
File.WriteAllText(Path.Combine(dir.FullName, "child.proj"), @"<Project><Target Name=""Build"" /></Project>");
File.WriteAllText(Path.Combine(dir.FullName, "root.proj"), @"<Project>
<ItemGroup>
<P Include=""child.proj"" AdditionalProperties=""X=1"" />
<P Include=""child.proj"" AdditionalProperties=""X=2"" />
</ItemGroup>
<Target Name=""Build""><MSBuild Projects=""@(P)"" BuildInParallel=""true"" /></Target>
</Project>");
var psi = new ProcessStartInfo("dotnet", "msbuild root.proj -m:2 -nologo -v:q") { WorkingDirectory = dir.FullName };
// `dotnet test` sets this for its children: MSBuild then lets worker nodes inherit stdout/stderr instead of redirecting them
psi.Environment["MSBUILDENSURESTDOUTFORTASKPROCESSES"] = "1";
// A unique handshake stops MSBuild reusing an idle node, so it must launch a new one
psi.Environment["MSBUILDNODEHANDSHAKESALT"] = Guid.NewGuid().ToString("N");

var build = psi.GetOutputAsync();
var completed = await Task.WhenAny(build, Task.Delay(BuildTimeout));

Assert.True(completed == build, $"dotnet msbuild output didn't end within {BuildTimeout}");
var (exitCode, stdOut, stdErr) = await build;
Assert.True(exitCode == 0, stdOut + stdErr);
} finally {
dir.Delete(true);
}
}
}
Loading