From 85e8492fb3274db76e95452b4812d2548e41595e Mon Sep 17 00:00:00 2001 From: Nate Date: Fri, 6 Dec 2024 15:39:42 -0800 Subject: [PATCH] fix ImportDefinitionsService bug --- .../context/ImportDefinitionsService.ts | 13 +++++++++++-- core/util/LruCache.ts | 8 ++++++-- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/core/autocomplete/context/ImportDefinitionsService.ts b/core/autocomplete/context/ImportDefinitionsService.ts index 128dbf78e..309310738 100644 --- a/core/autocomplete/context/ImportDefinitionsService.ts +++ b/core/autocomplete/context/ImportDefinitionsService.ts @@ -29,14 +29,23 @@ export class ImportDefinitionsService { return this.cache.get(filepath); } - private async _getFileInfo(filepath: string): Promise { + private async _getFileInfo(filepath: string): Promise { const parser = await getParserForFile(filepath); if (!parser) { return { imports: {}, }; } - const ast = parser.parse(await this.ide.readFile(filepath), undefined, { + + let fileContents: string | undefined = undefined; + try { + fileContents = await this.ide.readFile(filepath); + } catch (err) { + // File removed + return null; + } + + const ast = parser.parse(fileContents, undefined, { includedRanges: [ { startIndex: 0, diff --git a/core/util/LruCache.ts b/core/util/LruCache.ts index f6d923fed..86925a6bc 100644 --- a/core/util/LruCache.ts +++ b/core/util/LruCache.ts @@ -1,7 +1,7 @@ export class PrecalculatedLruCache { private items: [string, V][] = []; constructor( - private readonly calculateValue: (key: string) => Promise, + private readonly calculateValue: (key: string) => Promise, private readonly N: number, ) {} @@ -11,7 +11,11 @@ export class PrecalculatedLruCache { if (index < 0) { // Calculate info for new file - const value: V = await this.calculateValue(key); + const value: V | null = await this.calculateValue(key); + + if (value === null) { + return; + } this.items.push([key, value]); if (this.items.length > this.N) {