Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/workspaces.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import assert from "node:assert/strict";
import { loadConfig } from "./config.js";
import { GitWorktreeError } from "./git-worktrees.js";
import { SqliteWorkspaceStore } from "./workspace-store.js";
import { WorkspaceRegistry } from "./workspaces.js";
import { ensureCheckoutWorkspaceRoot, WorkspaceRegistry } from "./workspaces.js";

const execFileAsync = promisify(execFile);
const root = await mkdtemp(join(tmpdir(), "devspace-workspace-test-"));
Expand Down Expand Up @@ -47,6 +47,21 @@ try {
assert.equal(missingWorkspace.workspace.mode, "checkout");
assert.equal((await stat(missingWorkspaceRoot)).isDirectory(), true);

{
let mkdirCalls = 0;
const existingStats = await ensureCheckoutWorkspaceRoot(root, {
stat: async (path) => {
assert.equal(path, root);
return await stat(path);
},
mkdir: async () => {
mkdirCalls += 1;
},
});
assert.equal(existingStats.isDirectory(), true);
assert.equal(mkdirCalls, 0);
}

await assert.rejects(
() => registry.openWorkspace({ path: root, mode: "worktree" }),
(error: unknown) =>
Expand Down
31 changes: 28 additions & 3 deletions src/workspaces.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { randomUUID } from "node:crypto";
import type { Stats } from "node:fs";
import type { WorkspaceMode, WorkspaceStore } from "./workspace-store.js";
import { mkdir, opendir, stat } from "node:fs/promises";
import { dirname, join, relative, resolve, sep } from "node:path";
Expand Down Expand Up @@ -61,6 +62,12 @@ export interface OpenWorkspaceInput {
baseRef?: string;
}

type PathStats = Stats;
type DirectoryOps = {
stat: (path: string) => Promise<PathStats>;
mkdir: (path: string, options: { recursive: true }) => Promise<unknown>;
};

export class WorkspaceRegistry {
private readonly workspaces = new Map<string, Workspace>();

Expand Down Expand Up @@ -162,9 +169,7 @@ export class WorkspaceRegistry {

private async openCheckoutWorkspace(path: string): Promise<WorkspaceContext> {
const root = assertAllowedPath(path, this.config.allowedRoots);
await mkdir(root, { recursive: true });

const rootStats = await stat(root);
const rootStats = await ensureCheckoutWorkspaceRoot(root);
if (!rootStats.isDirectory()) {
throw new Error(`Workspace root must be a directory: ${path}`);
}
Expand Down Expand Up @@ -273,6 +278,22 @@ export class WorkspaceRegistry {
}
}

export async function ensureCheckoutWorkspaceRoot(
path: string,
ops: DirectoryOps = { stat, mkdir },
): Promise<PathStats> {
try {
return await ops.stat(path);
} catch (error) {
if (!isErrnoException(error) || error.code !== "ENOENT") {
throw error;
}
}

await ops.mkdir(path, { recursive: true });
return await ops.stat(path);
}

const CONTEXT_FILE_NAMES = new Set(["AGENTS.md", "AGENTS.MD", "CLAUDE.md", "CLAUDE.MD"]);
const SKIPPED_CONTEXT_DIRS = new Set([
".git",
Expand Down Expand Up @@ -326,3 +347,7 @@ async function walkWorkspace(
await visit(path, entry);
}
}

function isErrnoException(error: unknown): error is NodeJS.ErrnoException {
return error instanceof Error && "code" in error;
}