Skip to content

REPL loop error, index out of range, must be non negative #1642

Closed
@ninmonkey

Description

@ninmonkey

About

Some regular errors are trigging this exception from editor services. The exact same code outside of vscode throws a parameter binding error like normal. It seems like there's a regular error, for a failed parameter binding, which is triggering an EditorServices crash/error

An error occurred while running the REPL loop:
System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')

image

Notes or Ideas

  • I started new windows, and respawned the PSIT to see if it was a one-off or not.
  • It is not specific to ConvertFrom-LiteralPath , it seems to occur on functions with Write-Error, but not all of them
  • inside vscode PSIT /w PSES it throws the REPL exception
  • code outside in wt throws just the parameter binding error.
  • the REPL exception "consumes" the real error, the parameter
  • The filepaths show drive d:\, I'm wondering if that's a path to one of the developers ? I have no d:\a\ directory
  • When reproducing with a minimal example, it seemed like if the function was in a module, then the REPL error would throw
  • Exact same code, outside of a module, didn't seem to work
  • Sometimes it seemed like Write-Error -ea Continue caused the REPL exception, verses stop or implicit
in D:\a\1\s\src\PowerShellEdi 
                 torServices\Services\PowerShell\Execution\SynchronousPowerShellTask.cs:line 120" value of   
                 type "System.Management.Automation.ActionPreferenceStopException" to type "System.Int32".   
TargetSite     : System.Object ThrowInvalidCastException(System.Object, System.Type)
Message        : Cannot convert the "System.Management.Automation.ActionPreferenceStopException: The
                 running command stopped because the preference variable "ErrorActionPreference" or common   
                 parameter is set to Stop: Cannot bind argument to parameter 'LiteralPath' because it is an  
                 empty string.
  • Is casting [Exception] as [int] normal ?

Code

I simplified the cause down to a single write-error. It triggered when imported from a module verses dotsourced externally.

function ConvertFrom-LiteralPath {
    <#
    .synopsis
        transform literal paths back into an EnvironmentVariable path
    #>
    [cmdletbinding()]
    param (
        # input LiteralPath to convert
        [Parameter(Mandatory, Position = 0, ValueFromPipeline)][string]$LiteralPath
    )
    begin {}
    process {
        Write-Error 'No matches'
    }
    end {}
}

Expected Behavior

PS> '' | BugFrom-LiteralPath
BugFrom-LiteralPath: Cannot bind argument to parameter 'LiteralPath' because it is an empty string.

image

Actual Behavior

Run

'' | ConvertFrom-LiteralPath

Output

An error occurred while running the REPL loop:
System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')       
   at System.Collections.Generic.List`1.get_Item(Int32 index)
   at Microsoft.PowerShell.EditorServices.Utility.PSCommandExtensions.MergePipelineResults(PSCommand psCommand) in D:\a\1\s\src\PowerShellEditorServices\Utility\PSCommandExtensions.cs:line 62
   at Microsoft.PowerShell.EditorServices.Utility.PSCommandExtensions.AddOutputCommand(PSCommand psCommand) in D:\a\1\s\src\PowerShellEditorServices\Utility\PSCommandExtensions.cs:line 45
   at Microsoft.PowerShell.EditorServices.Services.PowerShell.Execution.SynchronousPowerShellTask`1.ExecuteNormally(CancellationToken cancellationToken) in D:\a\1\s\src\PowerShellEditorServices\Services\PowerShell\Execution\SynchronousPowerShellTask.cs:line 120
   at Microsoft.PowerShell.EditorServices.Services.PowerShell.Execution.SynchronousPowerShellTask`1.Run(CancellationToken cancellationToken) in D:\a\1\s\src\PowerShellEditorServices\Services\PowerShell\Execution\SynchronousPowerShellTask.cs:line 55
   at Microsoft.PowerShell.EditorServices.Services.PowerShell.Execution.SynchronousTask`1.ExecuteSynchronously(CancellationToken executorCancellationToken) in D:\a\1\s\src\PowerShellEditorServices\Services\PowerShell\Execution\SynchronousTask.cs:line 104
--- End of stack trace from previous location ---
   at Microsoft.PowerShell.EditorServices.Services.PowerShell.Execution.SynchronousTask`1.get_Result() in D:\a\1\s\src\PowerShellEditorServices\Services\PowerShell\Execution\SynchronousTask.cs:line 60
   at Microsoft.PowerShell.EditorServices.Services.PowerShell.Host.PsesInternalHost.InvokeInput(String input, CancellationToken cancellationToken) in D:\a\1\s\src\PowerShellEditorServices\Services\PowerShell\Host\PsesInternalHost.cs:line 682
   at Microsoft.PowerShell.EditorServices.Services.PowerShell.Host.PsesInternalHost.DoOneRepl(CancellationToken cancellationToken) in D:\a\1\s\src\PowerShellEditorServices\Services\PowerShell\Host\PsesInternalHost.cs:line 654

Log

Warn  - 6:26:59 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: Cannot bind argument to parameter 'LiteralPath' because it is an empty string.
   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) in D:\a\1\s\src\PowerShellEditorServices\Services\PowerShell\Utility\PowerShellExtensions.cs:line 50
   at Microsoft.PowerShell.EditorServices.Services.PowerShell.Execution.SynchronousPowerShellTask`1.ExecuteNormally(CancellationToken cancellationToken) in D:\a\1\s\src\PowerShellEditorServices\Services\PowerShell\Execution\SynchronousPowerShellTask.cs:line 120 | 
[Error - 6:26:59 PM] Microsoft.PowerShell.EditorServices.Services.PowerShell.Host.PsesInternalHost: An error occurred while running the REPL loop - System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')
   at System.Collections.Generic.List`1.get_Item(Int32 index)
   at Microsoft.PowerShell.EditorServices.Utility.PSCommandExtensions.MergePipelineResults(PSCommand psCommand) in D:\a\1\s\src\PowerShellEditorServices\Utility\PSCommandExtensions.cs:line 62
   at Microsoft.PowerShell.EditorServices.Utility.PSCommandExtensions.AddOutputCommand(PSCommand psCommand) in D:\a\1\s\src\PowerShellEditorServices\Utility\PSCommandExtensions.cs:line 45
   at Microsoft.PowerShell.EditorServices.Services.PowerShell.Execution.SynchronousPowerShellTask`1.ExecuteNormally(CancellationToken cancellationToken) in D:\a\1\s\src\PowerShellEditorServices\Services\PowerShell\Execution\SynchronousPowerShellTask.cs:line 120
   at Microsoft.PowerShell.EditorServices.Services.PowerShell.Execution.SynchronousPowerShellTask`1.Run(CancellationToken cancellationToken) in D:\a\1\s\src\PowerShellEditorServices\Services\PowerShell\Execution\SynchronousPowerShellTask.cs:line 55
   at Microsoft.PowerShell.EditorServices.Services.PowerShell.Execution.SynchronousTask`1.ExecuteSynchronously(CancellationToken executorCancellationToken) in D:\a\1\s\src\PowerShellEditorServices\Services\PowerShell\Execution\SynchronousTask.cs:line 104
--- End of stack trace from previous location ---
   at Microsoft.PowerShell.EditorServices.Services.PowerShell.Execution.SynchronousTask`1.get_Result() in D:\a\1\s\src\PowerShellEditorServices\Services\PowerShell\Execution\SynchronousTask.cs:line 60
   at Microsoft.PowerShell.EditorServices.Services.PowerShell.Host.PsesInternalHost.InvokeInput(String input, CancellationToken cancellationToken) in D:\a\1\s\src\PowerShellEditorServices\Services\PowerShell\Host\PsesInternalHost.cs:line 682
   at Microsoft.PowerShell.EditorServices.Services.PowerShell.Host.PsesInternalHost.DoOneRepl(CancellationToken cancellationToken) in D:\a\1\s\src\PowerShellEditorServices\Services\PowerShell\Host\PsesInternalHost.cs:line 654 | 

Environment

Name Version
code 1.63.0-insider, 0cc0904c565399781defa830facf43141db8b6f3, x64
PSVersionTable within PSIT 7.2.0
Installed Addon Version
justin-grote.powershell-extension-pack 0.0.5
ms-vscode.powershell-preview 2021.11.1
ms-vscode.test-adapter-converter 0.1.4
pspester.pester-test 2021.10.2
TylerLeonhardt.vscode-inline-values-powershell 0.0.5
TylerLeonhardt.vscode-pester-test-adapter 0.0.23
Module Version
EditorServicesCommandSuite 1.0.0
Pansies 2.3.1
posh-git 1.0.0
PowerShellEditorServices.Commands 0.2.0
PowerShellEditorServices.VSCode 0.2.0
PSReadLine 2.2.0

Logs

About

Some regular errors are trigging this exception from editor services. The exact same code outside of vscode throws a parameter binding error like normal. It seems like there's a regular error, for a failed parameter binding, which is triggering an EditorServices crash/error

An error occurred while running the REPL loop:
System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')

image

Notes or Ideas

  • I started new windows, and respawned the PSIT to see if it was a one-off or not.
  • It is not specific to ConvertFrom-LiteralPath , it seems to occur on functions with Write-Error, but not all of them
  • inside vscode PSIT /w PSES it throws the REPL exception
  • code outside in wt throws just the parameter binding error.
  • the REPL exception "consumes" the real error, the parameter
  • The filepaths show drive d:\, I'm wondering if that's a path to one of the developers ? I have no d:\a\ directory
  • When reproducing with a minimal example, it seemed like if the function was in a module, then the REPL error would throw
  • Exact same code, outside of a module, didn't seem to work
  • Sometimes it seemed like Write-Error -ea Continue caused the REPL exception, verses stop or implicit
in D:\a\1\s\src\PowerShellEdi 
                 torServices\Services\PowerShell\Execution\SynchronousPowerShellTask.cs:line 120" value of   
                 type "System.Management.Automation.ActionPreferenceStopException" to type "System.Int32".   
TargetSite     : System.Object ThrowInvalidCastException(System.Object, System.Type)
Message        : Cannot convert the "System.Management.Automation.ActionPreferenceStopException: The
                 running command stopped because the preference variable "ErrorActionPreference" or common   
                 parameter is set to Stop: Cannot bind argument to parameter 'LiteralPath' because it is an  
                 empty string.
  • Is casting [Exception] as [int] normal ?

Code

I simplified the cause down to a single write-error. It triggered when imported from a module verses dotsourced externally.

function ConvertFrom-LiteralPath {
    <#
    .synopsis
        transform literal paths back into an EnvironmentVariable path
    #>
    [cmdletbinding()]
    param (
        # input LiteralPath to convert
        [Parameter(Mandatory, Position = 0, ValueFromPipeline)][string]$LiteralPath
    )
    begin {}
    process {
        Write-Error 'No matches'
    }
    end {}
}

Expected Behavior

PS> '' | BugFrom-LiteralPath
BugFrom-LiteralPath: Cannot bind argument to parameter 'LiteralPath' because it is an empty string.

image

Actual Behavior

Run

'' | ConvertFrom-LiteralPath

Output

An error occurred while running the REPL loop:
System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')       
   at System.Collections.Generic.List`1.get_Item(Int32 index)
   at Microsoft.PowerShell.EditorServices.Utility.PSCommandExtensions.MergePipelineResults(PSCommand psCommand) in D:\a\1\s\src\PowerShellEditorServices\Utility\PSCommandExtensions.cs:line 62
   at Microsoft.PowerShell.EditorServices.Utility.PSCommandExtensions.AddOutputCommand(PSCommand psCommand) in D:\a\1\s\src\PowerShellEditorServices\Utility\PSCommandExtensions.cs:line 45
   at Microsoft.PowerShell.EditorServices.Services.PowerShell.Execution.SynchronousPowerShellTask`1.ExecuteNormally(CancellationToken cancellationToken) in D:\a\1\s\src\PowerShellEditorServices\Services\PowerShell\Execution\SynchronousPowerShellTask.cs:line 120
   at Microsoft.PowerShell.EditorServices.Services.PowerShell.Execution.SynchronousPowerShellTask`1.Run(CancellationToken cancellationToken) in D:\a\1\s\src\PowerShellEditorServices\Services\PowerShell\Execution\SynchronousPowerShellTask.cs:line 55
   at Microsoft.PowerShell.EditorServices.Services.PowerShell.Execution.SynchronousTask`1.ExecuteSynchronously(CancellationToken executorCancellationToken) in D:\a\1\s\src\PowerShellEditorServices\Services\PowerShell\Execution\SynchronousTask.cs:line 104
--- End of stack trace from previous location ---
   at Microsoft.PowerShell.EditorServices.Services.PowerShell.Execution.SynchronousTask`1.get_Result() in D:\a\1\s\src\PowerShellEditorServices\Services\PowerShell\Execution\SynchronousTask.cs:line 60
   at Microsoft.PowerShell.EditorServices.Services.PowerShell.Host.PsesInternalHost.InvokeInput(String input, CancellationToken cancellationToken) in D:\a\1\s\src\PowerShellEditorServices\Services\PowerShell\Host\PsesInternalHost.cs:line 682
   at Microsoft.PowerShell.EditorServices.Services.PowerShell.Host.PsesInternalHost.DoOneRepl(CancellationToken cancellationToken) in D:\a\1\s\src\PowerShellEditorServices\Services\PowerShell\Host\PsesInternalHost.cs:line 654

Log

Warn  - 6:26:59 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: Cannot bind argument to parameter 'LiteralPath' because it is an empty string.
   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) in D:\a\1\s\src\PowerShellEditorServices\Services\PowerShell\Utility\PowerShellExtensions.cs:line 50
   at Microsoft.PowerShell.EditorServices.Services.PowerShell.Execution.SynchronousPowerShellTask`1.ExecuteNormally(CancellationToken cancellationToken) in D:\a\1\s\src\PowerShellEditorServices\Services\PowerShell\Execution\SynchronousPowerShellTask.cs:line 120 | 
[Error - 6:26:59 PM] Microsoft.PowerShell.EditorServices.Services.PowerShell.Host.PsesInternalHost: An error occurred while running the REPL loop - System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')
   at System.Collections.Generic.List`1.get_Item(Int32 index)
   at Microsoft.PowerShell.EditorServices.Utility.PSCommandExtensions.MergePipelineResults(PSCommand psCommand) in D:\a\1\s\src\PowerShellEditorServices\Utility\PSCommandExtensions.cs:line 62
   at Microsoft.PowerShell.EditorServices.Utility.PSCommandExtensions.AddOutputCommand(PSCommand psCommand) in D:\a\1\s\src\PowerShellEditorServices\Utility\PSCommandExtensions.cs:line 45
   at Microsoft.PowerShell.EditorServices.Services.PowerShell.Execution.SynchronousPowerShellTask`1.ExecuteNormally(CancellationToken cancellationToken) in D:\a\1\s\src\PowerShellEditorServices\Services\PowerShell\Execution\SynchronousPowerShellTask.cs:line 120
   at Microsoft.PowerShell.EditorServices.Services.PowerShell.Execution.SynchronousPowerShellTask`1.Run(CancellationToken cancellationToken) in D:\a\1\s\src\PowerShellEditorServices\Services\PowerShell\Execution\SynchronousPowerShellTask.cs:line 55
   at Microsoft.PowerShell.EditorServices.Services.PowerShell.Execution.SynchronousTask`1.ExecuteSynchronously(CancellationToken executorCancellationToken) in D:\a\1\s\src\PowerShellEditorServices\Services\PowerShell\Execution\SynchronousTask.cs:line 104
--- End of stack trace from previous location ---
   at Microsoft.PowerShell.EditorServices.Services.PowerShell.Execution.SynchronousTask`1.get_Result() in D:\a\1\s\src\PowerShellEditorServices\Services\PowerShell\Execution\SynchronousTask.cs:line 60
   at Microsoft.PowerShell.EditorServices.Services.PowerShell.Host.PsesInternalHost.InvokeInput(String input, CancellationToken cancellationToken) in D:\a\1\s\src\PowerShellEditorServices\Services\PowerShell\Host\PsesInternalHost.cs:line 682
   at Microsoft.PowerShell.EditorServices.Services.PowerShell.Host.PsesInternalHost.DoOneRepl(CancellationToken cancellationToken) in D:\a\1\s\src\PowerShellEditorServices\Services\PowerShell\Host\PsesInternalHost.cs:line 654 | 

Environment

Name Version
code 1.63.0-insider, 0cc0904c565399781defa830facf43141db8b6f3, x64
PSVersionTable within PSIT 7.2.0
Installed Addon Version
justin-grote.powershell-extension-pack 0.0.5
ms-vscode.powershell-preview 2021.11.1
ms-vscode.test-adapter-converter 0.1.4
pspester.pester-test 2021.10.2
TylerLeonhardt.vscode-inline-values-powershell 0.0.5
TylerLeonhardt.vscode-pester-test-adapter 0.0.23
Module Version
EditorServicesCommandSuite 1.0.0
Pansies 2.3.1
posh-git 1.0.0
PowerShellEditorServices.Commands 0.2.0
PowerShellEditorServices.VSCode 0.2.0
PSReadLine 2.2.0

Logs

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions