PowerShell / PowerShellEditorServices

A common platform for PowerShell development support in any editor or application!
MIT License
631 stars 215 forks source link

Debugging script with breakpoint fails if PS profile sets ErroractionPreference to Stop #2037

Closed theGeorgew closed 1 year ago

theGeorgew commented 1 year ago

Prerequisites

Steps to reproduce

Set the profile's EAP to stop Set-content -path $pshome/profile.ps1 -value '$ErrorActionPreference = "stop"' Debug a Powershell script that has a breakpoint set Observe the Powershell terminal prints an error importing the DSC module. The script will also not stop at the breakpoint. image

if you rollback the version of the Powershell Extension to the previous release the error does not occur code --install-extension powershell-2023.5.0.vsix --force I saw that this issue was merged for the release that is causing the bug: https://github.com/PowerShell/PowerShellEditorServices/pull/2020 I see there were changes made to the loading of the DSC module that is failing to be loaded in the error https://github.com/PowerShell/PowerShellEditorServices/pull/2020/files#diff-df2c1977dd89746b4b394dc2e26725467d330545813bf2bbc3383476693de476R94-R97

Expected behavior

I can debug scripts with breakpoints and it stops at the breakpoints without throwing errors in the terminal

Actual behavior

When debugging scripts with breakpoints it does not stop at the breakpoints and throws errors in the terminal

Error details

Error logged to terminal: 
Import-Module : The specified module 'C:\Program Files\DesiredStateConfiguration\1.0.0.0\Modules\PSDesiredStateConfiguration\PSDesiredStateConfiguration.psd1' was not loaded because no valid module file was found in any module directory. was not loaded because no valid module file was found in any module directory.
    + CategoryInfo          : ResourceUnavailable: (C:\Program File...figuration.psd1:String) [Import-Module], FileNotFoundException
    + FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand

Error logged in Powershell Editor Services 
[Warn  - 12:34:26 PM] Microsoft.PowerShell.EditorServices.Services.PowerShell.Host.PsesInternalHost: Runtime exception occurred while executing command:

System.Management.Automation.ActionPreferenceStopException: The running command stopped because the preference variable "ErrorActionPreference" or common parameter is set to Stop: The specified module 'C:\Program Files\DesiredStateConfiguration\1.0.0.0\Modules\PSDesiredStateConfiguration\PSDesiredStateConfiguration.psd1' was not loaded because no valid module file was found in any module directory.
   at System.Management.Automation.Runspaces.PipelineBase.Invoke(IEnumerable input)
   at System.Management.Automation.PowerShell.Worker.ConstructPipelineAndDoWork(Runspace rs, Boolean performSyncInvoke)
   at System.Management.Automation.PowerShell.Worker.CreateRunspaceIfNeededAndDoWork(Runspace rsToUse, Boolean isSync)
   at System.Management.Automation.PowerShell.CoreInvokeHelper[TInput,TOutput](PSDataCollection`1 input, PSDataCollection`1 output, PSInvocationSettings settings)
   at System.Management.Automation.PowerShell.CoreInvoke[TInput,TOutput](PSDataCollection`1 input, PSDataCollection`1 output, PSInvocationSettings settings)
   at System.Management.Automation.PowerShell.Invoke[T](IEnumerable input, PSInvocationSettings settings)
   at Microsoft.PowerShell.EditorServices.Services.PowerShell.Utility.PowerShellExtensions.InvokeAndClear[TResult](PowerShell pwsh, PSInvocationSettings invocationSettings)
   at Microsoft.PowerShell.EditorServices.Services.PowerShell.Execution.SynchronousPowerShellTask`1.ExecuteNormally(CancellationToken cancellationToken) |

Environment data

Name                           Value
----                           -----
PSVersion                      5.1.19041.2673
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.19041.2673
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

Version

2023.6.0

Visuals

No response

andyleejordan commented 1 year ago

We'll just have to override $ErrorActionPreference during this code path.

andyleejordan commented 1 year ago

Ok so this is the code we're using to import that module:

                PSCommand psCommand = new PSCommand()
                    .AddCommand("Import-Module")
                    .AddArgument(@"C:\Program Files\DesiredStateConfiguration\1.0.0.0\Modules\PSDesiredStateConfiguration\PSDesiredStateConfiguration.psd1")
                    .AddParameter("PassThru");

                IReadOnlyList<PSModuleInfo> dscModule =
                    await psesHost.ExecutePSCommandAsync<PSModuleInfo>(
                        psCommand,
                        CancellationToken.None,
                        new PowerShellExecutionOptions { ThrowOnError = false }).ConfigureAwait(false);

                isDscInstalled = dscModule.Count > 0;
                logger.LogDebug("Side-by-side DSC module found: " + isDscInstalled.Value);

I'm testing some stuff...strangely I can get the error to show up, but I can't repro where it causes things to crash, even with '$ErrorActionPreference = "stop".