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

Function declaredPythonRoots

src/resolution/import-resolver.ts:775–821  ·  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

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