Skip to content

armgit / Ix

Public Imported from GitHub
0

Understand any codebase instantly. System intelligence for codebases, built for humans and AI.

fix(workspace): a nested linked worktree is its own workspace boundary

KageBinaryafb937297db6
diff --git a/ix-cli/src/cli/__tests__/map-root.test.ts b/ix-cli/src/cli/__tests__/map-root.test.ts
index bf9aec2d..b640ed8f 100644
--- a/ix-cli/src/cli/__tests__/map-root.test.ts
+++ b/ix-cli/src/cli/__tests__/map-root.test.ts
@@ -8,7 +8,7 @@ import { join } from "node:path";
import { Command } from "commander";
import { registerMapCommand } from "../commands/map.js";
import { canonicalMapRoot, resolveIngestRoot, resolveMapRoot } from "../map-root.js";
-import { loadConfig } from "../config.js";
+import { findWorkspaceForCwd, loadConfig } from "../config.js";
import { lockPathForTest } from "../single-flight.js";
const fixtures: string[] = [];
@@ -173,6 +173,76 @@ describe("map root resolution", () => {
});
});
+describe("nested linked worktrees", () => {
+ /** A committed repository registered as a workspace, with a linked worktree at `wtPath`. */
+ function repoWithWorktree(wtRelative: string): { repo: string; wt: string } {
+ const repo = realpathSync.native(fixture());
+ const git = (...args: string[]) =>
+ execFileSync("git", ["-c", "user.name=t", "-c", "user.email=t@t", ...args], { cwd: repo, stdio: "ignore" });
+ git("init", "-q", "-b", "main");
+ writeFileSync(join(repo, "a.ts"), "export const a = 1;\n");
+ git("add", "-A");
+ git("commit", "-q", "-m", "init");
+ const wt = join(repo, wtRelative);
+ git("worktree", "add", "-q", "-b", "feature", wt);
+ register(repo);
+ return { repo, wt: realpathSync.native(wt) };
+ }
+
+ function register(...roots: string[]): void {
+ writeFileSync(join(home, ".ix", "config.yaml"), [
+ "endpoint: http://localhost:8090",
+ "workspaces:",
+ ...roots.flatMap((root, i) => [
+ ` - workspace_id: ws-${i}`,
+ ` workspace_name: ws-${i}`,
+ ` root_path: ${root}`,
+ " default: false",
+ ]),
+ "",
+ ].join("\n"));
+ }
+
+ it("treats a worktree inside a registered repo as unmapped, and maps the worktree itself", () => {
+ const { wt } = repoWithWorktree(join(".claude", "worktrees", "x"));
+ const inside = join(wt, "src");
+ mkdirSync(inside, { recursive: true });
+
+ expect(findWorkspaceForCwd(wt)).toBeUndefined();
+ expect(findWorkspaceForCwd(inside)).toBeUndefined();
+ expect(resolveMapRoot(undefined, wt)).toBe(wt);
+ expect(resolveMapRoot(undefined, inside)).toBe(wt);
+ });
+
+ it("answers for a registered nested worktree from its own workspace", () => {
+ const { repo, wt } = repoWithWorktree(join(".claude", "worktrees", "y"));
+ register(repo, wt);
+ expect(findWorkspaceForCwd(wt)?.workspace_id).toBe("ws-1");
+ expect(findWorkspaceForCwd(repo)?.workspace_id).toBe("ws-0");
+ });
+
+ it("keeps a submodule and a nested clone inside the enclosing workspace", () => {
+ const { repo } = repoWithWorktree(join(".claude", "worktrees", "z"));
+ // A submodule's .git file points into .git/modules/, not .git/worktrees/.
+ const sub = join(repo, "vendor", "sub");
+ mkdirSync(sub, { recursive: true });
+ writeFileSync(join(sub, ".git"), "gitdir: ../../.git/modules/vendor/sub\n");
+ // A nested repository with its own .git directory is not a linked worktree.
+ const clone = join(repo, "third_party", "lib");
+ mkdirSync(join(clone, ".git"), { recursive: true });
+
+ expect(findWorkspaceForCwd(sub)?.workspace_id).toBe("ws-0");
+ expect(findWorkspaceForCwd(clone)?.workspace_id).toBe("ws-0");
+ });
+
+ it("still answers for the main checkout and its subdirectories", () => {
+ const { repo } = repoWithWorktree(join(".claude", "worktrees", "w"));
+ expect(findWorkspaceForCwd(repo)?.workspace_id).toBe("ws-0");
+ mkdirSync(join(repo, "src"), { recursive: true });
+ expect(findWorkspaceForCwd(join(repo, "src"))?.workspace_id).toBe("ws-0");
+ });
+});
+
describe("ingest root resolution", () => {
const writeWorkspaces = (...roots: string[]): void => {
writeFileSync(join(home, ".ix", "config.yaml"), [
diff --git a/ix-cli/src/cli/config.ts b/ix-cli/src/cli/config.ts
index f1c8047f..13cb2b5b 100644
--- a/ix-cli/src/cli/config.ts
+++ b/ix-cli/src/cli/config.ts
@@ -1,7 +1,7 @@
// Copyright 2026 Ix Infrastructure Inc.
-import { readFileSync, writeFileSync, existsSync, rmSync, chmodSync, renameSync, realpathSync, mkdirSync } from "node:fs";
-import { isAbsolute, join, relative, resolve as resolvePath, sep } from "node:path";
+import { readFileSync, writeFileSync, existsSync, rmSync, chmodSync, renameSync, realpathSync, mkdirSync, statSync } from "node:fs";
+import { dirname, isAbsolute, join, relative, resolve as resolvePath, sep } from "node:path";
import { createHash } from "node:crypto";
import { execFileSync } from "node:child_process";
import { parse, stringify } from "yaml";
@@ -326,15 +326,43 @@ export function isReadablePath(candidate: string, explicitRoot?: string): boolea
);
}
+/**
+ * Is `dir` the root of a linked git worktree (`git worktree add`)? Its `.git`
+ * is a file pointing into the main repository's `.git/worktrees/<name>`. A
+ * submodule's `.git` file points into `.git/modules/` instead and is not one:
+ * a submodule is part of the repository that contains it.
+ */
+export function isLinkedWorktreeRoot(dir: string): boolean {
+ const dotGit = join(dir, ".git");
+ try {
+ if (!statSync(dotGit).isFile()) return false;
+ const gitdir = /^gitdir:\s*(.+?)\s*$/m.exec(readFileSync(dotGit, "utf-8"))?.[1];
+ return !!gitdir && /[\\/]worktrees[\\/]/.test(gitdir);
+ } catch {
+ return false;
+ }
+}
+
export function selectWorkspaceForCwd(
workspaces: WorkspaceConfig[],
cwd: string,
): WorkspaceConfig | undefined {
const canonicalCwd = canonicalWorkspacePath(cwd);
- return workspaces
+ const match = workspaces
.map(workspace => ({ workspace, root: canonicalWorkspacePath(workspace.root_path) }))
.filter(({ root }) => isPathInside(root, canonicalCwd))
- .sort((a, b) => b.root.length - a.root.length)[0]?.workspace;
+ .sort((a, b) => b.root.length - a.root.length)[0];
+ if (!match) return undefined;
+ // A linked worktree nested inside a registered repository (Claude Code puts
+ // them at <repo>/.claude/worktrees/<name>) is a separate checkout, usually
+ // on another branch. Answering for it from the enclosing repo's workspace
+ // read and mapped the wrong tree; it is unmapped until it is registered,
+ // exactly like a sibling worktree. Lexically below the match, so a few stats.
+ for (let dir = canonicalCwd; dir !== match.root; dir = dirname(dir)) {
+ if (isLinkedWorktreeRoot(dir)) return undefined;
+ if (dirname(dir) === dir) break;
+ }
+ return match.workspace;
}
export function findWorkspaceForCwd(cwd: string): WorkspaceConfig | undefined {