fix: pass vscode.Uri instead of string to getWorkspaceFolder in SHOW_GRAPHICAL_VIEW - #1555
fix: pass vscode.Uri instead of string to getWorkspaceFolder in SHOW_GRAPHICAL_VIEW#1555totolook wants to merge 1 commit into
Conversation
…GRAPHICAL_VIEW getWorkspaceFolder() expects a vscode.Uri, but when 'file' arrives as a Uri it was being converted to a string (file.fsPath) and then cast with 'as any' before being passed in. This causes an internal comparator (used for binary search over workspace folders) to throw when accessing Uri-only properties like scheme/authority/path on a string, resulting in an uncaught error when invoking 'Open Graphical View' on an XML resource.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
📝 WalkthroughSummary
WalkthroughThe graphical view command handler now normalizes its file input into a Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 ESLint
ESLint install failed: dependency version conflict. Check your lock file or package.json. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Purpose
When the
SHOW_GRAPHICAL_VIEWcommand receives avscode.Uriargument, the handlerconverts it to a
stringviafile.fsPathand then passes that string tovscode.workspace.getWorkspaceFolder(), forcing the type withas any. Internally,getWorkspaceFolder()performs a binary search over workspace folders using acomparator that expects
Uri-only properties (scheme,authority,path). Sincethese are
undefinedon a plain string, the comparator throws, and the command failssilently with an uncaught error in the extension host — so "Open Graphical View" never
opens.
Goals
Ensure
getWorkspaceFolder()always receives a propervscode.Uri, regardless ofwhether the command was invoked with a
Urior astringpath, so the graphical viewopens reliably for XML resources.
Approach
Instead of converting
fileto a string before callinggetWorkspaceFolder(), we nowkeep a
vscode.Uri(fileUri) around specifically for that call, and only convert toa string (
fsPath) for thedocumentUrifield passed tonavigate().context.subscriptions.push( commands.registerCommand(COMMANDS.SHOW_GRAPHICAL_VIEW, async (file: vscode.Uri | string) => { - let projectUri; - if (typeof file !== 'string') { - file = file.fsPath; - projectUri = vscode.workspace.getWorkspaceFolder(file as any)?.uri.fsPath - } else { - projectUri = vscode.workspace.getWorkspaceFolder(vscode.Uri.file(file))?.uri.fsPath; - } + const fileUri = typeof file !== 'string' ? file : vscode.Uri.file(file); + if (typeof file !== 'string') { + file = file.fsPath; + } + const projectUri = vscode.workspace.getWorkspaceFolder(fileUri)?.uri.fsPath; if (!projectUri) { return; } navigate(projectUri, { location: { view: null, documentUri: file } }); }) );No UI changes; this is a logic-only fix.
User stories
As a developer, when I click "Open Graphical View" on an XML resource file, the
graphical editor opens instead of throwing an uncaught error.
Release note
Fixed an issue where clicking "Open Graphical View" on an XML resource could throw an
uncaught error due to an incorrect type being passed to
vscode.workspace.getWorkspaceFolder().Documentation
N/A — internal bug fix, no user-facing behavior or documented API changes.
Training
N/A
Certification
N/A — bug fix with no impact on certification exam content.
Marketing
N/A
Automation tests
Security checks
Samples
N/A
Related PRs
N/A
Migrations (if applicable)
N/A
Test environment
Learning
Root-caused via the extension host error log, which showed the exception originating
from VS Code's internal
getWorkspaceFolderbinary-search comparator(
extensionHostProcess.js). Reproduced the bug locally by launching the extension indebug mode (Extension Development Host, F5) and setting a breakpoint on the
getWorkspaceFoldercall, confirmingfilewas astringrather than aUriat thatpoint despite being cast with
as any.