MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / findPythonModuleFile

Function findPythonModuleFile

src/resolution/import-resolver.ts:1781–1798  ·  view source on GitHub ↗

* Find the file node for a Python dotted module path `a.b.c` — a module file * ending in `a/b/c.py`, or a package `a/b/c/__init__.py` (suffix-matched, so a * package rooted under `src/` etc. still resolves). Returns null for * stdlib/external modules (no matching repo file node), so `import os` c

(
  mod: string,
  context: ResolutionContext,
  excludeFilePath: string
)

Source from the content-addressed store, hash-verified

1779 * (where `c` is a submodule) resolution.
1780 */
1781function findPythonModuleFile(
1782 mod: string,
1783 context: ResolutionContext,
1784 excludeFilePath: string
1785): Node | null {
1786 if (!mod || mod.startsWith('.')) return null; // relative imports handled elsewhere
1787 const rel = mod.replace(/\./g, '/');
1788 const lastSeg = mod.split('.').pop()!;
1789 const endsWith = (p: string, want: string): boolean => p === want || p.endsWith('/' + want);
1790 const moduleFile = context
1791 .getNodesByName(`${lastSeg}.py`)
1792 .find((n) => n.kind === 'file' && n.filePath !== excludeFilePath && endsWith(n.filePath, `${rel}.py`));
1793 if (moduleFile) return moduleFile;
1794 const pkgFile = context
1795 .getNodesByName('__init__.py')
1796 .find((n) => n.kind === 'file' && n.filePath !== excludeFilePath && endsWith(n.filePath, `${rel}/__init__.py`));
1797 return pkgFile ?? null;
1798}
1799
1800/**
1801 * Resolve a Python ABSOLUTE dotted module import (`import a.b.c`) to its file —

Callers 3

Calls 2

endsWithFunction · 0.85
getNodesByNameMethod · 0.65

Tested by

no test coverage detected