Skip to content

Commit

Permalink
Propagate debug path to out of proc nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
cdmihai committed Jul 24, 2021
1 parent d0f2981 commit bb65c38
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ internal BuildRequestEngine()
{
_debugDumpState = Traits.Instance.DebugScheduler;
_debugDumpPath = ChangeWaves.AreFeaturesEnabled(ChangeWaves.Wave17_0)
? DebugUtils.DebugDumpPath()
? DebugUtils.DebugPath
: Environment.GetEnvironmentVariable("MSBUILDDEBUGPATH");
_debugForceCaching = Environment.GetEnvironmentVariable("MSBUILDDEBUGFORCECACHING") == "1";

Expand Down
2 changes: 1 addition & 1 deletion src/Build/BackEnd/Components/Scheduler/Scheduler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public Scheduler()
{
_debugDumpState = Traits.Instance.DebugScheduler;
_debugDumpPath = ChangeWaves.AreFeaturesEnabled(ChangeWaves.Wave17_0)
? DebugUtils.DebugDumpPath()
? DebugUtils.DebugPath
: Environment.GetEnvironmentVariable("MSBUILDDEBUGPATH");
_schedulingUnlimitedVariable = Environment.GetEnvironmentVariable("MSBUILDSCHEDULINGUNLIMITED");
_nodeLimitOffset = 0;
Expand Down
9 changes: 8 additions & 1 deletion src/Shared/CommunicationsUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,13 @@ static internal int NodeConnectionTimeout
/// </summary>
internal static Dictionary<string, string> GetEnvironmentVariables()
{
#if !CLR2COMPATIBILITY
// The DebugUtils static constructor can set the MSBUILDDEBUGPATH environment variable to propagate the debug path to out of proc nodes.
// Need to ensure that constructor is called before this method returns in order to capture its env var write.
// Otherwise the env var is not captured and thus gets deleted when RequiestBuilder resets the environment based on the cached results of this method.
ErrorUtilities.VerifyThrowInternalNull(DebugUtils.DebugPath, nameof(DebugUtils.DebugPath));
#endif

Dictionary<string, string> table = new Dictionary<string, string>(200, StringComparer.OrdinalIgnoreCase); // Razzle has 150 environment variables

if (NativeMethodsShared.IsWindows)
Expand Down Expand Up @@ -561,7 +568,7 @@ internal static void Trace(int nodeId, string format, params object[] args)
Environment.GetEnvironmentVariable("MSBUILDDEBUGPATH");
#else
ChangeWaves.AreFeaturesEnabled(ChangeWaves.Wave17_0)
? DebugUtils.DebugDumpPath()
? DebugUtils.DebugPath
: Environment.GetEnvironmentVariable("MSBUILDDEBUGPATH");
#endif

Expand Down
33 changes: 23 additions & 10 deletions src/Shared/Debugging/DebugUtils.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using Microsoft.Build.Utilities;
using System;
using System.Diagnostics;
using System.IO;
Expand All @@ -17,6 +18,25 @@ private enum NodeMode
OutOfProcTaskHostNode
}

static DebugUtils()
{
string environmentDebugPath = Environment.GetEnvironmentVariable("MSBUILDDEBUGPATH");
var debugDirectory = environmentDebugPath ?? Path.Combine(Directory.GetCurrentDirectory(), "MSBuild_Logs");

if (Traits.Instance.DebugEngine)
{
FileUtilities.EnsureDirectoryExists(debugDirectory);

// Out of proc nodes do not know the startup directory so set the environment variable for them.
if (string.IsNullOrWhiteSpace(environmentDebugPath))
{
Environment.SetEnvironmentVariable("MSBUILDDEBUGPATH", debugDirectory);
}
}

DebugPath = debugDirectory;
}

private static readonly Lazy<NodeMode> ProcessNodeMode = new(
() =>
{
Expand Down Expand Up @@ -57,27 +77,20 @@ private static bool CurrentProcessMatchesDebugName()

public static readonly bool ShouldDebugCurrentProcess = CurrentProcessMatchesDebugName();

public static string DebugDumpPath()
{
var debugDirectory = Environment.GetEnvironmentVariable("MSBUILDDEBUGPATH") ?? Path.Combine(Directory.GetCurrentDirectory(), "MSBuild_Logs");
FileUtilities.EnsureDirectoryExists(debugDirectory);

return debugDirectory;
}
public static string DebugPath { get; }

public static string FindNextAvailableDebugFilePath(string fileName)
{
var extension = Path.GetExtension(fileName);
var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fileName);

var debugRoot = DebugDumpPath();
var fullPath = Path.Combine(debugRoot, fileName);
var fullPath = Path.Combine(DebugPath, fileName);

var counter = 0;
while (File.Exists(fullPath))
{
fileName = $"{fileNameWithoutExtension}_{counter++}{extension}";
fullPath = Path.Combine(debugRoot, fileName);
fullPath = Path.Combine(DebugPath, fileName);
}

return fullPath;
Expand Down
2 changes: 1 addition & 1 deletion src/Shared/ExceptionHandling.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ private static string GetDebugDumpPath()
Environment.GetEnvironmentVariable("MSBUILDDEBUGPATH");
#else
ChangeWaves.AreFeaturesEnabled(ChangeWaves.Wave17_0)
? DebugUtils.DebugDumpPath()
? DebugUtils.DebugPath
: Environment.GetEnvironmentVariable("MSBUILDDEBUGPATH");
#endif

Expand Down

0 comments on commit bb65c38

Please sign in to comment.