Skip to content

Commit eb36d95

Browse files
committed
fix: detect newly added folders in workspace dynamically
- Added workspace folder change listener in extension.ts - Convert codeIndexManagers from array to Map for better tracking - Implement logic to create CodeIndexManager for added folders - Implement logic to dispose CodeIndexManager for removed folders - Add handleWorkspaceFoldersChanged method to ClineProvider - Fixes #8886
1 parent a3101aa commit eb36d95

File tree

2 files changed

+62
-12
lines changed

2 files changed

+62
-12
lines changed

src/core/webview/ClineProvider.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1614,6 +1614,15 @@ export class ClineProvider
16141614
await this.postStateToWebview()
16151615
}
16161616

1617+
async handleWorkspaceFoldersChanged() {
1618+
// Update the current workspace path when folders change
1619+
this.currentWorkspacePath = getWorkspacePath()
1620+
// Notify the webview about the state change
1621+
await this.postStateToWebview()
1622+
// Log the workspace change for debugging
1623+
this.log("Workspace folders changed - updated workspace path and notified webview")
1624+
}
1625+
16171626
async postStateToWebview() {
16181627
const state = await this.getStateToPostToWebview()
16191628
this.postMessageToWebview({ type: "state", state })

src/extension.ts

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -101,27 +101,68 @@ export async function activate(context: vscode.ExtensionContext) {
101101
const contextProxy = await ContextProxy.getInstance(context)
102102

103103
// Initialize code index managers for all workspace folders.
104-
const codeIndexManagers: CodeIndexManager[] = []
104+
const codeIndexManagers: Map<string, CodeIndexManager> = new Map()
105105

106+
// Helper function to initialize a CodeIndexManager for a folder
107+
async function initializeCodeIndexManagerForFolder(folder: vscode.WorkspaceFolder): Promise<void> {
108+
const manager = CodeIndexManager.getInstance(context, folder.uri.fsPath)
109+
110+
if (manager) {
111+
codeIndexManagers.set(folder.uri.fsPath, manager)
112+
113+
try {
114+
await manager.initialize(contextProxy)
115+
} catch (error) {
116+
outputChannel.appendLine(
117+
`[CodeIndexManager] Error during background CodeIndexManager configuration/indexing for ${folder.uri.fsPath}: ${error.message || error}`,
118+
)
119+
}
120+
121+
context.subscriptions.push(manager)
122+
}
123+
}
124+
125+
// Initialize managers for existing workspace folders
106126
if (vscode.workspace.workspaceFolders) {
107127
for (const folder of vscode.workspace.workspaceFolders) {
108-
const manager = CodeIndexManager.getInstance(context, folder.uri.fsPath)
109-
110-
if (manager) {
111-
codeIndexManagers.push(manager)
128+
await initializeCodeIndexManagerForFolder(folder)
129+
}
130+
}
112131

113-
try {
114-
await manager.initialize(contextProxy)
115-
} catch (error) {
132+
// Listen for workspace folder changes
133+
context.subscriptions.push(
134+
vscode.workspace.onDidChangeWorkspaceFolders(async (event) => {
135+
// Handle removed folders
136+
for (const removedFolder of event.removed) {
137+
const manager = codeIndexManagers.get(removedFolder.uri.fsPath)
138+
if (manager) {
116139
outputChannel.appendLine(
117-
`[CodeIndexManager] Error during background CodeIndexManager configuration/indexing for ${folder.uri.fsPath}: ${error.message || error}`,
140+
`[CodeIndexManager] Removing CodeIndexManager for removed folder: ${removedFolder.uri.fsPath}`,
118141
)
142+
// Dispose the manager
143+
manager.dispose()
144+
// Remove from our map
145+
codeIndexManagers.delete(removedFolder.uri.fsPath)
146+
// Remove from context subscriptions
147+
const index = context.subscriptions.indexOf(manager)
148+
if (index > -1) {
149+
context.subscriptions.splice(index, 1)
150+
}
119151
}
152+
}
120153

121-
context.subscriptions.push(manager)
154+
// Handle added folders
155+
for (const addedFolder of event.added) {
156+
outputChannel.appendLine(
157+
`[CodeIndexManager] Adding CodeIndexManager for new folder: ${addedFolder.uri.fsPath}`,
158+
)
159+
await initializeCodeIndexManagerForFolder(addedFolder)
122160
}
123-
}
124-
}
161+
162+
// Notify the provider about workspace changes
163+
provider.handleWorkspaceFoldersChanged()
164+
}),
165+
)
125166

126167
// Initialize the provider *before* the Roo Code Cloud service.
127168
const provider = new ClineProvider(context, outputChannel, "sidebar", contextProxy, mdmService)

0 commit comments

Comments
 (0)