Support PowerShell and resolve workspaces by directory path

This commit is contained in:
Mohamed Boudra
2026-04-04 15:37:26 +07:00
parent 5d5a79a2a8
commit 449a4b48d2
10 changed files with 323 additions and 17 deletions
@@ -6,6 +6,7 @@ import { afterEach, describe, expect, test, vi } from "vitest";
import type { SessionOutboundMessage } from "./messages.js";
import { ServiceRouteStore } from "./service-proxy.js";
import * as worktreeBootstrap from "./worktree-bootstrap.js";
import {
createPaseoWorktreeInBackground,
handleCreatePaseoWorktreeRequest,
@@ -732,6 +733,55 @@ describe("createPaseoWorktreeInBackground", () => {
});
describe("handleCreatePaseoWorktreeRequest", () => {
test("invokes worktree creation once for a create request", async () => {
const { tempDir, repoDir } = createGitRepo();
const paseoHome = path.join(tempDir, ".paseo");
const emitted: SessionOutboundMessage[] = [];
const createAgentWorktreeSpy = vi.spyOn(worktreeBootstrap, "createAgentWorktree");
try {
await handleCreatePaseoWorktreeRequest(
{
paseoHome,
sessionLogger: createLogger(),
emit: (message) => emitted.push(message),
registerPendingWorktreeWorkspace: vi.fn(async (options) => ({
workspaceId: options.worktreePath,
projectId: options.repoRoot,
})),
describeWorkspaceRecord: vi.fn(async (workspace) => ({
id: workspace.workspaceId,
projectId: workspace.projectId,
projectDisplayName: path.basename(repoDir),
projectRootPath: repoDir,
projectKind: "git",
workspaceKind: "worktree",
name: path.basename(workspace.workspaceId),
status: "done",
activityAt: null,
})),
createPaseoWorktreeInBackground: vi.fn(async () => {}),
},
{
type: "create_paseo_worktree_request",
cwd: repoDir,
worktreeSlug: "single-call",
requestId: "req-single-call",
},
);
expect(createAgentWorktreeSpy).toHaveBeenCalledTimes(1);
const response = emitted.find(
(message): message is Extract<SessionOutboundMessage, { type: "create_paseo_worktree_response" }> =>
message.type === "create_paseo_worktree_response",
);
expect(response?.payload.error).toBeNull();
} finally {
createAgentWorktreeSpy.mockRestore();
rmSync(tempDir, { recursive: true, force: true });
}
});
test("creates the worktree before emitting the response", async () => {
const { tempDir, repoDir } = createGitRepo();
const paseoHome = path.join(tempDir, ".paseo");
@@ -605,13 +605,6 @@ export async function handleCreatePaseoWorktreeRequest(
worktreePath: createdWorktree.worktree.worktreePath,
branchName: createdWorktree.worktree.branchName,
});
await createAgentWorktree({
cwd: repoRoot,
branchName: normalizedSlug,
baseBranch,
worktreeSlug: normalizedSlug,
paseoHome: dependencies.paseoHome,
});
const descriptor = await dependencies.describeWorkspaceRecord(workspace);
dependencies.emit({
type: "create_paseo_worktree_response",
@@ -0,0 +1,36 @@
import { describe, expect, it } from "vitest";
import { buildStringCommandShellInvocation } from "./string-command-shell.js";
describe("buildStringCommandShellInvocation", () => {
it("uses bash login-command semantics on unix platforms", () => {
expect(
buildStringCommandShellInvocation({
command: 'echo "hello"',
platform: "darwin",
}),
).toEqual({
shell: "/bin/bash",
args: ["-lc", 'echo "hello"'],
});
});
it("uses powershell command semantics on windows", () => {
expect(
buildStringCommandShellInvocation({
command: "Write-Output 'hello'",
platform: "win32",
}),
).toEqual({
shell: "powershell",
args: [
"-NoProfile",
"-NonInteractive",
"-ExecutionPolicy",
"Bypass",
"-Command",
"Write-Output 'hello'",
],
});
});
});
@@ -0,0 +1,34 @@
export interface BuildStringCommandShellInvocationOptions {
command: string;
platform?: NodeJS.Platform;
}
export interface StringCommandShellInvocation {
shell: string;
args: string[];
}
export function buildStringCommandShellInvocation(
options: BuildStringCommandShellInvocationOptions,
): StringCommandShellInvocation {
const platform = options.platform ?? process.platform;
if (platform === "win32") {
return {
shell: "powershell",
args: [
"-NoProfile",
"-NonInteractive",
"-ExecutionPolicy",
"Bypass",
"-Command",
options.command,
],
};
}
return {
shell: "/bin/bash",
args: ["-lc", options.command],
};
}
@@ -0,0 +1,81 @@
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
const execFileMock = vi.hoisted(() => vi.fn());
vi.mock("child_process", async () => {
const actual = await vi.importActual<typeof import("child_process")>("child_process");
return {
...actual,
execFile: execFileMock,
};
});
describe("worktree shell selection", () => {
const originalPlatform = process.platform;
beforeEach(() => {
execFileMock.mockReset();
execFileMock.mockImplementation(
(_file: string, _args: string[], _options: unknown, callback?: (error: Error | null, stdout: string, stderr: string) => void) => {
callback?.(null, "", "");
return {};
},
);
});
afterEach(() => {
Object.defineProperty(process, "platform", {
value: originalPlatform,
configurable: true,
});
vi.resetModules();
});
it("routes teardown command execution through powershell on win32", async () => {
Object.defineProperty(process, "platform", {
value: "win32",
configurable: true,
});
const worktreePath = mkdtempSync(join(tmpdir(), "worktree-shell-selection-"));
try {
mkdirSync(join(worktreePath, ".git"), { recursive: true });
writeFileSync(
join(worktreePath, "paseo.json"),
JSON.stringify({
worktree: {
teardown: ["Write-Output 'teardown'"],
},
}),
"utf8",
);
const { runWorktreeTeardownCommands } = await import("./worktree.js");
await runWorktreeTeardownCommands({
worktreePath,
repoRootPath: worktreePath,
branchName: "main",
});
expect(execFileMock).toHaveBeenCalledTimes(1);
expect(execFileMock).toHaveBeenCalledWith(
"powershell",
[
"-NoProfile",
"-NonInteractive",
"-ExecutionPolicy",
"Bypass",
"-Command",
"Write-Output 'teardown'",
],
expect.objectContaining({ cwd: worktreePath }),
expect.any(Function),
);
} finally {
rmSync(worktreePath, { recursive: true, force: true });
}
});
});
+9 -5
View File
@@ -1,4 +1,4 @@
import { exec, spawn } from "child_process";
import { exec, execFile, spawn } from "child_process";
import { promisify } from "util";
import { existsSync, mkdirSync, readFileSync, realpathSync, rmSync, statSync } from "fs";
import { join, basename, dirname, resolve, sep } from "path";
@@ -7,6 +7,7 @@ import { createHash } from "node:crypto";
import * as pty from "node-pty";
import { createNameId } from "mnemonic-id";
import stripAnsi from "strip-ansi";
import { buildStringCommandShellInvocation } from "./string-command-shell.js";
import {
normalizeBaseRefName,
readPaseoWorktreeMetadata,
@@ -27,6 +28,7 @@ interface PaseoConfig {
}
const execAsync = promisify(exec);
const execFileAsync = promisify(execFile);
const READ_ONLY_GIT_ENV: NodeJS.ProcessEnv = {
...process.env,
GIT_OPTIONAL_LOCKS: "0",
@@ -292,11 +294,11 @@ async function execSetupCommand(
options: { cwd: string; env: NodeJS.ProcessEnv },
): Promise<WorktreeSetupCommandResult> {
const startedAt = Date.now();
const shellInvocation = buildStringCommandShellInvocation({ command });
try {
const { stdout, stderr } = await execAsync(command, {
const { stdout, stderr } = await execFileAsync(shellInvocation.shell, shellInvocation.args, {
cwd: options.cwd,
env: options.env,
shell: "/bin/bash",
});
return {
command,
@@ -389,7 +391,8 @@ async function execSetupCommandStreamed(options: {
});
const spawnWithPipes = () => {
const child = spawn("/bin/bash", ["-lc", options.command], {
const shellInvocation = buildStringCommandShellInvocation({ command: options.command });
const child = spawn(shellInvocation.shell, shellInvocation.args, {
cwd: options.cwd,
env: options.env,
stdio: ["ignore", "pipe", "pipe"],
@@ -415,7 +418,8 @@ async function execSetupCommandStreamed(options: {
try {
ensureNodePtySpawnHelperExecutableForCurrentPlatform();
const terminal = pty.spawn("/bin/bash", ["-lc", options.command], {
const shellInvocation = buildStringCommandShellInvocation({ command: options.command });
const terminal = pty.spawn(shellInvocation.shell, shellInvocation.args, {
cwd: options.cwd,
env: options.env,
name: "xterm-color",