Skip to content

Turbocharge Claude Code, Cursor, Codex, Gemini & every coding agent: faster, cheaper, with contextual understanding specific to your codebase.

fix(graph): workspace-in-workspace collapses to the shallower scope

Shrish Dwivedi874e0b6ad1f4
diff --git a/src/graph/scopes.ts b/src/graph/scopes.ts
index b04773fc..6b64918f 100644
--- a/src/graph/scopes.ts
+++ b/src/graph/scopes.ts
@@ -234,12 +234,17 @@ export function discoverScopes(root: string): ScopeV1[] {
// Applying them produces the "post-glob-resolution" stable set.
//
// Pass B — plain nesting collapse (uses the POST-A set, NOT the original
- // frozen set): for every remaining NON-workspace candidate, find its
- // nearest ancestor in the post-A set; if one exists, queue the deeper
- // candidate for deletion (keep the shallower ancestor). Workspace
- // candidates are never deletion targets here — Pass A already resolved
- // a workspace match's relationship to its blocking parent, so it always
- // survives regardless of any remaining (grand)ancestor.
+ // frozen set): for every remaining candidate, find its nearest ancestor
+ // in the post-A set; if one exists, the deeper candidate is queued for
+ // deletion (keep the shallower ancestor) UNLESS it's a workspace match
+ // whose ancestor is NOT itself a workspace match — that's the one case
+ // Pass A already grants immunity to ("workspace beats a non-workspace
+ // parent"). Rule 4's workspace exception does NOT extend to a workspace
+ // candidate nested under ANOTHER workspace candidate (e.g. a recursive
+ // `packages/**` glob matching both `packages/a` and `packages/a/b`, or a
+ // `packages/**` glob sweeping every markerless dir under a real package)
+ // — that pair falls back to the plain rule, same as any other nested
+ // pair: keep the shallower, drop the deeper.
//
// Running Pass B against the post-A set (rather than the frozen one) is
// what makes a Pass-A deletion visible downstream: a candidate whose only
@@ -271,8 +276,15 @@ export function discoverScopes(root: string): ScopeV1[] {
const postA: ReadonlyMap<string, Candidate> = new Map(candidates);
const passBDeletes = new Set<string>();
for (const [prefix, entry] of postA) {
- if (prefix === "" || entry.isWorkspace) continue;
- if (findNearestAncestorCandidate(prefix, postA)) passBDeletes.add(prefix);
+ if (prefix === "") continue;
+ const ancestor = findNearestAncestorCandidate(prefix, postA);
+ if (!ancestor) continue;
+ const ancestorEntry = postA.get(ancestor)!;
+ // Keep only a workspace candidate whose nearest surviving ancestor is
+ // NOT itself a workspace match (Pass A's exemption). Every other nested
+ // pair — non-workspace child under anything, or workspace child under an
+ // ALSO-workspace ancestor — collapses to the shallower one here.
+ if (!entry.isWorkspace || ancestorEntry.isWorkspace) passBDeletes.add(prefix);
}
for (const prefix of passBDeletes) candidates.delete(prefix);
diff --git a/test/graph-scopes.test.ts b/test/graph-scopes.test.ts
index 3cc3139a..09c725bd 100644
--- a/test/graph-scopes.test.ts
+++ b/test/graph-scopes.test.ts
@@ -110,6 +110,39 @@ test("literal (non-glob) workspace entry resolves as a workspace match", () => {
rmSync(d, { recursive: true, force: true });
});
+test("workspace-under-workspace collapses to the shallower scope (packages/**)", () => {
+ const d = fx({
+ "pnpm-workspace.yaml": "packages:\n - 'packages/**'\n",
+ "packages/a/package.json": "{}",
+ "packages/a/b/package.json": "{}",
+ });
+ const prefixes = discoverScopes(d).map((s) => s.prefix).sort();
+ assert.deepEqual(prefixes, ["packages/a"]);
+ rmSync(d, { recursive: true, force: true });
+});
+
+test("workspace-under-workspace collapse also sweeps markerless dirs under a **-glob", () => {
+ const d = fx({
+ "pnpm-workspace.yaml": "packages:\n - 'packages/**'\n",
+ "packages/a/package.json": "{}",
+ "packages/a/src/utils/x.ts": "export const x = 1;",
+ });
+ const prefixes = discoverScopes(d).map((s) => s.prefix).sort();
+ assert.deepEqual(prefixes, ["packages/a"]); // no markerless packages/a/src* survives
+ rmSync(d, { recursive: true, force: true });
+});
+
+test("literal + glob workspace overlap collapses to the shallower literal entry", () => {
+ const d = fx({
+ "package.json": JSON.stringify({ workspaces: ["apps", "apps/*"] }),
+ "apps/package.json": "{}",
+ "apps/web/package.json": "{}",
+ });
+ const prefixes = discoverScopes(d).map((s) => s.prefix).sort();
+ assert.deepEqual(prefixes, ["apps"]);
+ rmSync(d, { recursive: true, force: true });
+});
+
test("discoverWorkspaceChildren finds immediate git children only", () => {
const d = fx({ "repoA/x.ts": "1", "repoB/y.py": "1", "plain/z.go": "1" });
mkdirSync(join(d, "repoA/.git"), { recursive: true });