diff --git a/packages/cli/src/commands/workspace/create.test.ts b/packages/cli/src/commands/workspace/create.test.ts index 62bfcdc6f1..a0c03b5e0d 100644 --- a/packages/cli/src/commands/workspace/create.test.ts +++ b/packages/cli/src/commands/workspace/create.test.ts @@ -1,3 +1,4 @@ +import path from "node:path"; import { describe, expect, it } from "vitest"; import { buildWorkspaceSource } from "./create.js"; @@ -8,6 +9,49 @@ describe("workspace create source", () => { ).toEqual({ kind: "directory", path: "/tmp/project", projectId: "project-1" }); }); + it("resolves a relative path against the directory the command runs in", () => { + const daemonTarget = { kind: "instance" as const, home: "/tmp/paseo-home" }; + expect(buildWorkspaceSource({ daemonTarget, isolation: "local", path: "." })).toEqual({ + kind: "directory", + path: process.cwd(), + }); + expect( + buildWorkspaceSource({ daemonTarget, isolation: "worktree", path: "../sibling" }), + ).toEqual({ + kind: "worktree", + cwd: path.resolve(process.cwd(), "../sibling"), + action: "branch-off", + }); + }); + + it("sends a relative path to an explicit daemon endpoint as written", () => { + const daemonTarget = { kind: "endpoint" as const, host: "other-box:6767" }; + expect(buildWorkspaceSource({ daemonTarget, isolation: "local", path: "repo" })).toEqual({ + kind: "directory", + path: "repo", + }); + }); + + it("passes an absolute path from another operating system through unchanged", () => { + expect(buildWorkspaceSource({ isolation: "local", path: "C:\\repos\\project" })).toEqual({ + kind: "directory", + path: "C:\\repos\\project", + }); + }); + + it("leaves a home-relative path for the daemon to expand", () => { + const daemonTarget = { kind: "instance" as const, home: "/tmp/paseo-home" }; + expect(buildWorkspaceSource({ daemonTarget, isolation: "local", path: "~/repo" })).toEqual({ + kind: "directory", + path: "~/repo", + }); + expect(buildWorkspaceSource({ daemonTarget, isolation: "worktree", path: "~" })).toEqual({ + kind: "worktree", + cwd: "~", + action: "branch-off", + }); + }); + it("keeps branch names separate from managed worktree slugs", () => { expect( buildWorkspaceSource({ diff --git a/packages/cli/src/commands/workspace/create.ts b/packages/cli/src/commands/workspace/create.ts index 04d9e10ef0..63417587cb 100644 --- a/packages/cli/src/commands/workspace/create.ts +++ b/packages/cli/src/commands/workspace/create.ts @@ -1,6 +1,10 @@ +import { isAbsolute as isPosixAbsolute } from "node:path/posix"; +import { isAbsolute as isWindowsAbsolute } from "node:path/win32"; +import { resolve } from "node:path"; import type { Command } from "commander"; import { connectToDaemon, getDaemonHost } from "../../utils/client.js"; import { resolveCallerAgentId } from "../../utils/caller-agent.js"; +import type { DaemonTarget } from "../../utils/daemon-target.js"; import type { CommandError, CommandOptions, SingleResult } from "../../output/index.js"; import { toWorkspaceRow, workspaceSchema, type WorkspaceRow } from "./shared.js"; @@ -121,12 +125,26 @@ function buildWorktreeWorkspaceSource(options: WorkspaceCreateOptions, path: str } } +// A relative --path names a directory relative to where the command runs, so +// it is made absolute here for a local daemon, which would otherwise resolve +// it against its own working directory. For an explicit endpoint the path is +// sent as written, the same rule as resolveRepositoryDirectory. An absolute +// path can target a daemon on a different operating system, and the daemon +// expands a leading ~ to its own home directory. +function resolveCallerPath(input: string, daemonTarget: DaemonTarget): string { + const isHomeRelative = input === "~" || input.startsWith("~/"); + if (isHomeRelative || isPosixAbsolute(input) || isWindowsAbsolute(input)) return input; + return daemonTarget.kind === "instance" ? resolve(input) : input; +} + export function buildWorkspaceSource(options: WorkspaceCreateOptions) { + const callerPath = + options.path === undefined ? undefined : resolveCallerPath(options.path, options.daemonTarget); if (options.isolation === "local") { - return buildLocalWorkspaceSource(options, options.path ?? process.cwd()); + return buildLocalWorkspaceSource(options, callerPath ?? process.cwd()); } if (options.isolation === "worktree") { - const sourcePath = options.path ?? (options.project ? undefined : process.cwd()); + const sourcePath = callerPath ?? (options.project ? undefined : process.cwd()); return buildWorktreeWorkspaceSource(options, sourcePath); } throw new Error(`Unsupported workspace isolation: ${String(options.isolation)}`);