Files
oh-my-pi/packages/coding-agent/src/cli/startup-cwd.ts
T
can1357 5be2600b92 fix(coding-agent): normalized relative --cwd flag before downstream use
- Updated startup handling so `applyStartupCwd` now re-syncs `parsed.cwd` to the resolved absolute project directory after `setProjectDir` runs.
- Adjusted the CLI cwd tests to verify a relative `--cwd` argument is normalized to an absolute path and does not double-resolve against the new process cwd.
2026-06-08 00:58:22 +02:00

69 lines
1.7 KiB
TypeScript

import * as fs from "node:fs/promises";
import * as os from "node:os";
import * as path from "node:path";
import { getProjectDir, normalizePathForComparison, setProjectDir } from "@oh-my-pi/pi-utils";
import type { Args } from "./args";
async function maybeAutoChdir(parsed: Args): Promise<void> {
if (parsed.allowHome || parsed.cwd) {
return;
}
const home = os.homedir();
if (!home) {
return;
}
const normalizePath = normalizePathForComparison;
const cwd = normalizePath(getProjectDir());
const normalizedHome = normalizePath(home);
if (cwd !== normalizedHome) {
return;
}
const isDirectory = async (p: string) => {
try {
const s = await fs.stat(p);
return s.isDirectory();
} catch {
return false;
}
};
const candidates = [path.join(home, "tmp"), "/tmp", "/var/tmp"];
for (const candidate of candidates) {
try {
if (!(await isDirectory(candidate))) {
continue;
}
setProjectDir(candidate);
return;
} catch {
// Try next candidate.
}
}
try {
const fallback = os.tmpdir();
if (fallback && normalizePath(fallback) !== cwd && (await isDirectory(fallback))) {
setProjectDir(fallback);
}
} catch {
// Ignore fallback errors.
}
}
export async function applyStartupCwd(parsed: Args): Promise<void> {
if (parsed.cwd) {
setProjectDir(parsed.cwd);
// setProjectDir resolves the (possibly relative) target against the launch
// cwd and chdirs into it. Re-sync parsed.cwd to the resolved absolute path
// so downstream consumers (buildSessionOptions, settings/discovery, session
// persistence) don't re-resolve a relative string against the new cwd.
parsed.cwd = getProjectDir();
return;
}
await maybeAutoChdir(parsed);
}