Ctrl + A on code in maximized window causes Exception

Open
#3,750 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
45/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Stale
Tech stack
csharp
Domain
cli

Research direction

Start by reproducing the issue in a small PowerShell window with the multiline scriptblock, maximize it, and invoke Ctrl+A. Read the SelectAll, Render, ReallyRender, and RecomputeInitialCoords stack entries; done means maximization reflows the command correctly and Ctrl+A no longer throws an InvalidOperationException.

Written by the indexing model from the issue text.

Description

Area-Stability Needs-More Investigation
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
Last 200 Keys:

 i l e n a m e Spacebar o f Spacebar t h e Spacebar X M L Spacebar w e l l - f o r m e d Enter
 Spacebar Spacebar $ ( $ u s i n g : C S D B M i g r a t i o n S t a t u s ) [ $ i ] . F i l e n a m e Spacebar = Spacebar $ x m l p a t h . F u l l N a m e Spacebar - r e p l a c e Spacebar ( [ r e g e x ] : : E s c a p e ( $ T p G x ) ) , Spacebar ' t p - g x ' Enter
 Enter
 Spacebar Spacebar # Spacebar R e l e a s e Spacebar t h e Spacebar s e m a p h o r e Enter
 Spacebar Spacebar $ ( $ u s i n g : s e m a p h o r e ) . R e l e a s e ( ) Enter
 } Ctrl+a

### Exception

System.InvalidOperationException: Cannot locate the offset in the rendered text that was pointed by the original cursor. Initial Coord: (35, -3) Buffer: (98, 30) Cursor: (3, 29)
   at Microsoft.PowerShell.PSConsoleReadLine.RecomputeInitialCoords(Boolean isTextBufferUnchanged)
   at Microsoft.PowerShell.PSConsoleReadLine.ReallyRender(RenderData renderData, String defaultColor)
   at Microsoft.PowerShell.PSConsoleReadLine.ForceRender()
   at Microsoft.PowerShell.PSConsoleReadLine.Render()
   at Microsoft.PowerShell.PSConsoleReadLine.RenderWithPredictionQueryPaused()
   at Microsoft.PowerShell.PSConsoleReadLine.SelectAll(Nullable`1 key, Object arg)
   at Microsoft.PowerShell.PSConsoleReadLine.ProcessOneKey(PSKeyInfo key, Dictionary`2 dispatchTable, Boolean ignoreIfNoAction, Object arg)
   at Microsoft.PowerShell.PSConsoleReadLine.InputLoop()
   at Microsoft.PowerShell.PSConsoleReadLine.ReadLine(Runspace runspace, EngineIntrinsics engineIntrinsics, CancellationToken cancellationToken, Nullable`1 lastRunStatus)
Screenshot

Screenshot

🛈 Note: Some data has been obscured for privacy.

Environment data
PS Version: 7.3.6
PS HostName: ConsoleHost (Windows Terminal)
PSReadLine Version: 2.3.1-beta1
PSReadLine EditMode: Windows
OS: 10.0.19041.320 (WinBuild.160101.0800)
Window size
Before maximizing
BufferWidth: 98
BufferHeight: 30
After maximizing
BufferWidth: 183
BufferHeight: 51
Steps to reproduce
  1. Small PowerShell window.
  2. Multiline scriptblock at the prompt.
$CSDBMigrationStatus = Import-Csv -Path "$env:OneDriveCommercial\Documents\csdbMigrationPlan.csv"  # A CSV export of the latest version of the CSDB Migration Plan XLSX file
$semaphore = [System.Threading.Semaphore]::new(0,1)  # A semaphore to limit the number of threads accessing the CSDB object concurrently
$CSDBMigrationStatus | Where-Object -FilterScript { -not [string]::IsNullOrEmpty($_.'Full Path') } | ForEach-Object -ThrottleLimit $env:NUMBER_OF_PROCESSORS -Parallel {  
# Get the export.log path for the manual export
$exportPath = $_.'Full Path' -replace 'tp-gx', $($using:TpGx) | Split-Path -Parent

# Get all the XML files in the export folder
$xmls = Get-ChildItem -Path $exportPath -Filter '*.xml' | Where-Object -FilterScript {-not (Select-String -Path $_ -Pattern 'This is DRAFT data to be used only for development')} 

# Get the latest XML with the 'well' keyword
$xmlPath = $xmls | Where-Object -FilterScript { $_.BaseName -match 'well' } | Sort-Object -Descending -Property CreationTimeUtc -Top 1

# If there is no such file, get the latest XML with the same name as the export folder
if ($null -eq $xmlPath -or $xmlPath.Count -eq 0) {
  $xmlPath = $xmls | Where-Object -FilterScript { $_.BaseName -match ([regex]::Escape(($exportPath | Split-Path -Leaf))) } | Sort-Object -Descending -Property CreationTimeUtc -Top 1
}

# Look up the index of the current item in the array
$i = $($using:CSDBMigrationStatus).IndexOf($_)

# Wait for the semaphore
$($using:semaphore).WaitOne()

# Set the filename of the XML well-formed
$($using:CSDBMigrationStatus)[$i].Filename = $xmlpath.FullName -replace ([regex]::Escape($TpGx)), 'tp-gx'

# Release the semaphore
$($using:semaphore).Release()
}
  1. Maximize window
  2. Select all using Ctrl + A
  3. Boom 💥 exception
Expected behavior

The original issue is that when I maximize, the text as displayed in the window is not the position of the editable underlying command text. That is, the maximize command isn't able to correctly reflow the command at the command prompt to anticipate where it is in the maximized context.

This is evident by the fact that moving the cursor around does not land in the correct place corresponding to the text as rendered. (I.e., the cursor does not edit the shown text and moving the cursor around doesn't follow the text as shown.)

To mitigate, I do a select all Ctrl + A to force the PowerShell to recalculate the text reflow. The highlighting will show be the boundaries of the text and allow me to understand how the text has moved.

Actual behavior

There is an exception thrown.

Dominant language
C#
Stars
4.4k
Forks
341
PR merge metrics
No merged PRs in 30d

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from PowerShell/PSReadLine

All issues in PowerShell/PSReadLine

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.