From f8a148c06deb729443d507d9a2a671083d4456a3 Mon Sep 17 00:00:00 2001 From: audichuang <72485427+audichuang@users.noreply.github.com> Date: Thu, 26 Nov 2026 18:34:37 +0800 Subject: [PATCH] fix(project-tree): don't offer Python Package in a nested non-Python module's roots (#6028) In a project with a root Python build file and a nested JavaScript app (frontend/package.json), New inside frontend/src offered Python Package: any folder under a Python build file counted as Python, even inside a root a deeper non-Python build file detected. The innermost claim now wins; folders outside those roots, and roots a Python build file claimed first, keep Python Package. Co-authored-by: audichuang <72485427+audichuang@users.noreply.github.com> --- src/features/project-tree/packages.ts | 16 ++++++++++++---- src/features/project-tree/roots.test.ts | 19 +++++++++++++++++++ 2 files changed, 31 insertions(+), 4 deletions(-) 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/);