From e1668028dc105dfdd10fe3dcf3f025e7dd4f14bd Mon Sep 17 00:00:00 2001 From: Marco Rossignoli Date: Tue, 3 Dec 2019 10:06:49 +0100 Subject: [PATCH] Add msbuild instrumentation task debugging mode (#631) Add msbuild instrumentation task debugging mode --- Documentation/Troubleshooting.md | 13 ++++++++++++ .../InstrumentationTask.cs | 20 +++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/Documentation/Troubleshooting.md b/Documentation/Troubleshooting.md index 5d44682..0a5e474 100644 --- a/Documentation/Troubleshooting.md +++ b/Documentation/Troubleshooting.md @@ -223,3 +223,16 @@ We can collect logs from trackers through an enviroment variable ``` When enabled, tracking event will be collected in log file near to module location. File name will be something like `moduleName.dll_tracker.txt` + +## Enable msbuild task instrumentation debugging + +You can live attach and debug msbuild tasks with `COVERLET_MSBUILD_INSTRUMENTATIONTASK_DEBUG` env variable +``` + set COVERLET_MSBUILD_INSTRUMENTATIONTASK_DEBUG=1 +``` +You'll get this message during test run +``` +dotnet test -p:Include="[test_coverage.]" -p:Exclude="[*.Test.*]*" -p:CollectCoverage=true -p:CoverletOutputFormat=cobertura -p:CoverletOutput=coverage.cobertura.xml +Coverlet msbuild instrumentation task debugging is enabled. Please attach debugger to process to continue +Process Id: 29228 Name: dotnet +``` \ No newline at end of file diff --git a/src/coverlet.msbuild.tasks/InstrumentationTask.cs b/src/coverlet.msbuild.tasks/InstrumentationTask.cs index ee15c41..27521a7 100644 --- a/src/coverlet.msbuild.tasks/InstrumentationTask.cs +++ b/src/coverlet.msbuild.tasks/InstrumentationTask.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics; using System.IO; using Coverlet.Core; @@ -97,8 +98,27 @@ namespace Coverlet.MSbuild.Tasks _logger = new MSBuildLogger(Log); } + private void WaitForDebuggerIfEnabled() + { + if (int.TryParse(Environment.GetEnvironmentVariable("COVERLET_MSBUILD_INSTRUMENTATIONTASK_DEBUG"), out int result) && result == 1) + { + Console.WriteLine("Coverlet msbuild instrumentation task debugging is enabled. Please attach debugger to process to continue"); + Process currentProcess = Process.GetCurrentProcess(); + Console.WriteLine($"Process Id: {currentProcess.Id} Name: {currentProcess.ProcessName}"); + + while (!Debugger.IsAttached) + { + System.Threading.Tasks.Task.Delay(1000).Wait(); + } + + Debugger.Break(); + } + } + public override bool Execute() { + WaitForDebuggerIfEnabled(); + try { var includeFilters = _include?.Split(',');