From db095b7febfc591d3d086b46aec7f419452bade0 Mon Sep 17 00:00:00 2001 From: Roo Code Date: Fri, 5 Sep 2025 07:06:25 +0000 Subject: [PATCH] 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)