diff --git a/src/PowerShellEditorServices/Services/PowerShell/Debugging/DscBreakpointCapability.cs b/src/PowerShellEditorServices/Services/PowerShell/Debugging/DscBreakpointCapability.cs index c929f4bcc..763094be4 100644 --- a/src/PowerShellEditorServices/Services/PowerShell/Debugging/DscBreakpointCapability.cs +++ b/src/PowerShellEditorServices/Services/PowerShell/Debugging/DscBreakpointCapability.cs @@ -10,7 +10,6 @@ using Microsoft.Extensions.Logging; using Microsoft.PowerShell.EditorServices.Services.DebugAdapter; using Microsoft.PowerShell.EditorServices.Services.PowerShell.Execution; -using Microsoft.PowerShell.EditorServices.Services.PowerShell.Host; using Microsoft.PowerShell.EditorServices.Services.PowerShell.Runspace; namespace Microsoft.PowerShell.EditorServices.Services.PowerShell.Debugging @@ -54,10 +53,7 @@ public async Task> SetLineBreakpointsAsync( ? $"Enable-DscDebug -Breakpoint {hashtableString}" : "Disable-DscDebug"); - await executionService.ExecutePSCommandAsync( - dscCommand, - CancellationToken.None) - .ConfigureAwait(false); + await executionService.ExecutePSCommandAsync(dscCommand, CancellationToken.None).ConfigureAwait(false); // Verify all the breakpoints and return them foreach (BreakpointDetails breakpoint in breakpoints) @@ -80,7 +76,7 @@ public bool IsDscResourcePath(string scriptPath) public static async Task GetDscCapabilityAsync( ILogger logger, IRunspaceInfo currentRunspace, - PsesInternalHost psesHost) + IInternalPowerShellExecutionService executionService) { // DSC support is enabled only for Windows PowerShell. if ((currentRunspace.PowerShellVersionDetails.Version.Major >= 6) && @@ -92,16 +88,13 @@ public static async Task GetDscCapabilityAsync( if (!isDscInstalled.HasValue) { PSCommand psCommand = new PSCommand() - .AddScript($"$global:{DebugService.PsesGlobalVariableNamePrefix}prevProgressPreference = $ProgressPreference") - .AddScript("$ProgressPreference = 'SilentlyContinue'") .AddCommand(@"Microsoft.PowerShell.Core\Import-Module") .AddParameter("Name", "PSDesiredStateConfiguration") .AddParameter("PassThru") - .AddParameter("ErrorAction", ActionPreference.Ignore) - .AddScript($"$ProgressPreference = $global:{DebugService.PsesGlobalVariableNamePrefix}prevProgressPreference"); + .AddParameter("ErrorAction", ActionPreference.Ignore); IReadOnlyList dscModule = - await psesHost.ExecutePSCommandAsync( + await executionService.ExecutePSCommandAsync( psCommand, CancellationToken.None, new PowerShellExecutionOptions { ThrowOnError = false }) @@ -113,19 +106,29 @@ await psesHost.ExecutePSCommandAsync( if (isDscInstalled.Value) { - PSCommand psCommand = new PSCommand() - .AddCommand("Get-DscResource") - .AddCommand("Select-Object") - .AddParameter("ExpandProperty", "ParentPath"); + // Note that __psEditorServices_ is DebugService.PsesGlobalVariableNamePrefix but + // it's notoriously difficult to interpolate it in this script, which has to be a + // single script to guarantee everything is run at once. + PSCommand psCommand = new PSCommand().AddScript( + """ + try { + $global:__psEditorServices_prevProgressPreference = $ProgressPreference + $global:ProgressPreference = 'SilentlyContinue' + return Get-DscResource | Select-Object -ExpandProperty ParentPath + } finally { + $ProgressPreference = $global:__psEditorServices_prevProgressPreference + } + """); IReadOnlyList resourcePaths = - await psesHost.ExecutePSCommandAsync( + await executionService.ExecutePSCommandAsync( psCommand, CancellationToken.None, new PowerShellExecutionOptions { ThrowOnError = false } ).ConfigureAwait(false); logger.LogTrace($"DSC resources found: {resourcePaths.Count}"); + return new DscBreakpointCapability { dscResourceRootPaths = resourcePaths.ToArray()