Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 5, 2026

The terminal suggest contribution crashes with TypeError: Cannot read properties of undefined (reading 'querySelector') when the xterm element becomes unavailable during layout preparation, particularly during configuration changes.

Changes

Added optional chaining to the querySelector call in _prepareAddonLayout:

// Before
const screenElement = xtermElement.querySelector('.xterm-screen');

// After  
const screenElement = xtermElement?.querySelector('.xterm-screen');

The existing null check at line 228 doesn't catch all edge cases where the element becomes invalid between validation and usage. Optional chaining provides defense-in-depth without altering control flow—if xtermElement is nullish, screenElement becomes undefined and the subsequent isHTMLElement check handles it gracefully.

Original prompt

This section details on the original issue you should resolve

<issue_title>Cannot read properties of undefined (reading 'querySelector')</issue_title>
<issue_description>```javascript
TypeError: Cannot read properties of undefined (reading 'querySelector')
at vb.y in vs/workbench/contrib/terminalContrib/suggest/browser/terminal.suggest.contribution.ts:169:34
at Sce.value in vs/workbench/contrib/terminalContrib/suggest/browser/terminal.suggest.contribution.ts:87:11
at E.C in ./file:/Users/cloudtest/vss/_work/1/s/src/vs/base/common/event.ts:1202:13
at E.D in ./file:/Users/cloudtest/vss/_work/1/s/src/vs/base/common/event.ts:1213:9
at E.fire in ./file:/Users/cloudtest/vss/_work/1/s/src/vs/base/common/event.ts:1237:9
at _Cn.Lb in vs/workbench/services/configuration/browser/configurationService.ts:1121:35
at _Cn.tb in vs/workbench/services/configuration/browser/configurationService.ts:768:9
at Sce.value in vs/workbench/services/configuration/browser/configurationService.ts:155:104
at E.C in ./file:/Users/cloudtest/vss/_work/1/s/src/vs/base/common/event.ts:1202:13
at E.D in ./file:/Users/cloudtest/vss/_work/1/s/src/vs/base/common/event.ts:1213:9

[Go to Errors Site](https://errors.code.visualstudio.com/card?ch=994fd12f8d3a5aa16f17d42c041e5809167e845a&bH=185f799e-a031-8036-240c-d20e1f6b4d9c)</issue_description>

## Comments on the Issue (you are @copilot in this section)

<comments>
</comments>


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Adds optional chaining operator to prevent "Cannot read properties of undefined (reading 'querySelector')" error in _prepareAddonLayout method. This handles edge cases where xtermElement might become invalid between the null check and the querySelector call.

Co-authored-by: meganrogge <29464607+meganrogge@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix TypeError when reading querySelector property Fix querySelector TypeError in terminal suggest when xterm element becomes undefined Jan 5, 2026
Copilot AI requested a review from meganrogge January 5, 2026 17:14
@meganrogge meganrogge marked this pull request as ready for review January 5, 2026 17:50
Copilot AI review requested due to automatic review settings January 5, 2026 17:51
@meganrogge meganrogge added this to the December 2025 milestone Jan 5, 2026
@meganrogge meganrogge enabled auto-merge (squash) January 5, 2026 17:51
Copy link
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.

Pull request overview

This PR fixes a TypeError crash in the terminal suggest contribution where xtermElement.querySelector fails when the xterm element becomes undefined during configuration changes or other async operations. The fix adds optional chaining (?.) to safely handle the case where xtermElement is nullish.

Key Changes

  • Added optional chaining to the querySelector call on xtermElement in _prepareAddonLayout method to prevent TypeError when the element becomes undefined

addon.setContainerWithOverflow(container);
// eslint-disable-next-line no-restricted-syntax
const screenElement = xtermElement.querySelector('.xterm-screen');
const screenElement = xtermElement?.querySelector('.xterm-screen');
Copy link

Copilot AI Jan 5, 2026

Choose a reason for hiding this comment

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

The optional chaining added to line 235 suggests that xtermElement can become undefined/null after the check on line 228. However, line 232 passes xtermElement to _resolveAddonContainer without optional chaining, and that method expects a non-null HTMLElement parameter. If xtermElement can become invalid between line 228 and line 235 (as the fix implies), then line 232 would also fail with the same race condition. Consider adding an early return or additional null check after line 232 if xtermElement could be nullish, or apply consistent defensive programming across all usages of xtermElement in this code block.

Suggested change
const screenElement = xtermElement?.querySelector('.xterm-screen');
const screenElement = xtermElement.querySelector('.xterm-screen');

Copilot uses AI. Check for mistakes.
@meganrogge meganrogge merged commit de56ab1 into main Jan 5, 2026
27 of 28 checks passed
@meganrogge meganrogge deleted the copilot/fix-queryselector-typeerror branch January 5, 2026 18:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cannot read properties of undefined (reading 'querySelector')

3 participants