Skip to content

Conversation

@roomote
Copy link
Contributor

@roomote roomote bot commented Sep 5, 2025

Summary

This PR fixes the "path argument must be of type string" error that occurs when VSCode workspace folders array is empty or undefined. This regression was introduced between v3.25.17 and v3.26.7.

Problem

The .at(0) method was being used to access the first element of the workspace folders array. When the array is empty, this returns undefined, which then gets passed to path functions expecting a string, causing the error.

Solution

Replaced all .at(0) usages with safe array access patterns that check the array length before accessing elements:

  • In src/utils/path.ts: Modified getWorkspacePath()
  • In src/core/context-tracking/FileContextTracker.ts: Modified getCwd()
  • In src/integrations/claude-code/run.ts: Modified cwd constant initialization
  • In src/services/mcp/McpHub.ts: Modified default cwd in Zod schema

Testing

  • All existing tests pass ✅
  • Linting passes ✅
  • Type checking passes ✅

Related Issues

Fixes #7695


Important

Fixes path type error by checking array length before accessing elements in workspace folders.

  • Behavior:
    • Fixes "path argument must be of type string" error by checking array length before accessing elements.
    • Affects getWorkspacePath() in path.ts, getCwd() in FileContextTracker.ts, cwd initialization in run.ts, and default cwd in McpHub.ts.
  • Testing:
    • All existing tests pass.
    • Linting and type checking pass.

This description was created by Ellipsis for f276e68. You can customize this summary. It will automatically update as commits are pushed.

- 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
- 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
@roomote roomote bot requested review from cte, jr and mrubens as code owners September 5, 2025 07:14
@dosubot dosubot bot added size:S This PR changes 10-29 lines, ignoring generated files. bug Something isn't working labels Sep 5, 2025
@hannesrudolph hannesrudolph added the Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. label Sep 5, 2025
Copy link
Contributor Author

@roomote roomote bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewing my own code is like proofreading my own typos - I'll find them all after it's merged.

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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I notice this implementation differs from the other three files - here I removed the .map() call entirely, while in the others I store workspaceFolders in a variable first. Would it be better to use a consistent pattern across all four files for maintainability?

// 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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment "Ensure we handle empty arrays properly" could be more specific. Consider: "Check array length to avoid undefined when accessing first element" to make the intent clearer.

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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this same pattern appears in 4 different files, would it make sense to create a shared utility function like getFirstWorkspaceFolder() to centralize this logic? This could help prevent similar regressions in the future.

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()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good defensive programming here with the fallback to process.cwd(). Though I wonder if we should add a test case that specifically validates the behavior when vscode.workspace.workspaceFolders is empty or undefined? The PR mentions tests pass but doesn't add new ones for this edge case.

@daniel-lxs
Copy link
Member

Fixed by #7697

@daniel-lxs daniel-lxs closed this Sep 6, 2025
@github-project-automation github-project-automation bot moved this from Triage to Done in Roo Code Roadmap Sep 6, 2025
@github-project-automation github-project-automation bot moved this from New to Done in Roo Code Roadmap Sep 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. size:S This PR changes 10-29 lines, ignoring generated files.

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

The "path" argument must be of type string. Received an instance of Array

4 participants