Skip to content

fix: guard against null diagnostic code in Copilot CLI diagnosticsChanged (fixes #332348) - #332355

Open
VS Code PR Bot (vscodebot-pr) wants to merge 2 commits into
microsoft:mainfrom
vscodebot-pr:fix/diagnostics-null-code-332348-aw-32745004652
Open

fix: guard against null diagnostic code in Copilot CLI diagnosticsChanged (fixes #332348)#332355
VS Code PR Bot (vscodebot-pr) wants to merge 2 commits into
microsoft:mainfrom
vscodebot-pr:fix/diagnostics-null-code-332348-aw-32745004652

Conversation

@vscodebot-pr

Copy link
Copy Markdown

Summary

The Copilot CLI diagnostics-changed notification crashes with TypeError: Cannot read properties of null (reading 'value') when serializing a diagnostic whose code is null. getDiagnosticsForUri uses typeof d.code === 'object' ? d.code.value : d.code, but typeof null === 'object' is true, so a null code falls into the object branch and dereferences null.value. Language servers and extensions are free to set Diagnostic.code to null, so this fires whenever such a diagnostic changes while a Copilot CLI session is active (50 users / 251 hits, new in extension 0.62.0).

Fixes #332348
Recommended reviewer: @alexweininger

Culprit Commit

Field Value
Commit ab48d553b302
Author @alexweininger
PR #3529
Message Migrate Copilot CLI integration (#3529)
Why This commit introduced getDiagnosticsForUri with the typeof d.code === 'object' ? d.code.value : d.code mapping at line 52. Because typeof null === 'object', a null code is routed into the object branch and .value is read off null. The code shipped in extension 0.62.0, which is when the bucket first appeared.

Code Flow

sequenceDiagram
    participant LS as Language Server / Extension
    participant API as vscode.languages.getDiagnostics
    participant Producer as getDiagnosticsForUri (map)
    participant CrashSite as d.code.value

    LS->>API: publishes Diagnostic with code = null
    API->>Producer: returns Diagnostic[] (code: null)
    Note over Producer: ⚠️ Root cause:<br/>typeof null === 'object' is true,<br/>so null enters the object branch
    Producer->>CrashSite: read d.code.value on null
    Note over CrashSite: TypeError: Cannot read<br/>properties of null (reading 'value')
Loading

Affected Files

File Role
extensions/copilot/src/extension/chatSessions/copilotcli/vscode-node/tools/push/diagnosticsChanged.ts Crash site and root cause — the code mapping on line 52

Repro Steps

  1. Open a Copilot CLI chat session so the diagnostics-changed notification is registered.
  2. Trigger a diagnostic from a language server/extension whose Diagnostic.code is null (rather than a string, number, or { value, target } object).
  3. When the diagnostics change fires, getDiagnosticsForUri maps the diagnostic and throws TypeError: Cannot read properties of null (reading 'value').

How the Fix Works

Chosen approachextensions/copilot/src/extension/chatSessions/copilotcli/vscode-node/tools/push/diagnosticsChanged.ts:52: tighten the type test to typeof d.code === 'object' && d.code !== null so that a null code falls through to the : d.code branch and is emitted as null, instead of being dereferenced. This is the correct location because the code value crosses an external/untrusted boundary — vscode.languages.getDiagnostics returns data authored by arbitrary language servers and extensions, which the consumer cannot constrain — so the guard belongs at the point where the value is first narrowed and consumed. The fix is the standard typeof x === 'object' && x !== null idiom, preserves the existing behavior for real { value, target } code objects and for string/number codes, and does not swallow or hide any error.

Alternatives considered: wrapping the .map body in try/catch was rejected because it would silence a legitimate serialization failure and hide it from telemetry rather than correctly serializing a valid (null) code.

Recommended Owner

@alexweininger — authored the migration commit that introduced getDiagnosticsForUri, is a VS Code team member with write access, and has landed commits to microsoft/vscode within the last 90 days.

Generated by errors-fix · opus48 · 325.6 AIC · ⌖ 11.2 AIC · ⊞ 18.6K ·

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Prevents Copilot CLI diagnostic notifications from crashing when a diagnostic code is null.

Changes:

  • Adds a null guard before reading Diagnostic.code.value.

💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

…on tests

Extracts a shared normalizeDiagnosticCode helper so both the diagnostics_changed
push notification and the get_diagnostics tool guard against a null Diagnostic.code
(typeof null === 'object'), and adds regression tests covering code: null.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@vs-code-engineering

Copy link
Copy Markdown
Contributor
Driver cycle recordederrors-fix-driver:cycle head:9f44f1b7812eb90ab160c177169f93cfa04f03bb

@alexweininger

Copy link
Copy Markdown
Member

cc: Don Jayamanne (@DonJayamanne)

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-Cannot read properties of null (reading 'value')

3 participants