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

Function declaredPythonRoots

src/resolution/import-resolver.ts:820–866  ·  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]

(read: (name: string) => string | null, service: string)

Source from the content-addressed store, hash-verified

818 * of the service by name, or null.
819 */
820function declaredPythonRoots(read: (name: string) => string | null, service: string): string[] {
821 const out: string[] = [];
822 const add = (raw: string): void => {
823 const dir = raw.trim().replace(/^["']|["']$/g, '').replace(/^\.(?:\/|$)/, '').replace(/\/+$/, '');
824 if (!dir || dir.startsWith('..') || path.isAbsolute(dir)) return;
825 out.push(joinRel(service, dir));
826 };
827 const toml = read('pyproject.toml');
828 if (toml) {
829 for (const [table, body] of iniTables(toml)) {
830 if (table === 'tool.setuptools.packages.find') {
831 const m = body.match(/^\s*where\s*=\s*\[([^\]]*)\]/m);
832 if (m) for (const item of m[1]!.split(',')) add(item);
833 } else if (table === 'tool.setuptools.package-dir') {
834 const m = body.match(/^\s*["']{2}\s*=\s*["']([^"']+)["']/m);
835 if (m) add(m[1]!);
836 } else if (table === 'tool.setuptools') {
837 const m = body.match(/package-dir\s*=\s*\{[^}]*?["']{2}\s*=\s*["']([^"']+)["']/);
838 if (m) add(m[1]!);
839 } else if (table === 'tool.poetry') {
840 const pkgs = body.match(/^\s*packages\s*=\s*\[([\s\S]*?)\]\s*$/m);
841 if (pkgs) for (const m of pkgs[1]!.matchAll(/\bfrom\s*=\s*["']([^"']+)["']/g)) add(m[1]!);
842 } else if (table === 'tool.hatch.build.targets.wheel') {
843 const pkgs = body.match(/^\s*packages\s*=\s*\[([^\]]*)\]/m);
844 if (pkgs) {
845 for (const item of pkgs[1]!.split(',')) {
846 const p = item.trim().replace(/^["']|["']$/g, '');
847 if (p.includes('/')) add(p.slice(0, p.lastIndexOf('/')));
848 }
849 }
850 }
851 }
852 }
853 const cfg = read('setup.cfg');
854 if (cfg) {
855 for (const [table, body] of iniTables(cfg)) {
856 if (table === 'options') {
857 const m = body.match(/^\s*package_dir\s*=\s*(?:\n\s+)?=\s*(\S+)\s*$/m);
858 if (m) add(m[1]!);
859 } else if (table === 'options.packages.find') {
860 const m = body.match(/^\s*where\s*=\s*(\S+)\s*$/m);
861 if (m) add(m[1]!);
862 }
863 }
864 }
865 return out;
866}
867
868/**
869 * The import paths the tool configs of one folder declare, as written there:

Callers 1

buildPythonModuleIndexFunction · 0.85

Calls 3

iniTablesFunction · 0.85
readFunction · 0.70
addFunction · 0.70

Tested by

no test coverage detected