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

Function declaredPythonRoots

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

* Package roots a build config declares, relative to the repo root. Only the * keys that mean "packages live here" are read, each in its own table: * setuptools `[tool.setuptools.packages.find] where` and * `[tool.setuptools.package-dir] "" =` (or the inline `package-dir` of * `[tool.setuptools]

(context: ResolutionContext, service: string)

Source from the content-addressed store, hash-verified

771 * case, which the `__init__.py` chain can't see either.
772 */
773function declaredPythonRoots(context: ResolutionContext, service: string): string[] {
774 const out: string[] = [];
775 const add = (raw: string): void => {
776 const dir = raw.trim().replace(/^["']|["']$/g, '').replace(/^\.(?:\/|$)/, '').replace(/\/+$/, '');
777 if (!dir || dir.startsWith('..') || path.isAbsolute(dir)) return;
778 out.push(joinRel(service, dir));
779 };
780 const toml = context.readFile(joinRel(service, 'pyproject.toml'));
781 if (toml) {
782 for (const [table, body] of iniTables(toml)) {
783 if (table === 'tool.setuptools.packages.find') {
784 const m = body.match(/^\s*where\s*=\s*\[([^\]]*)\]/m);
785 if (m) for (const item of m[1]!.split(',')) add(item);
786 } else if (table === 'tool.setuptools.package-dir') {
787 const m = body.match(/^\s*["']{2}\s*=\s*["']([^"']+)["']/m);
788 if (m) add(m[1]!);
789 } else if (table === 'tool.setuptools') {
790 const m = body.match(/package-dir\s*=\s*\{[^}]*?["']{2}\s*=\s*["']([^"']+)["']/);
791 if (m) add(m[1]!);
792 } else if (table === 'tool.poetry') {
793 const pkgs = body.match(/^\s*packages\s*=\s*\[([\s\S]*?)\]\s*$/m);
794 if (pkgs) for (const m of pkgs[1]!.matchAll(/\bfrom\s*=\s*["']([^"']+)["']/g)) add(m[1]!);
795 } else if (table === 'tool.hatch.build.targets.wheel') {
796 const pkgs = body.match(/^\s*packages\s*=\s*\[([^\]]*)\]/m);
797 if (pkgs) {
798 for (const item of pkgs[1]!.split(',')) {
799 const p = item.trim().replace(/^["']|["']$/g, '');
800 if (p.includes('/')) add(p.slice(0, p.lastIndexOf('/')));
801 }
802 }
803 }
804 }
805 }
806 const cfg = context.readFile(joinRel(service, 'setup.cfg'));
807 if (cfg) {
808 for (const [table, body] of iniTables(cfg)) {
809 if (table === 'options') {
810 const m = body.match(/^\s*package_dir\s*=\s*(?:\n\s+)?=\s*(\S+)\s*$/m);
811 if (m) add(m[1]!);
812 } else if (table === 'options.packages.find') {
813 const m = body.match(/^\s*where\s*=\s*(\S+)\s*$/m);
814 if (m) add(m[1]!);
815 }
816 }
817 }
818 return out;
819}
820
821/** `[table]` sections of a TOML / INI file, with the body under each. */
822function iniTables(content: string): Array<[string, string]> {

Callers 1

buildPythonModuleIndexFunction · 0.85

Calls 4

joinRelFunction · 0.85
iniTablesFunction · 0.85
addFunction · 0.70
readFileMethod · 0.65

Tested by

no test coverage detected