Skip to content

fix: guard against invalid line in provideAltTextQuickFix (fixes #318227)#318228

Merged
vs-code-engineering[bot] merged 1 commit into
mainfrom
fix/illegal-line-value-alt-text-318227-130469fa504e557f
May 25, 2026
Merged

fix: guard against invalid line in provideAltTextQuickFix (fixes #318227)#318228
vs-code-engineering[bot] merged 1 commit into
mainfrom
fix/illegal-line-value-alt-text-318227-130469fa504e557f

Conversation

@vs-code-engineering
Copy link
Copy Markdown
Contributor

🔧 Error Fix

Summary

The provideAltTextQuickFix method in the Copilot Chat extension's code action provider calls document.lineAt(range.start.line) without validating that the line number is within the document's bounds. When the document is modified between when VS Code computes the code action range and when the provider executes, range.start.line can exceed document.lineCount, causing TextDocument.lineAt() to throw "Illegal value for line".

This error has been present across many extension versions (0.38.2 through 0.49.0) with ~10k+ total hits affecting ~4000+ users, indicating a persistent race condition in the code action lifecycle.

Fixes #318227
Recommended reviewer: @AidanImmer

Culprit Commit

Not identified — this is a long-standing bug present across all tracked extension versions (since at least 0.38.2). The provideAltTextQuickFix method has always lacked bounds validation on the range parameter.

Code Flow

sequenceDiagram
    participant VSCode as VS Code Editor
    participant QFP as QuickFixesProvider
    participant Doc as TextDocument

    VSCode->>QFP: provideCodeActions(doc, range, ...)
    Note over VSCode: Document modified after range computed
    QFP->>QFP: provideAltTextQuickFix(doc, range)
    QFP->>Doc: lineAt(range.start.line)
    Note over Doc: line >= lineCount
    Doc-->>QFP: throws "Illegal value for line"
Loading

Affected Files

File Role
extensions/copilot/src/extension/inlineChat/vscode-node/inlineChatCodeActions.ts Crash site and fix location — provideAltTextQuickFix calls lineAt() without bounds check

Repro Steps

  1. Open a markdown file with image references in VS Code with Copilot Chat enabled
  2. Rapidly edit the document (adding/removing lines) while the code action provider is triggered
  3. The race condition between document modification and code action evaluation causes an out-of-bounds line number to be passed to lineAt()

How the Fix Works

Chosen approach (extensions/copilot/src/extension/inlineChat/vscode-node/inlineChatCodeActions.ts):

Added a guard clause at the entry of provideAltTextQuickFix that validates range.start.line is within [0, document.lineCount) before calling document.lineAt(). If the line is out of bounds, the method returns undefined early (no alt-text quick fix offered for that invocation). This follows the principle of adding guard clauses upstream of the crash site to prevent the error path from being reached, rather than wrapping in try/catch which would hide the error from telemetry.

The range is produced by VS Code's code action infrastructure and can become stale due to concurrent document edits — this is a known pattern where the consumer must validate framework-provided ranges.

Recommended Owner

@AidanImmer — owner of the Copilot Chat extension inline chat code actions based on the file path in the copilot extension area.

Generated by errors-fix · ● 17.8M ·

)

The provideAltTextQuickFix method calls document.lineAt(range.start.line)
without validating that the line number is within bounds. When the document
is modified between when VS Code computes the range and when the code action
provider executes, an out-of-bounds line number causes 'Illegal value for
`line`' to be thrown.

Add a bounds check before calling lineAt() so the method returns early
instead of throwing.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings May 25, 2026 15:51
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

@vs-code-engineering vs-code-engineering Bot marked this pull request as ready for review May 25, 2026 15:52
@vs-code-engineering vs-code-engineering Bot enabled auto-merge (squash) May 25, 2026 15:52
@vs-code-engineering vs-code-engineering Bot merged commit f5a135b into main May 25, 2026
25 checks passed
@vs-code-engineering vs-code-engineering Bot deleted the fix/illegal-line-value-alt-text-318227-130469fa504e557f branch May 25, 2026 20:11
@vs-code-engineering vs-code-engineering Bot added this to the 1.123.0 milestone May 25, 2026
dileepyavan pushed a commit that referenced this pull request May 27, 2026
) (#318228)

The provideAltTextQuickFix method calls document.lineAt(range.start.line)
without validating that the line number is within bounds. When the document
is modified between when VS Code computes the range and when the code action
provider executes, an out-of-bounds line number causes 'Illegal value for
`line`' to be thrown.

Add a bounds check before calling lineAt() so the method returns early
instead of throwing.

Co-authored-by: vs-code-engineering[bot] <122617954+vs-code-engineering[bot]@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Error] [GitHub.copilot-chat] unhandlederror-Illegal value for line

3 participants