Skip to content

armgit / Ix

Public Imported from GitHub
0

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

fix(config): respect workspace path boundaries (#403)

Hiro-Chibacdb245380ede
diff --git a/ix-cli/src/cli/__tests__/config-workspace-boundary.test.ts b/ix-cli/src/cli/__tests__/config-workspace-boundary.test.ts
new file mode 100644
index 00000000..c5a22b97
--- /dev/null
+++ b/ix-cli/src/cli/__tests__/config-workspace-boundary.test.ts
@@ -0,0 +1,35 @@
+import { describe, expect, it } from "vitest";
+
+import { selectWorkspaceForCwd, type WorkspaceConfig } from "../config.js";
+
+function workspace(rootPath: string, name = rootPath): WorkspaceConfig {
+ return {
+ workspace_id: name,
+ workspace_name: name,
+ root_path: rootPath,
+ default: false,
+ };
+}
+
+describe("workspace path matching", () => {
+ it("matches a workspace root and its descendants", () => {
+ const candidate = workspace("/work/app", "app");
+
+ expect(selectWorkspaceForCwd([candidate], "/work/app")).toBe(candidate);
+ expect(selectWorkspaceForCwd([candidate], "/work/app/src/features")).toBe(candidate);
+ });
+
+ it("does not match a sibling whose name only shares the root prefix", () => {
+ const candidate = workspace("/work/app", "app");
+
+ expect(selectWorkspaceForCwd([candidate], "/work/app-copy")).toBeUndefined();
+ expect(selectWorkspaceForCwd([candidate], "/work/application")).toBeUndefined();
+ });
+
+ it("selects the nearest workspace when roots are nested", () => {
+ const parent = workspace("/work/app", "parent");
+ const child = workspace("/work/app/packages/api", "child");
+
+ expect(selectWorkspaceForCwd([parent, child], "/work/app/packages/api/src")).toBe(child);
+ });
+});
diff --git a/ix-cli/src/cli/config.ts b/ix-cli/src/cli/config.ts
index ea5b8ccb..f0136978 100644
--- a/ix-cli/src/cli/config.ts
+++ b/ix-cli/src/cli/config.ts
@@ -1,5 +1,5 @@
import { readFileSync, writeFileSync, existsSync, rmSync, chmodSync, renameSync } from "node:fs";
-import { join, resolve as resolvePath } from "node:path";
+import { isAbsolute, join, relative, resolve as resolvePath, sep } from "node:path";
import { homedir } from "node:os";
import { createHash } from "node:crypto";
import { execSync } from "node:child_process";
@@ -133,13 +133,26 @@ export function loadWorkspaces(): WorkspaceConfig[] {
return config.workspaces ?? []; // workspace_id already normalized to string in loadConfig
}
-export function findWorkspaceForCwd(cwd: string): WorkspaceConfig | undefined {
- const workspaces = loadWorkspaces();
+export function selectWorkspaceForCwd(
+ workspaces: WorkspaceConfig[],
+ cwd: string,
+): WorkspaceConfig | undefined {
+ const resolvedCwd = resolvePath(cwd);
return workspaces
- .filter(w => cwd.startsWith(w.root_path))
+ .filter((workspace) => {
+ const relativePath = relative(resolvePath(workspace.root_path), resolvedCwd);
+ return (
+ relativePath === "" ||
+ (relativePath !== ".." && !relativePath.startsWith(`..${sep}`) && !isAbsolute(relativePath))
+ );
+ })
.sort((a, b) => b.root_path.length - a.root_path.length)[0];
}
+export function findWorkspaceForCwd(cwd: string): WorkspaceConfig | undefined {
+ return selectWorkspaceForCwd(loadWorkspaces(), cwd);
+}
+
export function getDefaultWorkspace(): WorkspaceConfig | undefined {
return loadWorkspaces().find(w => w.default);
}