Skip to content

Commit 5493844

Browse files
authored
fix: invalidate project file cache and handle watcher race condition (#2779)
#2773 This delays the file reading in svelte-check --watch until we're sure the new file is a project file. I still added a file existence check before reading, but it might be more of a fail-safe because the chance of it being deleted at this point is much smaller. While checking this, I also found that the new file watch is broken because it now caches the tsconfig parsing result. So I changed the "read the config again" logic to the one language server used to invalidate the project files cache.
1 parent 75eb2bb commit 5493844

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -716,7 +716,14 @@ async function createLanguageService(
716716

717717
function fileBelongsToProject(filePath: string, isNew: boolean): boolean {
718718
filePath = normalizePath(filePath);
719-
return hasFile(filePath) || (isNew && getParsedConfig().fileNames.includes(filePath));
719+
if (hasFile(filePath)) {
720+
return true;
721+
}
722+
if (!isNew) {
723+
return false;
724+
}
725+
ensureProjectFileUpdates(filePath);
726+
return snapshotManager.isProjectFile(filePath);
720727
}
721728

722729
function updateTsOrJsFile(fileName: string, changes?: TextDocumentContentChangeEvent[]): void {

packages/svelte-check/src/index.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,17 @@ class DiagnosticsWatcher {
191191
}
192192

193193
private async updateDocument(path: string, isNew: boolean) {
194-
const text = fs.readFileSync(path, 'utf-8');
195-
await this.svelteCheck.upsertDocument({ text, uri: URI.file(path).toString() }, isNew);
194+
await this.svelteCheck.upsertDocument(
195+
{
196+
// delay reading until we actually need the text
197+
// prevents race conditions from crashing svelte-check when something is created and deleted immediately afterwards
198+
get text() {
199+
return fs.existsSync(path) ? fs.readFileSync(path, 'utf-8') : '';
200+
},
201+
uri: URI.file(path).toString()
202+
},
203+
isNew
204+
);
196205
this.scheduleDiagnostics();
197206
}
198207

0 commit comments

Comments
 (0)