Skip to content
This repository was archived by the owner on May 15, 2026. It is now read-only.

Commit 49acdc9

Browse files
committed
feat: add multi-root workspace support
- Updated getWorkspacePath to properly handle multi-root workspaces - Added getAllWorkspacePaths and getWorkspaceFolderForPath utility functions - Fixed isPathOutsideWorkspace to correctly check all workspace folders - Updated Task class to support multiple workspace roots - Modified environment details to show files from all workspace folders - Added comprehensive tests for multi-root workspace scenarios Fixes #8041
1 parent 2263d86 commit 49acdc9

6 files changed

Lines changed: 559 additions & 46 deletions

File tree

‎src/core/environment/getEnvironmentDetails.ts‎

Lines changed: 59 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -238,32 +238,69 @@ export async function getEnvironmentDetails(cline: Task, includeFileDetails: boo
238238
}
239239

240240
if (includeFileDetails) {
241-
details += `\n\n# Current Workspace Directory (${cline.cwd.toPosix()}) Files\n`
242-
const isDesktop = arePathsEqual(cline.cwd, path.join(os.homedir(), "Desktop"))
241+
// Check if we have multiple workspace folders
242+
const workspaceFolders = vscode.workspace.workspaceFolders
243+
const isMultiRoot = workspaceFolders && workspaceFolders.length > 1
244+
245+
if (isMultiRoot) {
246+
// For multi-root workspaces, show files from all workspace folders
247+
details += `\n\n# Multi-Root Workspace Files\n`
248+
const maxFilesPerFolder = Math.floor((maxWorkspaceFiles ?? 200) / workspaceFolders.length)
249+
250+
for (const folder of workspaceFolders) {
251+
const folderPath = folder.uri.fsPath
252+
const folderName = folder.name
253+
details += `\n## ${folderName} (${folderPath.toPosix()})\n`
254+
255+
const isDesktop = arePathsEqual(folderPath, path.join(os.homedir(), "Desktop"))
256+
if (isDesktop) {
257+
details += "(Desktop files not shown automatically. Use list_files to explore if needed.)\n"
258+
} else if (maxFilesPerFolder === 0) {
259+
details += "(Workspace files context disabled. Use list_files to explore if needed.)\n"
260+
} else {
261+
const [files, didHitLimit] = await listFiles(folderPath, true, maxFilesPerFolder)
262+
const { showRooIgnoredFiles = false } = state ?? {}
263+
264+
const result = formatResponse.formatFilesList(
265+
folderPath,
266+
files,
267+
didHitLimit,
268+
cline.rooIgnoreController,
269+
showRooIgnoredFiles,
270+
)
243271

244-
if (isDesktop) {
245-
// Don't want to immediately access desktop since it would show
246-
// permission popup.
247-
details += "(Desktop files not shown automatically. Use list_files to explore if needed.)"
272+
details += result + "\n"
273+
}
274+
}
248275
} else {
249-
const maxFiles = maxWorkspaceFiles ?? 200
250-
251-
// Early return for limit of 0
252-
if (maxFiles === 0) {
253-
details += "(Workspace files context disabled. Use list_files to explore if needed.)"
276+
// Single workspace folder - use existing logic
277+
details += `\n\n# Current Workspace Directory (${cline.cwd.toPosix()}) Files\n`
278+
const isDesktop = arePathsEqual(cline.cwd, path.join(os.homedir(), "Desktop"))
279+
280+
if (isDesktop) {
281+
// Don't want to immediately access desktop since it would show
282+
// permission popup.
283+
details += "(Desktop files not shown automatically. Use list_files to explore if needed.)"
254284
} else {
255-
const [files, didHitLimit] = await listFiles(cline.cwd, true, maxFiles)
256-
const { showRooIgnoredFiles = false } = state ?? {}
257-
258-
const result = formatResponse.formatFilesList(
259-
cline.cwd,
260-
files,
261-
didHitLimit,
262-
cline.rooIgnoreController,
263-
showRooIgnoredFiles,
264-
)
285+
const maxFiles = maxWorkspaceFiles ?? 200
286+
287+
// Early return for limit of 0
288+
if (maxFiles === 0) {
289+
details += "(Workspace files context disabled. Use list_files to explore if needed.)"
290+
} else {
291+
const [files, didHitLimit] = await listFiles(cline.cwd, true, maxFiles)
292+
const { showRooIgnoredFiles = false } = state ?? {}
293+
294+
const result = formatResponse.formatFilesList(
295+
cline.cwd,
296+
files,
297+
didHitLimit,
298+
cline.rooIgnoreController,
299+
showRooIgnoredFiles,
300+
)
265301

266-
details += result
302+
details += result
303+
}
267304
}
268305
}
269306
}

‎src/core/task/Task.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2909,6 +2909,33 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
29092909
return this.workspacePath
29102910
}
29112911

2912+
/**
2913+
* Get all workspace folders for multi-root workspace support
2914+
* @returns Array of all workspace folder paths
2915+
*/
2916+
public get workspaceFolders(): string[] {
2917+
const folders = vscode.workspace.workspaceFolders
2918+
if (!folders || folders.length === 0) {
2919+
return [this.workspacePath]
2920+
}
2921+
return folders.map((folder) => folder.uri.fsPath)
2922+
}
2923+
2924+
/**
2925+
* Check if a path is within any of the workspace folders
2926+
* @param filePath The file path to check
2927+
* @returns true if the path is in a workspace folder, false otherwise
2928+
*/
2929+
public isPathInWorkspace(filePath: string): boolean {
2930+
const absolutePath = path.resolve(filePath)
2931+
const normalizedPath = path.normalize(absolutePath)
2932+
2933+
return this.workspaceFolders.some((folderPath) => {
2934+
const normalizedFolderPath = path.normalize(folderPath)
2935+
return normalizedPath === normalizedFolderPath || normalizedPath.startsWith(normalizedFolderPath + path.sep)
2936+
})
2937+
}
2938+
29122939
/**
29132940
* Process any queued messages by dequeuing and submitting them.
29142941
* This ensures that queued user messages are sent when appropriate,

‎src/utils/__tests__/path.spec.ts‎

Lines changed: 158 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,61 @@
33
import os from "os"
44
import * as path from "path"
55

6-
import { arePathsEqual, getReadablePath, getWorkspacePath } from "../path"
6+
import {
7+
arePathsEqual,
8+
getReadablePath,
9+
getWorkspacePath,
10+
getAllWorkspacePaths,
11+
getWorkspaceFolderForPath,
12+
} from "../path"
713

814
// Mock modules
15+
const mockWorkspaceFolders = vi.fn()
16+
const mockGetWorkspaceFolder = vi.fn()
17+
const mockActiveTextEditor = vi.fn()
918

1019
vi.mock("vscode", () => ({
1120
window: {
12-
activeTextEditor: {
13-
document: {
14-
uri: { fsPath: "/test/workspaceFolder/file.ts" },
15-
},
21+
get activeTextEditor() {
22+
return mockActiveTextEditor()
1623
},
1724
},
1825
workspace: {
19-
workspaceFolders: [
26+
get workspaceFolders() {
27+
return mockWorkspaceFolders()
28+
},
29+
getWorkspaceFolder: mockGetWorkspaceFolder,
30+
},
31+
}))
32+
describe("Path Utilities", () => {
33+
const originalPlatform = process.platform
34+
// Helper to mock VS Code configuration
35+
36+
beforeEach(() => {
37+
// Reset mocks before each test
38+
vi.clearAllMocks()
39+
40+
// Set default mock values
41+
mockWorkspaceFolders.mockReturnValue([
2042
{
2143
uri: { fsPath: "/test/workspace" },
2244
name: "test",
2345
index: 0,
2446
},
25-
],
26-
getWorkspaceFolder: vi.fn().mockReturnValue({
47+
])
48+
49+
mockActiveTextEditor.mockReturnValue({
50+
document: {
51+
uri: { fsPath: "/test/workspaceFolder/file.ts" },
52+
},
53+
})
54+
55+
mockGetWorkspaceFolder.mockReturnValue({
2756
uri: {
2857
fsPath: "/test/workspaceFolder",
2958
},
30-
}),
31-
},
32-
}))
33-
describe("Path Utilities", () => {
34-
const originalPlatform = process.platform
35-
// Helper to mock VS Code configuration
59+
})
60+
})
3661

3762
afterEach(() => {
3863
Object.defineProperty(process, "platform", {
@@ -56,14 +81,129 @@ describe("Path Utilities", () => {
5681
expect(extendedPath.toPosix()).toBe("\\\\?\\C:\\Very\\Long\\Path")
5782
})
5883
})
84+
5985
describe("getWorkspacePath", () => {
60-
it("should return the current workspace path", () => {
61-
const workspacePath = "/Users/test/project"
62-
expect(getWorkspacePath(workspacePath)).toBe("/Users/test/project")
86+
it("should return the workspace folder of the active editor", () => {
87+
mockActiveTextEditor.mockReturnValue({
88+
document: {
89+
uri: { fsPath: "/test/workspaceFolder/file.ts" },
90+
},
91+
})
92+
mockGetWorkspaceFolder.mockReturnValue({
93+
uri: { fsPath: "/test/workspaceFolder" },
94+
})
95+
mockWorkspaceFolders.mockReturnValue([{ uri: { fsPath: "/test/workspace" }, name: "workspace", index: 0 }])
96+
97+
expect(getWorkspacePath()).toBe("/test/workspaceFolder")
98+
})
99+
100+
it("should return the first workspace folder when no active editor", () => {
101+
mockActiveTextEditor.mockReturnValue(undefined)
102+
mockGetWorkspaceFolder.mockReturnValue(undefined)
103+
mockWorkspaceFolders.mockReturnValue([
104+
{ uri: { fsPath: "/test/workspace1" }, name: "workspace1", index: 0 },
105+
{ uri: { fsPath: "/test/workspace2" }, name: "workspace2", index: 1 },
106+
])
107+
108+
expect(getWorkspacePath()).toBe("/test/workspace1")
109+
})
110+
111+
it("should return default path when no workspace folders", () => {
112+
mockActiveTextEditor.mockReturnValue(undefined)
113+
mockGetWorkspaceFolder.mockReturnValue(undefined)
114+
mockWorkspaceFolders.mockReturnValue(undefined)
115+
116+
expect(getWorkspacePath("/default/path")).toBe("/default/path")
117+
})
118+
119+
it("should handle multi-root workspaces correctly", () => {
120+
mockWorkspaceFolders.mockReturnValue([
121+
{ uri: { fsPath: "/test/frontend" }, name: "frontend", index: 0 },
122+
{ uri: { fsPath: "/test/backend" }, name: "backend", index: 1 },
123+
])
124+
125+
// When active editor is in backend folder
126+
mockActiveTextEditor.mockReturnValue({
127+
document: { uri: { fsPath: "/test/backend/src/app.ts" } },
128+
})
129+
mockGetWorkspaceFolder.mockReturnValue({
130+
uri: { fsPath: "/test/backend" },
131+
})
132+
133+
expect(getWorkspacePath()).toBe("/test/backend")
134+
})
135+
})
136+
137+
describe("getAllWorkspacePaths", () => {
138+
it("should return all workspace folder paths", () => {
139+
mockWorkspaceFolders.mockReturnValue([
140+
{ uri: { fsPath: "/test/frontend" }, name: "frontend", index: 0 },
141+
{ uri: { fsPath: "/test/backend" }, name: "backend", index: 1 },
142+
])
143+
144+
const paths = getAllWorkspacePaths()
145+
expect(paths).toEqual(["/test/frontend", "/test/backend"])
146+
})
147+
148+
it("should return empty array when no workspace folders", () => {
149+
mockWorkspaceFolders.mockReturnValue(undefined)
150+
151+
const paths = getAllWorkspacePaths()
152+
expect(paths).toEqual([])
153+
})
154+
155+
it("should handle single workspace folder", () => {
156+
mockWorkspaceFolders.mockReturnValue([{ uri: { fsPath: "/test/workspace" }, name: "workspace", index: 0 }])
157+
158+
const paths = getAllWorkspacePaths()
159+
expect(paths).toEqual(["/test/workspace"])
160+
})
161+
})
162+
163+
describe("getWorkspaceFolderForPath", () => {
164+
beforeEach(() => {
165+
mockWorkspaceFolders.mockReturnValue([
166+
{ uri: { fsPath: "/test/frontend" }, name: "frontend", index: 0 },
167+
{ uri: { fsPath: "/test/backend" }, name: "backend", index: 1 },
168+
])
169+
})
170+
171+
it("should return the workspace folder containing the path", () => {
172+
expect(getWorkspaceFolderForPath("/test/frontend/src/app.ts")).toBe("/test/frontend")
173+
expect(getWorkspaceFolderForPath("/test/backend/src/server.ts")).toBe("/test/backend")
174+
})
175+
176+
it("should return the workspace folder for exact match", () => {
177+
expect(getWorkspaceFolderForPath("/test/frontend")).toBe("/test/frontend")
178+
expect(getWorkspaceFolderForPath("/test/backend")).toBe("/test/backend")
179+
})
180+
181+
it("should return null for paths outside any workspace", () => {
182+
expect(getWorkspaceFolderForPath("/other/path/file.ts")).toBeNull()
183+
expect(getWorkspaceFolderForPath("/test/other/file.ts")).toBeNull()
184+
})
185+
186+
it("should return null when no workspace folders", () => {
187+
mockWorkspaceFolders.mockReturnValue(undefined)
188+
expect(getWorkspaceFolderForPath("/test/frontend/src/app.ts")).toBeNull()
63189
})
64190

65-
it("should return undefined when outside a workspace", () => {})
191+
it("should handle relative paths by resolving them", () => {
192+
// This depends on the current working directory, so we'll use path.resolve
193+
const resolvedPath = path.resolve("./src/app.ts")
194+
const result = getWorkspaceFolderForPath("./src/app.ts")
195+
196+
// The result should be based on the resolved path
197+
if (resolvedPath.startsWith("/test/frontend")) {
198+
expect(result).toBe("/test/frontend")
199+
} else if (resolvedPath.startsWith("/test/backend")) {
200+
expect(result).toBe("/test/backend")
201+
} else {
202+
expect(result).toBeNull()
203+
}
204+
})
66205
})
206+
67207
describe("arePathsEqual", () => {
68208
describe("on Windows", () => {
69209
beforeEach(() => {

0 commit comments

Comments
 (0)