diff --git a/src/features/project-tree/packages.ts b/src/features/project-tree/packages.ts index bdbda22098..c710593279 100644 --- a/src/features/project-tree/packages.ts +++ b/src/features/project-tree/packages.ts @@ -20,8 +20,16 @@ export function packageKindAt( ): PackageKind | null { if (rootFor(roots, rel)?.kind === "excluded") return null; if (isJvmSourcePackage(rel, roots)) return "jvm"; - const python = [...roots, ...detected].some( - (r) => r.detectedFrom && PY_BUILD.test(r.detectedFrom) && within(rel, moduleDir(r.detectedFrom)), - ); - return python ? "python" : null; + // A root `pyproject.toml` must not claim `frontend/src`, a root that + // `frontend/package.json` detected, unless a deeper Python module holds the folder. + let python = -1; + let other = -1; + for (const r of [...roots, ...detected]) { + if (!r.detectedFrom) continue; + if (PY_BUILD.test(r.detectedFrom)) { + const dir = moduleDir(r.detectedFrom); + if (within(rel, dir)) python = Math.max(python, dir.length); + } else if (within(rel, r.path)) other = Math.max(other, r.path.length); + } + return python >= 0 && python >= other ? "python" : null; } diff --git a/src/features/project-tree/roots.test.ts b/src/features/project-tree/roots.test.ts index 3649b34e43..66a6103611 100644 --- a/src/features/project-tree/roots.test.ts +++ b/src/features/project-tree/roots.test.ts @@ -72,6 +72,25 @@ describe("New › Package", () => { expect(packageKindAt("build/classes", synced)).toBeNull(); }); + it("keeps a nested non-Python module out of a root-level Python project", () => { + // The roots detection reports for a root `pyproject.toml` next to `frontend/package.json`. + const mixed = [ + root("src", "source", "pyproject.toml"), + root("frontend/src", "source", "frontend/package.json"), + root("frontend/node_modules", "excluded", "frontend/package.json"), + ]; + expect(packageKindAt("frontend/src/components", mixed)).toBeNull(); + expect(packageKindAt("src/app", mixed)).toBe("python"); + expect(packageKindAt("scripts", mixed)).toBe("python"); + }); + + it("keeps Python packages next to a nested package.json outside its roots", () => { + // A Django app folder with its own `package.json` for front-end tooling. + const django = [root("", "source", "pyproject.toml"), root("web/node_modules", "excluded", "web/package.json")]; + expect(packageKindAt("web/blog", django)).toBe("python"); + expect(packageKindAt("web", django)).toBe("python"); + }); + it("validates dotted package names", () => { expect(packageSegments("com.acme.util", "jvm")).toEqual({ segments: ["com", "acme", "util"], error: null }); expect(packageSegments("com..x", "jvm").error).toMatch(/Not a valid package name/);