-
Notifications
You must be signed in to change notification settings - Fork 2.5k
fix: handle empty workspace folders array to prevent path type errors #7696
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
|
||
| // Claude Code installation URL - can be easily updated if needed | ||
| const CLAUDE_CODE_INSTALLATION_URL = "https://docs.anthropic.com/en/docs/claude-code/setup" | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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() | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good defensive programming here with the fallback to |
||
| }), | ||
| env: z.record(z.string()).optional(), | ||
| // Ensure no SSE fields are present | ||
| url: z.undefined().optional(), | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| const currentFileUri = vscode.window.activeTextEditor?.document.uri | ||
| if (currentFileUri) { | ||
| const workspaceFolder = vscode.workspace.getWorkspaceFolder(currentFileUri) | ||
|
|
||
There was a problem hiding this comment.
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.