Skip to content

Commit 62c84b9

Browse files
committed
fix: support multi-folder workspace for codebase indexing
- Update CodeIndexManager.getInstance() to use active workspace folder instead of always using first folder - Add handleWorkspaceFolderChange() method to ClineProvider to re-initialize code index when workspace changes - Integrate workspace change detection in WorkspaceTracker to trigger code index updates - This ensures codebase index corresponds to the currently active workspace folder Fixes #6197
1 parent 02118c5 commit 62c84b9

File tree

4 files changed

+70
-13
lines changed

4 files changed

+70
-13
lines changed

src/core/webview/ClineProvider.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1825,6 +1825,58 @@ export class ClineProvider
18251825
return this.mcpHub
18261826
}
18271827

1828+
/**
1829+
* Handle workspace folder change by re-initializing the CodeIndexManager
1830+
* This ensures the code index corresponds to the currently active workspace folder
1831+
*/
1832+
public async handleWorkspaceFolderChange() {
1833+
if (!this.codeIndexManager) {
1834+
return
1835+
}
1836+
1837+
try {
1838+
// Dispose of the old code index status subscription
1839+
if (this.codeIndexStatusSubscription) {
1840+
this.codeIndexStatusSubscription.dispose()
1841+
this.codeIndexStatusSubscription = undefined
1842+
}
1843+
1844+
// Get a new instance of CodeIndexManager for the current workspace
1845+
const newManager = CodeIndexManager.getInstance(this.context)
1846+
if (!newManager) {
1847+
this.log("No workspace folder available for code index")
1848+
return
1849+
}
1850+
1851+
// Re-initialize the manager
1852+
await newManager.initialize(this.contextProxy)
1853+
1854+
// Re-subscribe to status updates
1855+
this.codeIndexStatusSubscription = newManager.onProgressUpdate((update: IndexProgressUpdate) => {
1856+
this.postMessageToWebview({
1857+
type: "indexingStatusUpdate",
1858+
values: update,
1859+
})
1860+
})
1861+
if (this.webviewDisposables && this.codeIndexStatusSubscription) {
1862+
this.webviewDisposables.push(this.codeIndexStatusSubscription)
1863+
}
1864+
1865+
// Update the reference
1866+
;(this as any).codeIndexManager = newManager
1867+
1868+
// Send the current status to the webview
1869+
this.postMessageToWebview({
1870+
type: "indexingStatusUpdate",
1871+
values: newManager.getCurrentStatus(),
1872+
})
1873+
1874+
this.log(`Code index re-initialized for workspace: ${getWorkspacePath()}`)
1875+
} catch (error) {
1876+
this.log(`Failed to re-initialize code index: ${error}`)
1877+
}
1878+
}
1879+
18281880
/**
18291881
* Check if the current state is compliant with MDM policy
18301882
* @returns true if compliant, false if blocked

src/integrations/workspace/WorkspaceTracker.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,16 @@ class WorkspaceTracker {
9797
}
9898
this.resetTimer = setTimeout(async () => {
9999
if (this.prevWorkSpacePath !== this.cwd) {
100-
await this.providerRef.deref()?.postMessageToWebview({
101-
type: "workspaceUpdated",
102-
filePaths: [],
103-
openedTabs: this.getOpenedTabsInfo(),
104-
})
100+
const provider = this.providerRef.deref()
101+
if (provider) {
102+
await provider.postMessageToWebview({
103+
type: "workspaceUpdated",
104+
filePaths: [],
105+
openedTabs: this.getOpenedTabsInfo(),
106+
})
107+
// Trigger code index re-initialization for the new workspace
108+
await provider.handleWorkspaceFolderChange()
109+
}
105110
this.filePaths.clear()
106111
this.prevWorkSpacePath = this.cwd
107112
this.initializeFilePaths()

src/services/code-index/__tests__/manager.spec.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ vi.mock("vscode", () => ({
1313
},
1414
],
1515
},
16+
window: {
17+
activeTextEditor: undefined,
18+
},
1619
}))
1720

1821
// Mock only the essential dependencies

src/services/code-index/manager.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,14 @@ export class CodeIndexManager {
2929
private _cacheManager: CacheManager | undefined
3030

3131
public static getInstance(context: vscode.ExtensionContext): CodeIndexManager | undefined {
32-
// Use first workspace folder consistently
33-
const workspaceFolders = vscode.workspace.workspaceFolders
34-
if (!workspaceFolders || workspaceFolders.length === 0) {
32+
// Get the workspace path based on the active editor or current context
33+
const workspacePath = getWorkspacePath()
34+
if (!workspacePath) {
3535
return undefined
3636
}
3737

38-
// Always use the first workspace folder for consistency across all indexing operations.
39-
// This ensures that the same workspace context is used throughout the indexing pipeline,
40-
// preventing path resolution errors in multi-workspace scenarios.
41-
const workspacePath = workspaceFolders[0].uri.fsPath
42-
38+
// Use the workspace folder of the active editor to support multi-folder workspaces.
39+
// This ensures that the codebase index corresponds to the folder the user is currently working in.
4340
if (!CodeIndexManager.instances.has(workspacePath)) {
4441
CodeIndexManager.instances.set(workspacePath, new CodeIndexManager(workspacePath, context))
4542
}

0 commit comments

Comments
 (0)