diff --git a/packages/pyright-internal/src/analyzer/importResolver.ts b/packages/pyright-internal/src/analyzer/importResolver.ts index 269407dd5..0b9e4408b 100644 --- a/packages/pyright-internal/src/analyzer/importResolver.ts +++ b/packages/pyright-internal/src/analyzer/importResolver.ts @@ -1284,6 +1284,13 @@ export class ImportResolver { filePath: string, stripTopContainerDir = false ): ModuleNameInfoFromPath | undefined { + /** + * TODO(https://github.com/sourcegraph/scip-python/issues/91) + * Both paths are casted to lowercase to make the comparison case-insensitive. This has been fixed upstream and should be removed on merge. + */ + containerPath = containerPath.toLowerCase(); + filePath = filePath.toLowerCase(); + containerPath = ensureTrailingDirectorySeparator(containerPath); let filePathWithoutExtension = stripFileExtension(filePath); diff --git a/packages/pyright-scip/snapshots/input/nested_import/__init__.py b/packages/pyright-scip/snapshots/input/nested_import/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/packages/pyright-scip/snapshots/input/nested_import/src/bar/baz.py b/packages/pyright-scip/snapshots/input/nested_import/src/bar/baz.py new file mode 100644 index 000000000..0b315f88a --- /dev/null +++ b/packages/pyright-scip/snapshots/input/nested_import/src/bar/baz.py @@ -0,0 +1,2 @@ +def baz_function(): + pass diff --git a/packages/pyright-scip/snapshots/input/nested_import/src/bar/mod.py b/packages/pyright-scip/snapshots/input/nested_import/src/bar/mod.py new file mode 100644 index 000000000..861f1e52f --- /dev/null +++ b/packages/pyright-scip/snapshots/input/nested_import/src/bar/mod.py @@ -0,0 +1,2 @@ +def mod_function(): + pass diff --git a/packages/pyright-scip/snapshots/input/nested_import/src/bar/toc.py b/packages/pyright-scip/snapshots/input/nested_import/src/bar/toc.py new file mode 100644 index 000000000..bc6260d6a --- /dev/null +++ b/packages/pyright-scip/snapshots/input/nested_import/src/bar/toc.py @@ -0,0 +1,2 @@ +def toc_function(): + pass diff --git a/packages/pyright-scip/snapshots/input/nested_import/src/foo.py b/packages/pyright-scip/snapshots/input/nested_import/src/foo.py new file mode 100644 index 000000000..ea77d0fd0 --- /dev/null +++ b/packages/pyright-scip/snapshots/input/nested_import/src/foo.py @@ -0,0 +1,7 @@ +from .bar.baz import baz_function +from bar.mod import mod_function +from src.bar.toc import toc_function + +baz_function() +mod_function() +toc_function() diff --git a/packages/pyright-scip/snapshots/output/nested_import/__init__.py b/packages/pyright-scip/snapshots/output/nested_import/__init__.py new file mode 100644 index 000000000..47fa30af5 --- /dev/null +++ b/packages/pyright-scip/snapshots/output/nested_import/__init__.py @@ -0,0 +1,4 @@ +# < definition scip-python python snapshot-util 0.1 /__init__: +#documentation (module) + + diff --git a/packages/pyright-scip/snapshots/output/nested_import/src/bar/baz.py b/packages/pyright-scip/snapshots/output/nested_import/src/bar/baz.py new file mode 100644 index 000000000..0dacb203a --- /dev/null +++ b/packages/pyright-scip/snapshots/output/nested_import/src/bar/baz.py @@ -0,0 +1,10 @@ +# < definition scip-python python snapshot-util 0.1 `src.bar.baz`/__init__: +#documentation (module) src.bar.baz + +def baz_function(): +# ^^^^^^^^^^^^ definition snapshot-util 0.1 `src.bar.baz`/baz_function(). +# documentation ```python +# > def baz_function(): # -> None: +# > ``` + pass + diff --git a/packages/pyright-scip/snapshots/output/nested_import/src/bar/mod.py b/packages/pyright-scip/snapshots/output/nested_import/src/bar/mod.py new file mode 100644 index 000000000..898f7abe6 --- /dev/null +++ b/packages/pyright-scip/snapshots/output/nested_import/src/bar/mod.py @@ -0,0 +1,10 @@ +# < definition scip-python python snapshot-util 0.1 `src.bar.mod`/__init__: +#documentation (module) src.bar.mod + +def mod_function(): +# ^^^^^^^^^^^^ definition snapshot-util 0.1 `src.bar.mod`/mod_function(). +# documentation ```python +# > def mod_function(): # -> None: +# > ``` + pass + diff --git a/packages/pyright-scip/snapshots/output/nested_import/src/bar/toc.py b/packages/pyright-scip/snapshots/output/nested_import/src/bar/toc.py new file mode 100644 index 000000000..7f368f67b --- /dev/null +++ b/packages/pyright-scip/snapshots/output/nested_import/src/bar/toc.py @@ -0,0 +1,10 @@ +# < definition scip-python python snapshot-util 0.1 `src.bar.toc`/__init__: +#documentation (module) src.bar.toc + +def toc_function(): +# ^^^^^^^^^^^^ definition snapshot-util 0.1 `src.bar.toc`/toc_function(). +# documentation ```python +# > def toc_function(): # -> None: +# > ``` + pass + diff --git a/packages/pyright-scip/snapshots/output/nested_import/src/foo.py b/packages/pyright-scip/snapshots/output/nested_import/src/foo.py new file mode 100644 index 000000000..070f8330f --- /dev/null +++ b/packages/pyright-scip/snapshots/output/nested_import/src/foo.py @@ -0,0 +1,20 @@ +# < definition scip-python python snapshot-util 0.1 `src.foo`/__init__: +#documentation (module) src.foo + +from .bar.baz import baz_function +# ^^^^^^^^ reference snapshot-util 0.1 `src.bar.baz`/__init__: +# ^^^^^^^^^^^^ reference snapshot-util 0.1 `src.bar.baz`/baz_function(). +from bar.mod import mod_function +# ^^^^^^^ reference snapshot-util 0.1 `src.bar.mod`/__init__: +# ^^^^^^^^^^^^ reference snapshot-util 0.1 `src.bar.mod`/mod_function(). +from src.bar.toc import toc_function +# ^^^^^^^^^^^ reference snapshot-util 0.1 `src.bar.toc`/__init__: +# ^^^^^^^^^^^^ reference snapshot-util 0.1 `src.bar.toc`/toc_function(). + +baz_function() +#^^^^^^^^^^^ reference snapshot-util 0.1 `src.bar.baz`/baz_function(). +mod_function() +#^^^^^^^^^^^ reference snapshot-util 0.1 `src.bar.mod`/mod_function(). +toc_function() +#^^^^^^^^^^^ reference snapshot-util 0.1 `src.bar.toc`/toc_function(). + diff --git a/packages/pyright-scip/snapshots/output/nested_items/src/importer.py b/packages/pyright-scip/snapshots/output/nested_items/src/importer.py index cd90cbb8b..c18cedbe0 100644 --- a/packages/pyright-scip/snapshots/output/nested_items/src/importer.py +++ b/packages/pyright-scip/snapshots/output/nested_items/src/importer.py @@ -2,10 +2,10 @@ #documentation (module) src.importer from foo.bar import InitClass -# ^^^^^^^ reference snapshot-util 0.1 `foo.bar`/__init__: +# ^^^^^^^ reference snapshot-util 0.1 `src.foo.bar`/__init__: # ^^^^^^^^^ reference snapshot-util 0.1 `src.foo.bar`/InitClass# from foo.bar.baz.mod import SuchNestedMuchWow, AnotherNestedMuchWow -# ^^^^^^^^^^^^^^^ reference snapshot-util 0.1 `foo.bar.baz.mod`/__init__: +# ^^^^^^^^^^^^^^^ reference snapshot-util 0.1 `src.foo.bar.baz.mod`/__init__: # ^^^^^^^^^^^^^^^^^ reference snapshot-util 0.1 `src.foo.bar.baz.mod`/SuchNestedMuchWow# # ^^^^^^^^^^^^^^^^^^^^ reference snapshot-util 0.1 `src.foo.bar.baz.mod`/AnotherNestedMuchWow# diff --git a/packages/pyright-scip/snapshots/output/nested_items/src/long_importer.py b/packages/pyright-scip/snapshots/output/nested_items/src/long_importer.py index bc76513e4..bf61c04ca 100644 --- a/packages/pyright-scip/snapshots/output/nested_items/src/long_importer.py +++ b/packages/pyright-scip/snapshots/output/nested_items/src/long_importer.py @@ -2,7 +2,7 @@ #documentation (module) src.long_importer import foo.bar.baz.mod -# ^^^^^^^^^^^^^^^ reference snapshot-util 0.1 `foo.bar.baz.mod`/__init__: +# ^^^^^^^^^^^^^^^ reference snapshot-util 0.1 `src.foo.bar.baz.mod`/__init__: print(foo.bar.baz.mod.SuchNestedMuchWow) #^^^^ reference python-stdlib 3.11 builtins/print(). diff --git a/packages/pyright-scip/src/treeVisitor.ts b/packages/pyright-scip/src/treeVisitor.ts index 5bfcd9ea6..6d3c76145 100644 --- a/packages/pyright-scip/src/treeVisitor.ts +++ b/packages/pyright-scip/src/treeVisitor.ts @@ -202,7 +202,7 @@ export class TreeVisitor extends ParseTreeWalker { [] ); - this.cwd = path.resolve(process.cwd()); + this.cwd = path.resolve(process.cwd()).toLowerCase(); this._docstringWriter = new TypeStubExtendedWriter(this.config.sourceFile, this.evaluator); } @@ -498,13 +498,19 @@ export class TreeVisitor extends ParseTreeWalker { // ^^^^^^^ node.module (create new reference) // ^ node.alias (create new local definition) override visitImportAs(node: ImportAsNode): boolean { - const moduleName = _formatModuleName(node.module); + let moduleName = _formatModuleName(node.module); const importInfo = getImportInfo(node.module); if ( importInfo && - importInfo.resolvedPaths[0] && - path.resolve(importInfo.resolvedPaths[0]).startsWith(this.cwd) + importInfo.resolvedPaths[importInfo.resolvedPaths.length - 1] && + path + .resolve(importInfo.resolvedPaths[importInfo.resolvedPaths.length - 1]) + .toLowerCase() + .startsWith(this.cwd) ) { + moduleName = this.program._getImportNameForFile( + importInfo!.resolvedPaths[importInfo.resolvedPaths.length - 1] + ); const symbol = Symbols.makeModuleInit(this.projectPackage, moduleName); this.pushNewOccurrence(node.module, symbol); } else { @@ -1063,10 +1069,21 @@ export class TreeVisitor extends ParseTreeWalker { pythonPackage = this.stdlibPackage; } - return Symbols.makeModuleInit( - pythonPackage, - node.nameParts.map((namePart) => namePart.value).join('.') - ); + const importInfo = getImportInfo(node); + + let moduleName = node.nameParts.map((namePart) => namePart.value).join('.'); + + if ( + importInfo && + importInfo.resolvedPaths[importInfo.resolvedPaths.length - 1] && + importInfo.resolvedPaths[importInfo.resolvedPaths.length - 1].toLowerCase().startsWith(this.cwd) + ) { + moduleName = this.program._getImportNameForFile( + importInfo.resolvedPaths[importInfo.resolvedPaths.length - 1] + ); + } + + return Symbols.makeModuleInit(pythonPackage, moduleName); } case ParseNodeType.MemberAccess: { softAssert(false, 'Not supposed to get a member access'); @@ -1396,7 +1413,7 @@ export class TreeVisitor extends ParseTreeWalker { const nodeFilePath = path.resolve(nodeFileInfo.filePath); // TODO: Should use files from the package to determine this -- should be able to do that quite easily. - if (nodeFilePath.startsWith(this.cwd)) { + if (nodeFilePath.toLowerCase().startsWith(this.cwd.toLowerCase())) { return this.projectPackage; } @@ -1616,7 +1633,7 @@ export class TreeVisitor extends ParseTreeWalker { if (declPath && declPath.length !== 0) { const p = path.resolve(declPath); - if (p.startsWith(this.cwd)) { + if (p.toLowerCase().startsWith(this.cwd)) { return this.projectPackage; } }