Skip to content

Commit 9eaef28

Browse files
authored
Attempt to capture binlogs when test times out (#22468)
1 parent 544afc0 commit 9eaef28

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

src/Razor/Microsoft.NET.Sdk.Razor/test/IntegrationTests/MSBuildProcessManager.cs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,31 @@ public static async Task<MSBuildResult> RunProcessAsync(
5050
processStartInfo.EnvironmentVariables["DOTNET_SKIP_FIRST_TIME_EXPERIENCE"] = "true";
5151
}
5252

53-
var processResult = await RunProcessCoreAsync(processStartInfo, timeout);
53+
ProcessResult processResult;
54+
try
55+
{
56+
processResult = await RunProcessCoreAsync(processStartInfo, timeout);
57+
}
58+
catch (TimeoutException ex)
59+
{
60+
// Copy the binlog to the artifacts directory if executing MSBuild throws.
61+
// This would help diagnosing failures on the CI.
62+
var binaryLogFile = Path.Combine(project.ProjectFilePath, "msbuild.binlog");
63+
64+
var artifactsLogDir = Assembly.GetExecutingAssembly()
65+
.GetCustomAttributes<AssemblyMetadataAttribute>()
66+
.FirstOrDefault(ama => ama.Key == "ArtifactsLogDir")?.Value;
67+
68+
if (!string.IsNullOrEmpty(artifactsLogDir) && File.Exists(binaryLogFile))
69+
{
70+
var targetPath = Path.Combine(artifactsLogDir, Path.GetFileNameWithoutExtension(project.ProjectFilePath) + "." + Path.GetRandomFileName() + ".binlog");
71+
File.Copy(binaryLogFile, targetPath);
72+
73+
throw new TimeoutException(ex.Message + $"{Environment.NewLine}Captured binlog at {targetPath}");
74+
}
75+
76+
throw;
77+
}
5478

5579
return new MSBuildResult(project, processResult.FileName, processResult.Arguments, processResult.ExitCode, processResult.Output);
5680
}

0 commit comments

Comments
 (0)