Skip to content

ArgumentOutOfRangeException Error #4298

@SleepTheGod

Description

@SleepTheGod

Prerequisites

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

Exception report

Oops, something went wrong.  Please report this bug with the details below.
Report on GitHub: https://github.com/lzybkr/PSReadLine/issues/new
-----------------------------------------------------------------------
Last 98 Keys:
 h i s t o r y Enter
 UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow UpArrow Ctrl+z UpArrow UpArrow DownArrow

Exception:
System.ArgumentOutOfRangeException: The value must be greater than or equal to zero and less than the console's buffer size in that dimension.
Parameter name: top
Actual value was -45.
   at System.Console.SetCursorPosition(Int32 left, Int32 top)
   at Microsoft.PowerShell.PSConsoleReadLine.ReallyRender(RenderData renderData, String defaultColor)
   at Microsoft.PowerShell.PSConsoleReadLine.ForceRender()
   at Microsoft.PowerShell.PSConsoleReadLine.HistoryRecall(Int32 direction)
   at Microsoft.PowerShell.PSConsoleReadLine.NextHistory(Nullable`1 key, Object arg)
   at Microsoft.PowerShell.PSConsoleReadLine.ProcessOneKey(ConsoleKeyInfo key, Dictionary`2 dispatchTable, Boolean ignoreIfNoAction, Object arg)
   at Microsoft.PowerShell.PSConsoleReadLine.InputLoop()
   at Microsoft.PowerShell.PSConsoleReadLine.ReadLine(Runspace runspace, EngineIntrinsics engineIntrinsics)
-----------------------------------------------------------------------

This error occurs in PowerShell's PSReadLine module when navigating command history with the UpArrow and DownArrow keys. The issue arises because the cursor is set to an invalid position, typically caused by a mismatch between the console's visible buffer size and its internal rendering logic, such as when the terminal window is resized or history navigation exceeds the buffer's bounds. The error, System.ArgumentOutOfRangeException, indicates that a negative or out-of-range value (top = -45) was passed to the console's SetCursorPosition method. To resolve this, you can reset or update PSReadLine, adjust the console buffer size, or use Get-History for safer command recall.

Screenshot

image

Environment data

current

Steps to reproduce

Steps to Reproduce the Issue
Open a PowerShell terminal.
Execute a few commands to populate the command history (e.g., echo "test1", echo "test2", etc.).
Rapidly press the UpArrow key multiple times to scroll through the command history.
If the terminal window has been resized or its buffer size is misconfigured, continue pressing UpArrow or DownArrow to trigger the exception.

Expected behavior

Expected Behavior
Scrolling through the command history should navigate past commands without any errors.
The terminal cursor should remain within the visible console buffer.

Actual behavior

System.ArgumentOutOfRangeException: The value must be greater than or equal to zero and less than the console's buffer size in that dimension.
Parameter name: top
Actual value was -45.

$host.UI.RawUI.BufferSize = New-Object Management.Automation.Host.Size(10, 20) # Shrink buffer size
echo "Command 1"
echo "Command 2"
echo "Command 3"

Press UpArrow repeatedly after resizing

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions