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
This commit is contained in:
Roo Code 2025-09-05 07:06:25 +00:00
parent 90e7d09596
commit db095b7feb
3 changed files with 9 additions and 3 deletions

View file

@ -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")
}

View file

@ -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"

View file

@ -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)