Skip to content

Commit 89f86e7

Browse files
committed
fix: restore watcher-triggered reindexing
1 parent 9525b06 commit 89f86e7

File tree

6 files changed

+116
-10
lines changed

6 files changed

+116
-10
lines changed

src/index.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { fileURLToPath } from "url";
44

55
import { parseConfig } from "./config/schema.js";
66
import { loadMergedConfig } from "./config/merger.js";
7-
import { Indexer } from "./indexer/index.js";
87
import { createWatcherWithIndexer } from "./watcher/index.js";
98
import {
109
codebase_search,
@@ -20,10 +19,19 @@ import {
2019
add_knowledge_base,
2120
list_knowledge_bases,
2221
remove_knowledge_base,
22+
getSharedIndexer,
2323
initializeTools,
2424
} from "./tools/index.js";
2525
import { loadCommandsFromDirectory } from "./commands/loader.js";
2626
import { hasProjectMarker } from "./utils/files.js";
27+
import type { CombinedWatcher } from "./watcher/index.js";
28+
29+
let activeWatcher: CombinedWatcher | null = null;
30+
31+
function replaceActiveWatcher(nextWatcher: CombinedWatcher | null): void {
32+
activeWatcher?.stop();
33+
activeWatcher = nextWatcher;
34+
}
2735

2836
function getCommandsDir(): string {
2937
let currentDir = process.cwd();
@@ -43,7 +51,7 @@ const plugin: Plugin = async ({ directory }) => {
4351

4452
initializeTools(projectRoot, config);
4553

46-
const indexer = new Indexer(projectRoot, config);
54+
const indexer = getSharedIndexer();
4755

4856
const isValidProject = !config.indexing.requireProjectMarker || hasProjectMarker(projectRoot);
4957

@@ -61,7 +69,9 @@ const plugin: Plugin = async ({ directory }) => {
6169
}
6270

6371
if (config.indexing.watchFiles && isValidProject) {
64-
createWatcherWithIndexer(indexer, projectRoot, config);
72+
replaceActiveWatcher(createWatcherWithIndexer(getSharedIndexer, projectRoot, config));
73+
} else {
74+
replaceActiveWatcher(null);
6575
}
6676

6777
return {

src/tools/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ export function initializeTools(projectRoot: string, config: ParsedCodebaseIndex
3030
sharedIndexer = new Indexer(projectRoot, config);
3131
}
3232

33+
export function getSharedIndexer(): Indexer {
34+
return getIndexer();
35+
}
36+
3337
function refreshIndexerFromConfig(): void {
3438
if (!sharedProjectRoot) {
3539
throw new Error("Codebase index tools not initialized. Plugin may not be loaded correctly.");

src/utils/files.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,16 @@ export function shouldIncludeFile(
111111
}
112112

113113
function matchGlob(filePath: string, pattern: string): boolean {
114-
let regexPattern = pattern
114+
if (pattern.startsWith("**/")) {
115+
const withoutPrefix = pattern.slice(3);
116+
if (withoutPrefix && matchGlob(filePath, withoutPrefix)) {
117+
return true;
118+
}
119+
}
120+
121+
const escapedPattern = pattern.replace(/[.+^$()|[\]\\]/g, "\\$&");
122+
123+
let regexPattern = escapedPattern
115124
.replace(/\*\*/g, "<<<DOUBLESTAR>>>")
116125
.replace(/\*/g, "[^/]*")
117126
.replace(/<<<DOUBLESTAR>>>/g, ".*")

src/watcher/index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import chokidar, { FSWatcher } from "chokidar";
22
import * as path from "path";
33

4-
import { CodebaseIndexConfig } from "../config/schema.js";
4+
import type { CodebaseIndexConfig } from "../config/schema.js";
55
import { createIgnoreFilter, shouldIncludeFile } from "../utils/files.js";
6-
import { Indexer } from "../indexer/index.js";
6+
import type { Indexer } from "../indexer/index.js";
77
import { isGitRepo, getHeadPath, getCurrentBranch } from "../git/index.js";
88

99
export type FileChangeType = "add" | "change" | "unlink";
@@ -243,7 +243,7 @@ export interface CombinedWatcher {
243243
}
244244

245245
export function createWatcherWithIndexer(
246-
indexer: Indexer,
246+
getIndexer: () => Indexer,
247247
projectRoot: string,
248248
config: CodebaseIndexConfig
249249
): CombinedWatcher {
@@ -256,7 +256,7 @@ export function createWatcherWithIndexer(
256256
const hasDelete = changes.some((c) => c.type === "unlink");
257257

258258
if (hasAddOrChange || hasDelete) {
259-
await indexer.index();
259+
await getIndexer().index();
260260
}
261261
});
262262

@@ -266,7 +266,7 @@ export function createWatcherWithIndexer(
266266
gitWatcher = new GitHeadWatcher(projectRoot);
267267
gitWatcher.start(async (oldBranch, newBranch) => {
268268
console.log(`Branch changed: ${oldBranch ?? "(none)"} -> ${newBranch}`);
269-
await indexer.index();
269+
await getIndexer().index();
270270
});
271271
}
272272

tests/files.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,22 @@ describe("files utilities", () => {
8282
)
8383
).toBe(false);
8484
});
85+
86+
it("should include root-level files with dots in their names", () => {
87+
const filter = createIgnoreFilter(tempDir);
88+
const includePatterns = ["**/*.{ts,tsx,js,jsx,mjs,cjs}"];
89+
const excludePatterns = ["**/.*"];
90+
91+
expect(
92+
shouldIncludeFile(
93+
path.join(tempDir, "watcher.probe.ts"),
94+
tempDir,
95+
includePatterns,
96+
excludePatterns,
97+
filter
98+
)
99+
).toBe(true);
100+
});
85101
});
86102

87103
describe("collectFiles", () => {

tests/watcher.test.ts

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
22
import * as fs from "fs";
33
import * as path from "path";
44
import * as os from "os";
5-
import { FileWatcher, GitHeadWatcher, FileChange } from "../src/watcher/index.js";
5+
import { FileWatcher, GitHeadWatcher, FileChange, createWatcherWithIndexer } from "../src/watcher/index.js";
66
import { ParsedCodebaseIndexConfig } from "../src/config/schema.js";
77

88
const createTestConfig = (overrides: Partial<ParsedCodebaseIndexConfig> = {}): ParsedCodebaseIndexConfig => ({
@@ -119,6 +119,73 @@ describe("FileWatcher", () => {
119119
expect(tsChanges.length).toBeGreaterThanOrEqual(0);
120120
expect(mdChanges.length).toBe(0);
121121
});
122+
123+
it("should include matching root-level files", async () => {
124+
const changes: FileChange[] = [];
125+
watcher = new FileWatcher(tempDir, createTestConfig({ include: ["**/*.ts"] }));
126+
127+
watcher.start(async (c) => {
128+
changes.push(...c);
129+
});
130+
131+
await new Promise((r) => setTimeout(r, 100));
132+
133+
fs.writeFileSync(path.join(tempDir, "root.ts"), "export const root = 1;");
134+
135+
await new Promise((r) => setTimeout(r, 1500));
136+
137+
expect(changes.some((c) => c.path.endsWith("root.ts"))).toBe(true);
138+
});
139+
});
140+
141+
describe("createWatcherWithIndexer", () => {
142+
it("uses the latest indexer instance for file-triggered reindexing", async () => {
143+
const staleIndexer = {
144+
index: vi.fn().mockResolvedValue(undefined),
145+
};
146+
const refreshedIndexer = {
147+
index: vi.fn().mockResolvedValue(undefined),
148+
};
149+
150+
let currentIndexer = staleIndexer;
151+
const combinedWatcher = createWatcherWithIndexer(
152+
() => currentIndexer,
153+
tempDir,
154+
createTestConfig()
155+
);
156+
157+
await new Promise((r) => setTimeout(r, 100));
158+
currentIndexer = refreshedIndexer;
159+
160+
fs.writeFileSync(path.join(tempDir, "src", "reindex-me.ts"), "export const value = 1;");
161+
162+
await new Promise((r) => setTimeout(r, 1500));
163+
164+
expect(refreshedIndexer.index).toHaveBeenCalledTimes(1);
165+
expect(staleIndexer.index).not.toHaveBeenCalled();
166+
167+
combinedWatcher.stop();
168+
});
169+
170+
it("stops the watcher cleanly after start", () => {
171+
const indexer = {
172+
index: vi.fn().mockResolvedValue(undefined),
173+
};
174+
175+
const combinedWatcher = createWatcherWithIndexer(
176+
() => indexer,
177+
tempDir,
178+
createTestConfig()
179+
);
180+
181+
expect(combinedWatcher.fileWatcher.isRunning()).toBe(true);
182+
expect(combinedWatcher.gitWatcher?.isRunning() ?? false).toBe(false);
183+
184+
combinedWatcher.stop();
185+
186+
expect(combinedWatcher.fileWatcher.isRunning()).toBe(false);
187+
expect(combinedWatcher.gitWatcher?.isRunning() ?? false).toBe(false);
188+
});
122189
});
123190
});
124191

0 commit comments

Comments
 (0)