microsoft / microsoft/vscode

fix: guard null visible range in indent guides rendering (fixes #244370)

Open
#328,139 0 comments 0 reactions 1 assignee Claimed by @hediet View on GitHub
agentic-workflows errors-fix
Dominant language
TypeScript
Stars
193k
Forks
42.4k
PR merge metrics
PR metrics pending

Description

### Summary
The indent guides overlay throws `TypeError: Cannot read properties of null (reading 'left')` in `IndentGuidesOverlay.prepareRender`. When computing the horizontal offset for a bracket/indent guide, the code calls `ctx.visibleRangeForPosition(...)` and dereferences the result with a non-null assertion (`!.left`). That method is declared to return `HorizontalPosition | null` and legitimately returns `null` when the requested position has no visible/rendered range (e.g. the target column is not currently laid out). The `!` bypasses the nullable contract, so the null result is dereferenced and throws.

Fixes microsoft/vscode\#244370
Recommended reviewer: `@hediet`

### Culprit Commit
`78cb0b490a60` (Henning Dieterichs, 2022-04-01) introduced the guide-offset computation with the `!.left` non-null assertion on `visibleRangeForPosition(...)`.

### Code Flow
```mermaid
flowchart TD
A[view.ts render loop] --> B[viewOverlays.prepareRender]
B --> C[IndentGuidesOverlay.prepareRender]
C --> D[loop over guides]
D --> E["ctx.visibleRangeForPosition(pos)"]
E -->|returns HorizontalPosition or null| F["!.left non-null assertion"]
F -->|value is null| G[TypeError: reading 'left']
```

### Affected Files
- `src/vs/editor/browser/viewParts/indentGuides/indentGuides.ts` — `IndentGuidesOverlay.prepareRender`

### Repro Steps
Not reliably reproducible on demand; occurs during editor rendering when `visibleRangeForPosition` returns `null` for a guide's column (position not currently rendered, e.g. transient layout/scroll state or DOM/GPU view-line race). The surrounding code already anticipates this: the sibling calls at the same site use `?? leftOffset` (line 132) and `?? (left + this._spaceWidth)` (lines 148-150), confirming `null` is an expected return value here.

### How the Fix Works
**Chosen approach** (`src/vs/editor/browser/viewParts/indentGuides/indentGuides.ts`): replace the `!.left` non-null assertion with `?.left ?? leftOffset`. The producer `RenderingContext.visibleRangeForPosition` has a genuinely nullable declared type (`HorizontalPosition | null`, returned as `... ?? null` in `renderingContext.ts:103`), and no upstream bypass introduces the null — `null` is the intended signal that the position is not currently rendered. Removing the `!` bypass and falling back to `leftOffset` (the same fallback already used one branch above for the `guide.column === -1` case) is the priority-one fix per the type-contract guidance: remove the bypass of a legitimately-nullable value rather than force-assert it. This is not a masking guard — the declared type is already nullable and the sibling branches at this exact site already coalesce the same null, so the change simply makes the third branch consistent with the contract.

**Alternatives considered**: wrapping the loop body in try/catch was rejected because it would hide the condition from telemetry rather than handle the legitimately-nullable return value; guarding further upstream was rejected because the null does not originate from a type-system bypass — `visibleRangeForPosition` correctly returns `null` and each consumer is expected to handle it.

### Recommended Owner
`@hediet` — original author of the guide-offset computation (commit `78cb0b490a60`) and current assignee of the issue.

> Generated by [errors-fix](https://github.com/microsoft/vscode-engineering/actions/runs/30501964666) · opus48 · 125 AIC · ⌖ 17.3 AIC · ⊞ 18.1K · [◷](https://github.com/search?q=repo%3Amicrosoft%2Fvscode+%22gh-aw-workflow-id%3A+errors-fix%22&type=pullrequests)

---

> [!NOTE]
> This was originally intended as a pull request, but the git push operation failed.
>
> **Original error:** The process '/usr/bin/git' failed with exit code 128
>
> **Workflow Run:** [View run details and download bundle artifact](https://github.com/microsoft/vscode-engineering/actions/runs/30501964666)
>
> The bundle file is available in the `agent` artifact in the workflow run linked above.

To create a pull request with the changes:

```sh
# Download the artifact from the workflow run
gh run download 30501964666 -n agent -D /tmp/agent-30501964666

# Fetch the bundle into a temporary ref, then update the local branch
git fetch /tmp/agent-30501964666/aw-microsoft-vscode-fix-indent-guides-null-left.bundle refs/heads/fix/indent-guides-null-left:refs/bundles/create-pr-fix-indent-guides-null-left-07869df527ad0a39-242a9e73
git update-ref refs/heads/fix/indent-guides-null-left-07869df527ad0a39 refs/bundles/create-pr-fix-indent-guides-null-left-07869df527ad0a39-242a9e73
git checkout fix/indent-guides-null-left-07869df527ad0a39
# Ensure the working tree matches the updated branch
git reset --hard
# Remove the temporary bundle ref
git update-ref -d refs/bundles/create-pr-fix-indent-guides-null-left-07869df527ad0a39-242a9e73

# Push the branch to origin
git push https://github.com/bryanchen-d/vscode.git fix/indent-guides-null-left-07869df527ad0a39

# Create the pull request
gh pr create --title 'fix: guard null visible range in indent guides rendering (fixes #244370)' --base main --head bryanchen-d:fix/indent-guides-null-left-07869df527ad0a39 --repo microsoft/vscode
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.