From cdba65d4c8931a2a2b2dfa26df0f483973f1d3d2 Mon Sep 17 00:00:00 2001 From: Eric Traut Date: Wed, 3 Aug 2022 11:12:12 -0700 Subject: [PATCH] Fixed a bug that caused incorrect type evaluation because of stale module paths that are cached for specific source files. Module paths are dependent on the list of configured import resolution paths, so when the import resolution paths change, we may need to recompute the module path for a source file. --- packages/pyright-internal/src/analyzer/program.ts | 6 +++++- packages/pyright-internal/src/analyzer/sourceFile.ts | 10 +++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/packages/pyright-internal/src/analyzer/program.ts b/packages/pyright-internal/src/analyzer/program.ts index b88414b24..40b1b8df9 100644 --- a/packages/pyright-internal/src/analyzer/program.ts +++ b/packages/pyright-internal/src/analyzer/program.ts @@ -262,12 +262,16 @@ export class Program { addTrackedFile(filePath: string, isThirdPartyImport = false, isInPyTypedPackage = false): SourceFile { let sourceFileInfo = this._getSourceFileInfoFromPath(filePath); + const importName = this._getImportNameForFile(filePath); + if (sourceFileInfo) { + // The module name may have changed based on updates to the + // search paths, so update it here. + sourceFileInfo.sourceFile.setModuleName(importName); sourceFileInfo.isTracked = true; return sourceFileInfo.sourceFile; } - const importName = this._getImportNameForFile(filePath); const sourceFile = new SourceFile( this._fs, filePath, diff --git a/packages/pyright-internal/src/analyzer/sourceFile.ts b/packages/pyright-internal/src/analyzer/sourceFile.ts index e449f9b33..211ba9971 100644 --- a/packages/pyright-internal/src/analyzer/sourceFile.ts +++ b/packages/pyright-internal/src/analyzer/sourceFile.ts @@ -97,7 +97,7 @@ export class SourceFile { private readonly _filePath: string; // Period-delimited import path for the module. - private readonly _moduleName: string; + private _moduleName: string; // True if file is a type-hint (.pyi) file versus a python // (.py) file. @@ -247,6 +247,14 @@ export class SourceFile { return this._filePath; } + getModuleName(): string { + return this._moduleName; + } + + setModuleName(name: string) { + this._moduleName = name; + } + getDiagnosticVersion(): number { return this._diagnosticVersion; }