(context: ResolutionContext)
| 709 | } |
| 710 | |
| 711 | function buildPythonModuleIndex(context: ResolutionContext): PythonModuleIndex { |
| 712 | const files = new Set<string>(); |
| 713 | const packageDirs = new Set<string>(); |
| 714 | const dirs = new Set<string>(['']); |
| 715 | for (const f of context.getAllFiles()) { |
| 716 | const norm = f.replace(/\\/g, '/'); |
| 717 | if (!PYTHON_MODULE_FILE.test(norm)) continue; |
| 718 | files.add(norm); |
| 719 | const dir = parentDir(norm); |
| 720 | if (norm === '__init__.py' || norm.endsWith('/__init__.py')) packageDirs.add(dir); |
| 721 | for (let d = dir; d && !dirs.has(d); d = parentDir(d)) dirs.add(d); |
| 722 | } |
| 723 | |
| 724 | // Where a service's roots may answer from: everywhere (null), only itself |
| 725 | // when a package encloses it, or the nearest enclosing directory named as |
| 726 | // non-project code. |
| 727 | const serviceReach = (service: string): string | null => { |
| 728 | if (packageDirs.has(service)) return service; |
| 729 | for (let a = parentDir(service); a; a = parentDir(a)) { |
| 730 | if (packageDirs.has(a)) return service; |
| 731 | if (PYTHON_NON_PROJECT_DIRS.has(a.slice(a.lastIndexOf('/') + 1))) return a; |
| 732 | } |
| 733 | return null; |
| 734 | }; |
| 735 | |
| 736 | const projectRoots = new Set<string>(['']); |
| 737 | const localRoots = new Map<string, string>(); |
| 738 | const addLocal = (root: string, reach: string): void => { |
| 739 | const prior = localRoots.get(root); |
| 740 | // Two claims on one root: the wider reach wins. |
| 741 | if (prior === undefined || isWithin(prior, reach)) localRoots.set(root, reach); |
| 742 | }; |
| 743 | for (const dir of packageDirs) { |
| 744 | let top = dir; |
| 745 | while (top && packageDirs.has(parentDir(top))) top = parentDir(top); |
| 746 | if (top) addLocal(parentDir(top), parentDir(top)); |
| 747 | } |
| 748 | // Whether `dir` is a regular package or lies inside one. |
| 749 | const inPackage = (dir: string): boolean => { |
| 750 | for (let d = dir; d; d = parentDir(d)) if (packageDirs.has(d)) return true; |
| 751 | return false; |
| 752 | }; |
| 753 | // The directories that hold another directory of Python. |
| 754 | const withSubdirs = new Set<string>(); |
| 755 | for (const dir of dirs) if (dir) withSubdirs.add(parentDir(dir)); |
| 756 | for (const service of dirs) { |
| 757 | const hasConfig = |
| 758 | files.has(joinRel(service, 'setup.py')) || |
| 759 | context.fileExists(joinRel(service, 'pyproject.toml')) || |
| 760 | context.fileExists(joinRel(service, 'setup.cfg')); |
| 761 | const isService = hasConfig || service === ''; |
| 762 | // A tool's own config file (`pytest.ini`, `pyrightconfig.json`) can sit |
| 763 | // in a folder with no build config, and looking for one is six probes of |
| 764 | // the disk ({@link PYTHON_TOOL_CONFIGS}). So it is looked for only where |
| 765 | // it can name a folder: in one that holds another folder of Python, and |
| 766 | // is no part of a package, which is not where a tool is run from. That |
| 767 | // leaves out the leaves of the tree, nearly all of it, and a tree of |
| 768 | // packages whole. |
no test coverage detected