Repository: vscode
Issue: Key binding is unable to match when clause of a TreeItem
Labels: feature-request

<!-- ⚠️⚠️ Do Not Delete This! bug_report_template ⚠️⚠️ -->
<!-- Please read our Rules of Conduct: https://opensource.microsoft.com/codeofconduct/ -->
<!-- 🕮 Read our guide about submitting issues: https://github.com/microsoft/vscode/wiki/Submitting-Bugs-and-Suggestions -->
<!-- 🔎 Search existing issues to avoid creating duplicates. -->
<!-- 🧪 Test using the latest Insiders build to see if your issue has already been fixed: https://code.visualstudio.com/insiders/ -->
<!-- 💡 Instead of creating your report here, use 'Report Issue' from the 'Help' menu in VS Code to pre-fill useful information. -->
<!-- 🔧 Launch with `code --disable-extensions` to check. -->

<!-- 🪓 If you answered No above, use 'Help: Start Extension Bisect' from Command Palette to try to identify the cause. -->
<!-- 📣 Issues caused by an extension need to be reported directly to the extension publisher. The 'Help > Report Issue' dialog can assist with this. -->
- VS Code Version: 1.96.0 
- OS Version: MacOS 14.6.1 (23G93)

Steps to Reproduce:

1. Implement a `vscode.TreeDataProvider`, where a `treeItem` has a `contextValue`. 
2. In package.json, contribute a keybinding that uses a `when` clause referencing this `contextValue`.
3. Reload VS Code with the extension active, select the tree item in the custom view, and press the assigned key.
4. Observe that the command does not run, even though the context menu command (with the same when condition) works as expected.

**Additional Context:**
I’m trying to replicate the behavior of the built-in File Explorer’s delete operation. I have a custom tree view displaying workspace files in a different structure. The right-click “Delete File” option works correctly since the viewItem contextValue matches and the context menu appears. However, the corresponding keybinding with the same when condition never triggers the command. The KeybindingService logs indicate that the when clause does not match, despite the contextValue being identical to the one used for the context menu.


Relevant package.json snippet:

```
        "contributes": {
          "keybindings": [
            {
              "key": "alt+3",
              "mac": "alt+3",
              "command": "bit.delete-file",
              "when": "viewItem == compFile"
            }
          ],
          "menus": {
            "view/item/context": [
              {
                "command": "bit.delete-file",
                "when": "viewItem == compFile",
                "group": "navigation"
              }
            ]
          }
```

Relevant TreeItem code snippet:
```
export class CompFileTreeItem extends vscode.TreeItem {
	constructor(
		public readonly label: string,
		uri: vscode.Uri
	) {
		super(label, vscode.TreeItemCollapsibleState.None);
		this.resourceUri = uri;
		this.contextValue = 'compFile';
	}
}
```

Keybinding Inspector Output:
```
2024-12-17 15:37:45.239 [info] [KeybindingService]: | Resolving alt+[Digit3]
2024-12-17 15:37:45.239 [info] [KeybindingService]: \ From 1 keybinding entries, no when clauses matched the context.
```

Question:
Why does the viewItem == compFile condition work for the context menu but fail for the keybinding, even though the item is selected and the same context value is set?