Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
3 tasks done
theGeorgew opened this issue Jun 14, 2023 · 2 comments · Fixed by #2058
Closed
3 tasks done
Assignees
Labels

Comments

@theGeorgew
Copy link

Prerequisites

  • Write a descriptive title.
  • Make sure you are able to repro it on the latest version
  • Search the existing issues.

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: #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

@ghost ghost added the Needs: Triage Maintainer attention needed! label Jun 14, 2023
@andyleejordan andyleejordan self-assigned this Jun 19, 2023
@andyleejordan
Copy link
Member

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

@SydneyhSmith SydneyhSmith added Issue-Bug A bug to squash. Area-Debugging and removed Needs: Triage Maintainer attention needed! labels Aug 22, 2023
@andyleejordan
Copy link
Member

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".

andyleejordan added a commit that referenced this issue Aug 24, 2023
When the user has set `ErrorActionPreference` this could cause a crash,
so we have to override it. Resolves #2037.
@andyleejordan andyleejordan moved this to In Progress in Flying Fox Aug 24, 2023
github-merge-queue bot pushed a commit that referenced this issue Aug 25, 2023
When the user has set `ErrorActionPreference` this could cause a crash,
so we have to override it. Resolves #2037.
@github-project-automation github-project-automation bot moved this from In Progress to Done in Flying Fox Aug 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

3 participants