Skip to content

Fix error when hovering over invalid :nth-child() selector - #5455

Draft
Martin Aeschlimann (aeschli) with Copilot wants to merge 3 commits into
mainfrom
copilot/bugfix-error-nth-child-hover
Draft

Fix error when hovering over invalid :nth-child() selector#5455
Martin Aeschlimann (aeschli) with Copilot wants to merge 3 commits into
mainfrom
copilot/bugfix-error-nth-child-hover

Conversation

Copilot AI commented Aug 18, 2026

Copy link
Copy Markdown
Contributor

Hovering an invalid :nth-child() selector in a CSS model threw out of the worker and reached the editor's error handling:

TypeError: Cannot read properties of undefined (reading 'getText')
    at ... selectorToSpecificityMarkedString
    at ... doHover

Root cause is in vscode-css-languageservice specificity calculation, which reads childElements[1].getText() for :nth-child(...) without checking that a second child exists — still present in 6.3.10 and upstream main, so it can't be resolved by a version bump.

Changes

  • src/languages/features/css/cssWorker.ts: doHover catches errors from the language service and returns null, so an unparseable selector yields no hover instead of an unhandled rejection.
try {
    const hover = this._languageService.doHover(document, position, stylesheet);
    return Promise.resolve(hover);
} catch (e) {
    // The CSS language service can throw on invalid selectors, e.g. `:nth-child()`.
    return null;
}
  • src/languages/features/css/cssWorker.test.ts: regression test covering :nth-child() (no hover, no throw) and li:nth-child(2n of .important) (hover still produced).
  • package.json: test glob widened from src/languages/definitions/*/*.test.ts to src/languages/**/*.test.ts so feature-level tests run.

Notes for reviewers

The catch is deliberately scoped to hover only; other worker entry points are untouched. If the upstream parser bug is fixed and released, this guard becomes redundant but harmless.

Copilot AI and others added 2 commits August 18, 2026 18:30
Copilot AI changed the title [WIP] Fix error when hovering over invalid :nth-child instruction Fix error when hovering over invalid :nth-child() selector Aug 18, 2026
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.

[Bug] Error when hovering over invalid :nth-child instruction

2 participants