diff --git a/src/Microsoft.TestPlatform.Build/Tasks/VSTestTask.cs b/src/Microsoft.TestPlatform.Build/Tasks/VSTestTask.cs index df681503ed..2d151decb3 100644 --- a/src/Microsoft.TestPlatform.Build/Tasks/VSTestTask.cs +++ b/src/Microsoft.TestPlatform.Build/Tasks/VSTestTask.cs @@ -5,7 +5,9 @@ namespace Microsoft.TestPlatform.Build.Tasks { using System; using System.Collections.Generic; + using System.Diagnostics; using System.Linq; + using System.Threading; using Microsoft.Build.Framework; using Microsoft.Build.Utilities; using Microsoft.TestPlatform.Build.Resources; @@ -161,6 +163,27 @@ public override bool Execute() var traceEnabledValue = Environment.GetEnvironmentVariable("VSTEST_BUILD_TRACE"); Tracing.traceEnabled = !string.IsNullOrEmpty(traceEnabledValue) && traceEnabledValue.Equals("1", StringComparison.OrdinalIgnoreCase); + var debugEnabled = Environment.GetEnvironmentVariable("VSTEST_BUILD_DEBUG"); + if (!string.IsNullOrEmpty(debugEnabled) && debugEnabled.Equals("1", StringComparison.Ordinal)) + { + Console.WriteLine("Waiting for debugger attach..."); + + var currentProcess = Process.GetCurrentProcess(); + Console.WriteLine(string.Format("Process Id: {0}, Name: {1}", currentProcess.Id, currentProcess.ProcessName)); + + while (!Debugger.IsAttached) + { + Thread.Sleep(1000); + } + + Debugger.Break(); + } + + // Avoid logging "Task returned false but did not log an error." on test failure, because we don't + // write MSBuild error. https://github.com/dotnet/msbuild/blob/51a1071f8871e0c93afbaf1b2ac2c9e59c7b6491/src/Framework/IBuildEngine7.cs#L12 + var allowfailureWithoutError = BuildEngine.GetType().GetProperty("AllowFailureWithoutError"); + allowfailureWithoutError?.SetValue(BuildEngine, true); + vsTestForwardingApp = new VSTestForwardingApp(this.VSTestConsolePath, this.CreateArgument()); if (!string.IsNullOrEmpty(this.VSTestFramework)) {