Skip to content

Avoid :has() selectors that trigger workbench-wide style invalidation - #327052

Merged
Benjamin Christopher Simmonds (benibenj) merged 9 commits into
microsoft:mainfrom
zmr-233:fix/workbench-has-invalidation
Jul 27, 2026
Merged

Benjamin Christopher Simmonds (benibenj) merged 9 commits into
microsoft:mainfrom
zmr-233:fix/workbench-has-invalidation

Conversation

@zmr-233

@zmr-233 zmr-233 commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Fixes #327048

A :has() whose anchor exists in the live DOM makes every DOM mutation, including the inline letter-spacing writes the editor performs on each layout, pay a style recalc that scales with the whole visible workbench (measurements and per-rule profiling in the issue). The single biggest offender is the chat working set scrollbar rule; the root-anchored rules in floatingPanels.css and sessionsPolicyBlocked.css are the same hazard and the same class of problem that #324986 already fixed for the automations dialog.

Changes:

  • chat.css: the working set scrollbar padding is keyed off an overflowing class toggled in chatInputPart.ts, which already computes the overflow condition (allEntries.length > maxItemsShown), instead of :has(.visible.scrollbar.vertical). Behavior note: the padding now applies whenever the list overflows, not only while the auto-hide scrollbar is faded in. This also stops the labels from shifting under the scrollbar mid-scroll.
  • floatingPanels.css: the two root-anchored :has() rules are replaced by activitybar-compact (mirrored onto the workbench root by activitybarPart.ts) and noactivitybar (toggled by layout.ts like the other part visibility classes). A :not(.noactivitybar) on the compact rule keeps the hidden+compact case resolving to the hidden inset, matching the specificity behavior of the old rules.
  • sessionsPolicyBlocked.css: .monaco-workbench:has(.sessions-policy-blocked-overlay) becomes a class toggled for the overlay's lifetime.
  • build/stylelint.ts: new check that errors on :has() anchored to a root compound (body / html / :root / .monaco-workbench), with a stylelint-disable has-anchor-checker escape. It flags all three historical instances (including the original UI performance degrades after opening Hover, Command Palette, or main menu #324985 one) and has no hits on current src/**/*.css.

Testing:

  • Runtime equivalent of this change (deleting the offending rules from CSSOM plus one full restyle) takes the forced-recalc microbenchmark from 13-16ms to 0.04ms on my machine and makes sash dragging smooth again; details in the issue.
  • Verified sash dragging, working set overflow padding (7+ files in the working set), compact/hidden activity bar insets under floating panels, in a local dev build.
  • Lint check verified against the three historical bad selectors (all flagged) and a full run over src/**/*.css (no findings).

A :has() whose anchor exists in the live DOM makes Blink re-evaluate it on
DOM mutations anywhere in the document - including the inline style writes
the editor performs on every layout. With a populated chat working set,
every forced style recalc traversed the whole visible workbench (~218ms
per Recalculate Style in a large workspace; sash drag nearly frozen).
Same issue class as microsoft#324985.

Per-rule profiling showed the chat editing session scrollbar rule alone
accounted for 77% of the per-recalc cost; the root-anchored rules in
floatingPanels.css and sessionsPolicyBlocked.css are the same hazard.

- chat.css: key the scrollbar padding off an 'overflowing' class toggled
  in chatInputPart.ts, which already computes the condition. The padding
  now applies whenever the list overflows instead of only while the
  auto-hide scrollbar is faded in.
- floatingPanels.css: use 'activitybar-compact' (mirrored by
  activitybarPart.ts) and 'noactivitybar' (toggled by layout.ts like the
  other part visibility classes).
- sessionsPolicyBlocked.css: toggle a class for the overlay's lifetime.
- build/stylelint.ts: error on root-anchored :has() (body/html/:root/
  .monaco-workbench), with a has-anchor-checker disable escape.
Copilot AI review requested due to automatic review settings July 22, 2026 20:14
@vs-code-engineering

Copy link
Copy Markdown
Contributor

📬 CODENOTIFY

The following users are being notified based on files changed in this PR:

Benjamin Christopher Simmonds (@benibenj)

Matched files:

  • src/vs/workbench/browser/parts/activitybar/activitybarPart.ts

@zmr-233

zmr-233 commented Jul 22, 2026

Copy link
Copy Markdown
Contributor Author

@microsoft-github-policy-service agree

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

Replaces workbench-rooted :has() selectors with explicit state classes to avoid costly global style recalculation.

Changes:

  • Mirrors overflow, activity-bar, and policy-overlay state into CSS classes.
  • Updates affected selectors to consume those classes.
  • Adds lint detection for root-anchored :has() selectors.

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
build/stylelint.ts Adds the root-anchor lint check.
src/vs/workbench/browser/layout.ts Tracks hidden activity-bar state on the workbench root.
src/vs/workbench/browser/media/floatingPanels.css Replaces root :has() selectors.
src/vs/workbench/browser/parts/activitybar/activitybarPart.ts Mirrors compact activity-bar state.
src/vs/workbench/contrib/chat/browser/widget/input/chatInputPart.ts Tracks working-set overflow.
src/vs/workbench/contrib/chat/browser/widget/media/chat.css Uses the overflow class for padding.
src/vs/sessions/contrib/policyBlocked/browser/sessionsPolicyBlocked.ts Tracks overlay lifetime on the root.
src/vs/sessions/contrib/policyBlocked/browser/media/sessionsPolicyBlocked.css Uses the policy-blocked root class.

Comment thread build/stylelint.ts Resolved
zmr-233 and others added 2 commits July 22, 2026 13:25
Addresses Copilot review feedback: the previous regex missed root tokens
that are not first in the compound (.style-override.monaco-workbench:has())
and matched incomplete tokens (.monaco-workbench-test, bodyguard). Extracted
into build/lib/stylelint/validateHasSelectors.ts with focused tests.

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.

Thanks for looking into this and adding the styling rules! There is just one NIT

Review feedback: inject IWorkbenchLayoutService instead of walking the DOM
with container.closest(). The caller already passes mainContainer as the
overlay container. Stub the layout service in the component fixture.
@justschen

Copy link
Copy Markdown
Collaborator

cc Benjamin Christopher Simmonds (@benibenj) runninng the workflows on this, i think would be nice to get in 🚀

@zmr-233

zmr-233 commented Jul 26, 2026

Copy link
Copy Markdown
Contributor Author

cc Benjamin Christopher Simmonds (@benibenj) runninng the workflows on this, i think would be nice to get in 🚀

btw two checks failed. Component Fixtures is just the shallow fetch — main moved on too far since my last sync so it can't find the merge-base, merged latest main and pushed, should be green on rerun. the engineering system one blocks any build/ change from people without write access, so that one stays red no matter what — can split the stylelint bit into its own PR if that's easier, your call

@zmr-233

zmr-233 commented Jul 26, 2026

Copy link
Copy Markdown
Contributor Author

Justin Chen (@justschen) workflows need another approve after the push. also fair warning, the engineering system check will fail again no matter what — that's the build/ restriction from above. still happy to split that part into a separate PR if it's blocking
image

@benibenj

Copy link
Copy Markdown
Contributor

Yeah I don't think we allow community PRs for build folder changes. If you change the PR to only include none build folder changes, I can create the PR for the linting rules

auto-merge was automatically disabled July 27, 2026 13:54

Head branch was pushed to by a user without write access

@zmr-233

zmr-233 commented Jul 27, 2026

Copy link
Copy Markdown
Contributor Author

Yeah I don't think we allow community PRs for build folder changes. If you change the PR to only include none build folder changes, I can create the PR for the linting rules

done, dropped the build/ stuff — the PR only touches src/ now

@benibenj
Benjamin Christopher Simmonds (benibenj) merged commit f57741d into microsoft:main Jul 27, 2026
29 checks passed
@vs-code-engineering vs-code-engineering Bot added this to the 1.132.0 milestone Jul 27, 2026
Benjamin Christopher Simmonds (benibenj) added a commit to benibenj/vscode that referenced this pull request Jul 27, 2026
Temporarily stack the selector replacements so the new stylelint rule can pass while microsoft#327052 is still awaiting merge. Once that PR lands, these overlapping file changes drop from this PR's diff.

Co-authored-by: Copilot <[email protected]>
Benjamin Christopher Simmonds (benibenj) added a commit that referenced this pull request Jul 27, 2026
* Avoid :has() selectors that trigger workbench-wide style invalidation

A :has() whose anchor exists in the live DOM makes Blink re-evaluate it on
DOM mutations anywhere in the document - including the inline style writes
the editor performs on every layout. With a populated chat working set,
every forced style recalc traversed the whole visible workbench (~218ms
per Recalculate Style in a large workspace; sash drag nearly frozen).
Same issue class as #324985.

Per-rule profiling showed the chat editing session scrollbar rule alone
accounted for 77% of the per-recalc cost; the root-anchored rules in
floatingPanels.css and sessionsPolicyBlocked.css are the same hazard.

- chat.css: key the scrollbar padding off an 'overflowing' class toggled
  in chatInputPart.ts, which already computes the condition. The padding
  now applies whenever the list overflows instead of only while the
  auto-hide scrollbar is faded in.
- floatingPanels.css: use 'activitybar-compact' (mirrored by
  activitybarPart.ts) and 'noactivitybar' (toggled by layout.ts like the
  other part visibility classes).
- sessionsPolicyBlocked.css: toggle a class for the overlay's lifetime.
- build/stylelint.ts: error on root-anchored :has() (body/html/:root/
  .monaco-workbench), with a has-anchor-checker disable escape.

* Match root-anchored :has() as complete tokens at any compound position

Addresses Copilot review feedback: the previous regex missed root tokens
that are not first in the compound (.style-override.monaco-workbench:has())
and matched incomplete tokens (.monaco-workbench-test, bodyguard). Extracted
into build/lib/stylelint/validateHasSelectors.ts with focused tests.

* Use layoutService.mainContainer for the policy-blocked root class

Review feedback: inject IWorkbenchLayoutService instead of walking the DOM
with container.closest(). The caller already passes mainContainer as the
overlay container. Stub the layout service in the component fixture.

* small tweaks

* Drop build/ changes; lint rule moves to a separate PR

* build: prevent root-anchored :has() selectors

Add a stylelint check for root-level :has() selectors that cause workbench-wide style invalidation, together with focused parser tests. This is split from #327052 and should merge after it.

Co-authored-by: Copilot <[email protected]>

---------

Co-authored-by: zmr233 <[email protected]>
Co-authored-by: zmr-233 <[email protected]>
Co-authored-by: Copilot <[email protected]>
@vs-code-engineering vs-code-engineering Bot locked and limited conversation to collaborators Sep 10, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

:has() selectors cause workbench-wide style recalc on every inline style write (sash dragging almost freezes)

7 participants