Skip to content

Commit 8b53cb8

Browse files
committed
fix: discover missed watch directories with snapshots
1 parent 781467d commit 8b53cb8

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

packages/language-server/src/plugins/typescript/LSAndTSDocResolver.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ interface LSAndTSDocResolverOptions {
4646
tsSystem?: ts.System;
4747
watchDirectory?: (patterns: RelativePattern[]) => void;
4848
nonRecursiveWatchPattern?: string;
49+
/**
50+
* Optional callback invoked when a new snapshot is created.
51+
* Used by svelte-check to dynamically add parent directories to file watchers.
52+
*/
53+
onSnapshotCreated?: (dirPath: string) => void;
4954
}
5055

5156
export class LSAndTSDocResolver {
@@ -83,6 +88,20 @@ export class LSAndTSDocResolver {
8388

8489
this.tsSystem = this.wrapWithPackageJsonMonitoring(this.options?.tsSystem ?? ts.sys);
8590
this.globalSnapshotsManager = new GlobalSnapshotsManager(this.tsSystem);
91+
// Notify when new snapshots are created so external watchers (svelte-check)
92+
// can add their parent directories dynamically.
93+
if (this.options?.onSnapshotCreated) {
94+
this.globalSnapshotsManager.onChange((fileName, newDocument) => {
95+
if (newDocument) {
96+
try {
97+
const dir = dirname(fileName);
98+
this.options?.onSnapshotCreated?.(dir);
99+
} catch {
100+
// best-effort; ignore errors in callback
101+
}
102+
}
103+
});
104+
}
86105
this.userPreferencesAccessor = { preferences: this.getTsUserPreferences() };
87106
const projectService = createProjectService(this.tsSystem, this.userPreferencesAccessor);
88107

packages/language-server/src/svelte-check.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ export interface SvelteCheckOptions {
3131
tsconfig?: string;
3232
onProjectReload?: () => void;
3333
watch?: boolean;
34+
/**
35+
* Optional callback invoked when a new snapshot is created.
36+
* Used by svelte-check to dynamically add watch directories.
37+
*/
38+
onSnapshotCreated?: (dirPath: string) => void;
3439
}
3540

3641
/**
@@ -91,7 +96,8 @@ export class SvelteCheck {
9196
tsconfigPath: options.tsconfig,
9297
isSvelteCheck: true,
9398
onProjectReloaded: options.onProjectReload,
94-
watch: options.watch
99+
watch: options.watch,
100+
onSnapshotCreated: options.onSnapshotCreated
95101
}
96102
);
97103
this.pluginHost.register(

packages/svelte-check/src/index.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,16 @@ class DiagnosticsWatcher {
197197
this.updateWatchedDirectories();
198198
}
199199

200+
addWatchDirectory(dir: string) {
201+
if (!dir || this.currentWatchedDirs.has(dir)) {
202+
return;
203+
}
204+
this.watcher.add(dir);
205+
this.currentWatchedDirs.add(dir);
206+
// New files might now be visible; schedule a run
207+
this.scheduleDiagnostics();
208+
}
209+
200210
private async updateWatchedDirectories() {
201211
const watchDirs = await this.svelteCheck.getWatchDirectories();
202212
const dirsToWatch = watchDirs || [{ path: this.workspaceUri.fsPath, recursive: true }];
@@ -303,11 +313,16 @@ parseOptions(async (opts) => {
303313
};
304314

305315
if (opts.watch) {
316+
// Wire callbacks that can reference the watcher instance created below
317+
let watcher: DiagnosticsWatcher;
306318
svelteCheckOptions.onProjectReload = () => {
307319
watcher.updateWatchers();
308320
watcher.scheduleDiagnostics();
309321
};
310-
const watcher = new DiagnosticsWatcher(
322+
svelteCheckOptions.onSnapshotCreated = (dirPath: string) => {
323+
watcher.addWatchDirectory(dirPath);
324+
};
325+
watcher = new DiagnosticsWatcher(
311326
opts.workspaceUri,
312327
new SvelteCheck(opts.workspaceUri.fsPath, svelteCheckOptions),
313328
writer,

0 commit comments

Comments
 (0)