fix: guard setFocus in CommentsView against filtered elements (fixes #317795)#317801
Open
vs-code-engineering[bot] wants to merge 1 commit into
Open
fix: guard setFocus in CommentsView against filtered elements (fixes #317795)#317801vs-code-engineering[bot] wants to merge 1 commit into
vs-code-engineering[bot] wants to merge 1 commit into
Conversation
…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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🔧 Error Fix
Summary
The CommentsView
refresh()method retrieves the first comment thread from the model and callstree.setFocus([firstComment]). However,renderComments()filters elements viathreadHasMeaningfulCommentsbefore passing them to the tree. When the first comment in the model doesn't pass this filter, it's absent from the tree, causingObjectTreeModel.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:
@alexr00Culprit 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 thethreadHasMeaningfulCommentsfilter.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 foundAffected Files
src/vs/workbench/contrib/comments/browser/commentsView.tssrc/vs/base/browser/ui/tree/objectTreeModel.tsRepro Steps
threadHasMeaningfulCommentsreturns false (empty/deleted thread)resourceCommentThreads[0].commentThreads[0]refresh()→setFocus()How the Fix Works
Chosen approach (
src/vs/workbench/contrib/comments/browser/commentsView.ts): Addedthis.tree.hasElement(firstComment)guard before callingsetFocus/setSelection. This fixes at the data producer site — the code that selects which element to focus — rather than at the crash site (the tree'sgetNodemethod). The guard ensures only elements that survived thethreadHasMeaningfulCommentsfilter (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). ThehasElementguard preserves the original intent while preventing the error.Recommended Owner
@alexr00— primary maintainer of the Comments view (src/vs/workbench/contrib/comments/).