Repository: microsoft/terminal (language: cpp)

Issue:
Cursor droppings in WT cmd shell
### Windows Terminal version

Commmit 75f7ae4bec2f859b3f54043bd5c1a36161885a4d

### Windows build number

10.0.19045.4651

### Other Software

_No response_

### Steps to reproduce

I don't know how many of these steps are strictly required, but this is what reproduces the issue for me quite consistently:

1. Open a cmd shell in Windows Terminal.
2. Create a directory with 6 files in it.
3. Make sure the screen height is 24 rows.
4. Clear the screen with `cls`.
5. Execute `dir` twice in a row.


### Expected Behavior

Only one instance of the cursor should be visible on the screen.

### Actual Behavior

There is an extra zombie cursor dropping left after the second `dir` is typed.

![image](https://github.com/user-attachments/assets/a79bd285-8896-4e2f-a92e-21024d287625)

It happens quite consistently for me, but not _every_ time, and it disappears quite easily - switching to another window and back again is usually enough. That made it very difficult to screenshot. I had to take a video and save one of the frames.

I can't reproduce it in the preview build (1.21.1272.0), so I'm assuming it's a regression. I'll try and do a git bisect later to see if I can figure out the exact commit.


Discussion / hints:
It looks like commit 0a2b660e649bfc07b5142640b3fea56b1d8f564c is the cause.

Fix patch:
diff --git a/src/renderer/base/renderer.cpp b/src/renderer/base/renderer.cpp
index 511563a0e1c..8192404b9bc 100644
--- a/src/renderer/base/renderer.cpp
+++ b/src/renderer/base/renderer.cpp
@@ -435,8 +435,9 @@ bool Renderer::_CheckViewportAndScroll()
         auto coordCursor = _currentCursorOptions.coordCursor;
 
         // `coordCursor` was stored in viewport-relative while `view` is in absolute coordinates.
-        // --> Turn it back into the absolute coordinates with the help of the old viewport.
-        coordCursor.y += srOldViewport.top;
+        // --> Turn it back into the absolute coordinates with the help of the viewport.
+        // We have to use the new viewport, because _ScrollPreviousSelection adjusts the cursor position to match the new one.
+        coordCursor.y += srNewViewport.top;
 
         // Note that we allow the X coordinate to be outside the left border by 1 position,
         // because the cursor could still be visible if the focused character is double width.


Test patch: