Browse Source

fix: allow opening files without workspace root (#1054)

* fix: allow opening files without workspace root

The openFile function in open-file.ts was requiring a workspace root to be present,
which prevented opening global files (like MCP settings) when no workspace was open.
Modified the function to handle absolute paths without this requirement.

Previously, trying to open MCP settings in a new window without a workspace would
error with "Could not open file: No workspace root found". Now the function
properly handles both workspace-relative and absolute paths, allowing global
settings files to be accessed in any context.

Changes:
- Removed workspace root requirement in openFile
- Added fallback for relative paths when no workspace is present

* fix: update openFile function to use provided path without modification

---------

Co-authored-by: Roo Code <[email protected]>
Co-authored-by: Matt Rubens <[email protected]>
Hannes Rudolph 8 months ago
parent
commit
3d129e8a89
1 changed files with 7 additions and 5 deletions
  1. 7 5
      src/integrations/misc/open-file.ts

+ 7 - 5
src/integrations/misc/open-file.ts

@@ -29,12 +29,14 @@ export async function openFile(filePath: string, options: OpenFileOptions = {})
 	try {
 		// Get workspace root
 		const workspaceRoot = getWorkspacePath()
-		if (!workspaceRoot) {
-			throw new Error("No workspace root found")
-		}
 
-		// If path starts with ./, resolve it relative to workspace root
-		const fullPath = filePath.startsWith("./") ? path.join(workspaceRoot, filePath.slice(2)) : filePath
+		// If path starts with ./, resolve it relative to workspace root if available
+		// Otherwise, use the path as provided without modification
+		const fullPath = filePath.startsWith("./")
+			? workspaceRoot
+				? path.join(workspaceRoot, filePath.slice(2))
+				: filePath
+			: filePath
 
 		const uri = vscode.Uri.file(fullPath)