Skip to content

fix: pass vscode.Uri instead of string to getWorkspaceFolder in SHOW_GRAPHICAL_VIEW - #1555

Open
totolook wants to merge 1 commit into
wso2:mainfrom
totolook:fix/graphical-view-workspace-folder-uri
Open

fix: pass vscode.Uri instead of string to getWorkspaceFolder in SHOW_GRAPHICAL_VIEW#1555
totolook wants to merge 1 commit into
wso2:mainfrom
totolook:fix/graphical-view-workspace-folder-uri

Conversation

@totolook

Copy link
Copy Markdown

Purpose

Fixes a crash when clicking "Open Graphical View" on an XML resource file. #1554

When the SHOW_GRAPHICAL_VIEW command receives a vscode.Uri argument, the handler
converts it to a string via file.fsPath and then passes that string to
vscode.workspace.getWorkspaceFolder(), forcing the type with as any. Internally,
getWorkspaceFolder() performs a binary search over workspace folders using a
comparator that expects Uri-only properties (scheme, authority, path). Since
these are undefined on a plain string, the comparator throws, and the command fails
silently with an uncaught error in the extension host — so "Open Graphical View" never
opens.

Goals

Ensure getWorkspaceFolder() always receives a proper vscode.Uri, regardless of
whether the command was invoked with a Uri or a string path, so the graphical view
opens reliably for XML resources.

Approach

Instead of converting file to a string before calling getWorkspaceFolder(), we now
keep a vscode.Uri (fileUri) around specifically for that call, and only convert to
a string (fsPath) for the documentUri field passed to navigate().

     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

  • Unit tests

    Not added yet — happy to add a unit test asserting getWorkspaceFolder is called
    with a vscode.Uri instance for both Uri and string inputs, if maintainers
    want it as part of this PR.

  • Integration tests

    Manually verified: reproduced the original crash by invoking "Open Graphical View"
    on an XML resource (see linked issue/stack trace), confirmed the fix resolves it.

Security checks

Samples

N/A

Related PRs

N/A

Migrations (if applicable)

N/A

Test environment

[inserisci qui la tua versione di VS Code, OS (es. Linux/Debian), Node.js version]

Learning

Root-caused via the extension host error log, which showed the exception originating
from VS Code's internal getWorkspaceFolder binary-search comparator
(extensionHostProcess.js). Reproduced the bug locally by launching the extension in
debug mode (Extension Development Host, F5) and setting a breakpoint on the
getWorkspaceFolder call, confirming file was a string rather than a Uri at that
point despite being cast with as any.

…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.
@totolook
totolook requested a review from rosensilva as a code owner July 25, 2026 15:12
@CLAassistant

CLAassistant commented Jul 25, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@coderabbitai

coderabbitai Bot commented Jul 25, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: ce56c4a8-0ef1-4c77-a44c-a23ca3834c18

📥 Commits

Reviewing files that changed from the base of the PR and between 41334d5 and eae9dca.

📒 Files selected for processing (1)
  • packages/mi-extension/src/visualizer/activate.ts

📝 Walkthrough

Summary

  • Fixed a crash when opening XML resources through SHOW_GRAPHICAL_VIEW.
  • Normalized resource paths to vscode.Uri before resolving the workspace folder.
  • Preserved filesystem path handling for navigation without changing the UI.

Walkthrough

The graphical view command handler now normalizes its file input into a vscode.Uri, derives the workspace project URI through a single workspace-folder lookup, and returns early when no project URI is available. Navigation continues with the existing location structure and uses the normalized document path.

Suggested reviewers: rosensilva

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed Clear, concise, and accurately describes the main fix in SHOW_GRAPHICAL_VIEW.
Description check ✅ Passed Mostly complete and follows the template; only the test environment section still contains placeholder text.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants