From db095b7febfc591d3d086b46aec7f419452bade0 Mon Sep 17 00:00:00 2001 From: Roo Code Date: Fri, 5 Sep 2025 07:06:25 +0000 Subject: [PATCH 1/2] fix: handle empty workspaceFolders array to prevent path type error - Fix getWorkspacePath() in src/utils/path.ts to check array length before accessing - Fix getCwd() in FileContextTracker to check array length before accessing - Fix cwd assignment in claude-code/run.ts to check array length before accessing This prevents 'path argument must be of type string. Received an instance of Array' error when workspaceFolders is an empty array. Fixes #7695 --- src/core/context-tracking/FileContextTracker.ts | 4 +++- src/integrations/claude-code/run.ts | 4 +++- src/utils/path.ts | 4 +++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/core/context-tracking/FileContextTracker.ts b/src/core/context-tracking/FileContextTracker.ts index 5741b62cfc..dd4c0317da 100644 --- a/src/core/context-tracking/FileContextTracker.ts +++ b/src/core/context-tracking/FileContextTracker.ts @@ -37,7 +37,9 @@ export class FileContextTracker { // Gets the current working directory or returns undefined if it cannot be determined private getCwd(): string | undefined { - const cwd = vscode.workspace.workspaceFolders?.map((folder) => folder.uri.fsPath).at(0) + // Ensure we handle empty arrays properly - check length before accessing + const workspaceFolders = vscode.workspace.workspaceFolders + const cwd = workspaceFolders && workspaceFolders.length > 0 ? workspaceFolders[0].uri.fsPath : undefined if (!cwd) { console.info("No workspace folder available - cannot determine current working directory") } diff --git a/src/integrations/claude-code/run.ts b/src/integrations/claude-code/run.ts index ac2e856f7a..e6f42331bc 100644 --- a/src/integrations/claude-code/run.ts +++ b/src/integrations/claude-code/run.ts @@ -7,7 +7,9 @@ import { CLAUDE_CODE_DEFAULT_MAX_OUTPUT_TOKENS } from "@roo-code/types" import * as os from "os" import { t } from "../../i18n" -const cwd = vscode.workspace.workspaceFolders?.map((folder) => folder.uri.fsPath).at(0) +// Ensure we handle empty arrays properly - check length before accessing +const workspaceFolders = vscode.workspace.workspaceFolders +const cwd = workspaceFolders && workspaceFolders.length > 0 ? workspaceFolders[0].uri.fsPath : undefined // Claude Code installation URL - can be easily updated if needed const CLAUDE_CODE_INSTALLATION_URL = "https://docs.anthropic.com/en/docs/claude-code/setup" diff --git a/src/utils/path.ts b/src/utils/path.ts index 48e2ce6673..ccd0826212 100644 --- a/src/utils/path.ts +++ b/src/utils/path.ts @@ -107,7 +107,9 @@ export const toRelativePath = (filePath: string, cwd: string) => { } export const getWorkspacePath = (defaultCwdPath = "") => { - const cwdPath = vscode.workspace.workspaceFolders?.map((folder) => folder.uri.fsPath).at(0) || defaultCwdPath + // Ensure we handle empty arrays properly - check length before mapping + const workspaceFolders = vscode.workspace.workspaceFolders + const cwdPath = workspaceFolders && workspaceFolders.length > 0 ? workspaceFolders[0].uri.fsPath : defaultCwdPath const currentFileUri = vscode.window.activeTextEditor?.document.uri if (currentFileUri) { const workspaceFolder = vscode.workspace.getWorkspaceFolder(currentFileUri) From f276e681335dc6eb94d3a41eecfddd850d9a0cae Mon Sep 17 00:00:00 2001 From: Roo Code Date: Fri, 5 Sep 2025 07:13:10 +0000 Subject: [PATCH 2/2] fix: handle empty workspace folders array to prevent path type errors - Replace .at(0) with safe array access using length checks - Fixes "path argument must be of type string" error when workspace is empty - Addresses regression introduced between v3.25.17 and v3.26.7 Fixes #7695 --- src/services/mcp/McpHub.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/services/mcp/McpHub.ts b/src/services/mcp/McpHub.ts index 6ec5b839e8..07264637b2 100644 --- a/src/services/mcp/McpHub.ts +++ b/src/services/mcp/McpHub.ts @@ -86,7 +86,10 @@ const createServerTypeSchema = () => { type: z.enum(["stdio"]).optional(), command: z.string().min(1, "Command cannot be empty"), args: z.array(z.string()).optional(), - cwd: z.string().default(() => vscode.workspace.workspaceFolders?.at(0)?.uri.fsPath ?? process.cwd()), + cwd: z.string().default(() => { + const workspaceFolders = vscode.workspace.workspaceFolders + return workspaceFolders && workspaceFolders.length > 0 ? workspaceFolders[0].uri.fsPath : process.cwd() + }), env: z.record(z.string()).optional(), // Ensure no SSE fields are present url: z.undefined().optional(),