diff --git a/CodeConv/Util/ProcessRunner.cs b/CodeConv/Util/ProcessRunner.cs index 9070a93a8..99ca4da12 100644 --- a/CodeConv/Util/ProcessRunner.cs +++ b/CodeConv/Util/ProcessRunner.cs @@ -35,6 +35,7 @@ private static async Task 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(); var stdErrComplete = new TaskCompletionSource(); @@ -61,4 +62,16 @@ private static async Task GetExitCodeAsync(this ProcessStartInfo psi, TextW return process.ExitCode; } + + /// + /// 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 + /// + private static void DisableMsBuildNodeReuse(ProcessStartInfo psi) => psi.Environment["MSBUILDDISABLENODEREUSE"] = "1"; } \ No newline at end of file diff --git a/Tests/LanguageAgnostic/ProcessRunnerTests.cs b/Tests/LanguageAgnostic/ProcessRunnerTests.cs new file mode 100644 index 000000000..c21a7fbbd --- /dev/null +++ b/Tests/LanguageAgnostic/ProcessRunnerTests.cs @@ -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); + + /// + /// Two SDK-less projects built in parallel make MSBuild launch a worker node, without the cost of a real restore. + /// + [Fact] + public async Task GetOutputAsyncReturnsWhenBuildLeavesNewWorkerNodesRunningAsync() + { + var dir = Directory.CreateTempSubdirectory("ProcessRunnerTests"); + try { + File.WriteAllText(Path.Combine(dir.FullName, "child.proj"), @""); + File.WriteAllText(Path.Combine(dir.FullName, "root.proj"), @" + +

+

+ + +"); + 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); + } + } +}