* fix(#2623): resolve parent .planning root for sub_repos workspaces in SDK query dispatch When `gsd-sdk query` is invoked from inside a `sub_repos`-listed child repo, `projectDir` defaulted to `process.cwd()` which pointed at the child repo, not the parent workspace that owns `.planning/`. Handlers then directly checked `${projectDir}/.planning` and reported `project_exists: false`. The legacy `gsd-tools.cjs` CLI does not have this gap — it calls `findProjectRoot(cwd)` from `bin/lib/core.cjs`, which walks up from the starting directory checking each ancestor's `.planning/config.json` for a `sub_repos` entry that lists the starting directory's top-level segment. This change ports that walk-up as a new `findProjectRoot` helper in `sdk/src/query/helpers.ts` and applies it once in `cli.ts:main()` before dispatching `query`, `run`, `init`, or `auto`. Resolution is idempotent: if `projectDir` already owns `.planning/` (including an explicit `--project-dir` pointing at the workspace root), the helper returns it unchanged. The walk is capped at 10 parent levels and never crosses `$HOME`. All filesystem errors are swallowed. Regression coverage: - `helpers.test.ts` — 8 unit tests covering own-`.planning` guard (#1362), sub_repos match, nested-path match, `planning.sub_repos` shape, heuristic fallback, unparseable config, legacy `multiRepo: true`. - `sub-repos-root.integration.test.ts` — end-to-end baseline (reproduces the bug without the walk-up) and fixed behavior (walk-up + dispatch of `init.new-milestone` reports `project_exists: true` with the parent workspace as `project_root`). sdk vitest: 1511 pass / 24 fail (all 24 failures pre-existing on main, baseline is 26 failing — `comm -23` against baseline produces zero new failures). CJS: 5410 pass / 0 fail. Closes #2623 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * fix(#2623): remove stray .planing typo from integration test setup Address CodeRabbit nitpick: the mkdir('.planing') call on line 23 was dead code from a typo, with errors silently swallowed via .catch(() => {}). The test already creates '.planning' correctly on the next line. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -17,8 +17,9 @@
|
||||
* ```
|
||||
*/
|
||||
|
||||
import { join, dirname, relative, resolve, isAbsolute, normalize } from 'node:path';
|
||||
import { join, dirname, relative, resolve, isAbsolute, normalize, parse as parsePath, sep as pathSep } from 'node:path';
|
||||
import { realpath } from 'node:fs/promises';
|
||||
import { existsSync, statSync, readFileSync } from 'node:fs';
|
||||
import { homedir } from 'node:os';
|
||||
import { GSDError, ErrorClassification } from '../errors.js';
|
||||
import { relPlanningPath } from '../workstream-utils.js';
|
||||
@@ -428,6 +429,134 @@ export function planningPaths(projectDir: string, workstream?: string): Planning
|
||||
};
|
||||
}
|
||||
|
||||
// ─── findProjectRoot (multi-repo .planning resolution) ─────────────────────
|
||||
|
||||
/**
|
||||
* Maximum number of parent directories to walk when searching for a
|
||||
* multi-repo `.planning/` root. Bounded to avoid scanning to the filesystem
|
||||
* root in pathological cases.
|
||||
*/
|
||||
const FIND_PROJECT_ROOT_MAX_DEPTH = 10;
|
||||
|
||||
/**
|
||||
* Walk up from `startDir` to find the project root that owns `.planning/`.
|
||||
*
|
||||
* Ported from `get-shit-done/bin/lib/core.cjs:findProjectRoot` so that
|
||||
* `gsd-sdk query` resolves the same parent `.planning/` root as the legacy
|
||||
* `gsd-tools.cjs` CLI when invoked inside a `sub_repos`-listed child repo.
|
||||
*
|
||||
* Detection strategy (checked in order for each ancestor, up to
|
||||
* `FIND_PROJECT_ROOT_MAX_DEPTH` levels):
|
||||
* 1. `startDir` itself has `.planning/` — return it unchanged (#1362).
|
||||
* 2. Parent has `.planning/config.json` with `sub_repos` listing the
|
||||
* immediate child segment of the starting directory.
|
||||
* 3. Parent has `.planning/config.json` with `multiRepo: true` (legacy).
|
||||
* 4. Parent has `.planning/` AND an ancestor of `startDir` (up to the
|
||||
* candidate parent) contains `.git` — heuristic fallback.
|
||||
*
|
||||
* Returns `startDir` unchanged when no ancestor `.planning/` is found
|
||||
* (first-run or single-repo projects). Never walks above the user's home
|
||||
* directory.
|
||||
*
|
||||
* All filesystem errors are swallowed — a missing or unparseable
|
||||
* `config.json` falls back to the `.git` heuristic, and unreadable
|
||||
* directories terminate the walk at that level.
|
||||
*/
|
||||
export function findProjectRoot(startDir: string): string {
|
||||
let resolvedStart: string;
|
||||
try {
|
||||
resolvedStart = resolve(startDir);
|
||||
} catch {
|
||||
return startDir;
|
||||
}
|
||||
const fsRoot = parsePath(resolvedStart).root;
|
||||
const home = homedir();
|
||||
|
||||
// If startDir already contains .planning/, it IS the project root.
|
||||
try {
|
||||
const ownPlanning = join(resolvedStart, '.planning');
|
||||
if (existsSync(ownPlanning) && statSync(ownPlanning).isDirectory()) {
|
||||
return startDir;
|
||||
}
|
||||
} catch {
|
||||
// fall through
|
||||
}
|
||||
|
||||
// Walk upward, mirroring isInsideGitRepo from the CJS reference.
|
||||
function isInsideGitRepo(candidateParent: string): boolean {
|
||||
let d = resolvedStart;
|
||||
while (d !== fsRoot) {
|
||||
try {
|
||||
if (existsSync(join(d, '.git'))) return true;
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
if (d === candidateParent) break;
|
||||
const next = dirname(d);
|
||||
if (next === d) break;
|
||||
d = next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
let dir = resolvedStart;
|
||||
let depth = 0;
|
||||
while (dir !== fsRoot && depth < FIND_PROJECT_ROOT_MAX_DEPTH) {
|
||||
const parent = dirname(dir);
|
||||
if (parent === dir) break;
|
||||
if (parent === home) break;
|
||||
|
||||
const parentPlanning = join(parent, '.planning');
|
||||
let parentPlanningIsDir = false;
|
||||
try {
|
||||
parentPlanningIsDir = existsSync(parentPlanning) && statSync(parentPlanning).isDirectory();
|
||||
} catch {
|
||||
parentPlanningIsDir = false;
|
||||
}
|
||||
|
||||
if (parentPlanningIsDir) {
|
||||
const configPath = join(parentPlanning, 'config.json');
|
||||
let matched = false;
|
||||
try {
|
||||
const raw = readFileSync(configPath, 'utf-8');
|
||||
const config = JSON.parse(raw) as {
|
||||
sub_repos?: unknown;
|
||||
planning?: { sub_repos?: unknown };
|
||||
multiRepo?: unknown;
|
||||
};
|
||||
const subReposValue =
|
||||
(config.sub_repos as unknown) ?? (config.planning && config.planning.sub_repos);
|
||||
const subRepos = Array.isArray(subReposValue) ? (subReposValue as unknown[]) : [];
|
||||
|
||||
if (subRepos.length > 0) {
|
||||
const relPath = relative(parent, resolvedStart);
|
||||
const topSegment = relPath.split(pathSep)[0];
|
||||
if (subRepos.includes(topSegment)) {
|
||||
return parent;
|
||||
}
|
||||
}
|
||||
|
||||
if (config.multiRepo === true && isInsideGitRepo(parent)) {
|
||||
matched = true;
|
||||
}
|
||||
} catch {
|
||||
// config.json missing or unparseable — fall through to .git heuristic.
|
||||
}
|
||||
|
||||
if (matched) return parent;
|
||||
|
||||
// Heuristic: parent has .planning/ and we're inside a git repo.
|
||||
if (isInsideGitRepo(parent)) {
|
||||
return parent;
|
||||
}
|
||||
}
|
||||
|
||||
dir = parent;
|
||||
depth += 1;
|
||||
}
|
||||
return startDir;
|
||||
}
|
||||
|
||||
// ─── resolvePathUnderProject ───────────────────────────────────────────────
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user