-
Notifications
You must be signed in to change notification settings - Fork 37.2k
Fix querySelector TypeError in terminal suggest when xterm element becomes undefined #285970
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
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>
There was a problem hiding this 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
querySelectorcall onxtermElementin_prepareAddonLayoutmethod 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'); |
Copilot
AI
Jan 5, 2026
There was a problem hiding this comment.
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.
| const screenElement = xtermElement?.querySelector('.xterm-screen'); | |
| const screenElement = xtermElement.querySelector('.xterm-screen'); |
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
querySelectorcall in_prepareAddonLayout: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
xtermElementis nullish,screenElementbecomesundefinedand the subsequentisHTMLElementcheck handles it gracefully.Original prompt
💡 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.