Skip to content

fix: guard setFocus in CommentsView against filtered elements (fixes #317795)#317801

Open
vs-code-engineering[bot] wants to merge 1 commit into
mainfrom
fix/comments-tree-element-not-found-317795-145c1b9b6bf449ba
Open

fix: guard setFocus in CommentsView against filtered elements (fixes #317795)#317801
vs-code-engineering[bot] wants to merge 1 commit into
mainfrom
fix/comments-tree-element-not-found-317795-145c1b9b6bf449ba

Conversation

@vs-code-engineering
Copy link
Copy Markdown
Contributor

🔧 Error Fix

Summary

The CommentsView refresh() method retrieves the first comment thread from the model and calls tree.setFocus([firstComment]). However, renderComments() filters elements via threadHasMeaningfulComments before passing them to the tree. When the first comment in the model doesn't pass this filter, it's absent from the tree, causing ObjectTreeModel.getNode() to throw "TreeError [CommentsTree] Tree element not found: [object Object]".

This error occurs when the Comments panel becomes visible (e.g., opening the sidebar) and the first comment thread in the model has no meaningful comments (empty body or deleted).

Fixes #317795
Recommended reviewer: @alexr00

Culprit Commit

This is a latent bug — the refresh() method has had this pattern since the focus/selection logic was introduced. No single recent commit caused the regression; the error surfaces when comment data from extensions includes threads that fail the threadHasMeaningfulComments filter.

Code Flow

sequenceDiagram
    participant PaneComp as PaneCompositePart
    participant CommView as CommentsPanel
    participant TreeComp as CommentsList
    participant TreeModel as ObjectTreeModel

    PaneComp->>CommView: setVisible(true)
    CommView->>CommView: refresh()
    CommView->>CommView: renderComments()
    CommView->>TreeComp: setChildren(null, filtered elements)
    TreeComp->>TreeModel: update nodes (filtered)
    CommView->>CommView: get firstComment from model (unfiltered)
    CommView->>TreeComp: setFocus([firstComment])
    TreeComp->>TreeModel: getNode(firstComment)
    TreeModel-->>TreeComp: TreeError - element not found
Loading

Affected Files

File Role
src/vs/workbench/contrib/comments/browser/commentsView.ts Producer of invalid focus target — passes unfiltered model element to filtered tree
src/vs/base/browser/ui/tree/objectTreeModel.ts Crash site — throws when element not in node map

Repro Steps

  1. Install an extension that provides comment threads (e.g., GitHub PR extension)
  2. Have a comment thread where threadHasMeaningfulComments returns false (empty/deleted thread)
  3. Ensure that thread is the first in the model's resourceCommentThreads[0].commentThreads[0]
  4. Open the Comments panel (or switch to the sidebar containing it)
  5. Error is thrown during refresh()setFocus()

How the Fix Works

Chosen approach (src/vs/workbench/contrib/comments/browser/commentsView.ts): Added this.tree.hasElement(firstComment) guard before calling setFocus/setSelection. This fixes at the data producer site — the code that selects which element to focus — rather than at the crash site (the tree's getNode method). The guard ensures only elements that survived the threadHasMeaningfulComments filter (and are actually present in the tree) are passed to focus/selection APIs.

Alternatives considered: Using tree.getFirstElement() from the tree itself — while correct, this would change the semantics of which element gets focused (tree's first visible vs model's first thread). The hasElement guard preserves the original intent while preventing the error.

Recommended Owner

@alexr00 — primary maintainer of the Comments view (src/vs/workbench/contrib/comments/).

Generated by errors-fix · ● 35.2M ·

…317795)

The refresh() method gets the first comment from the model and calls
tree.setFocus(), but the tree may not contain that element if it was
filtered out by threadHasMeaningfulComments in renderComments(). Add a
hasElement check before setting focus/selection.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings May 21, 2026 16:33
Copy link
Copy Markdown
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.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

@vs-code-engineering vs-code-engineering Bot requested review from alexr00 and Copilot May 21, 2026 16:38
Copy link
Copy Markdown
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.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

@vs-code-engineering vs-code-engineering Bot marked this pull request as ready for review May 21, 2026 16:38
@vs-code-engineering vs-code-engineering Bot enabled auto-merge (squash) May 21, 2026 16:38
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] unhandlederror-TreeError [CommentsTree] Tree element not found: [object Object]

2 participants