Expand leading ~ in Codex home paths before exporting CODEX_HOME (#2210)

Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
Co-authored-by: = <=>
This commit is contained in:
authored and GitHub committed 2026-04-20 00:06:19 +00:00
1 parent c83bc5d48a
commit 20f346d8ef
6 files changed
+67 -5

No files matched your search

+3 -2
View File
@@ -32,6 +32,7 @@ import {
type CodexAccountSnapshot,
} from "./provider/codexAccount.ts";
import { buildCodexInitializeParams, killCodexChildProcess } from "./provider/codexAppServer.ts";
import { expandHomePath } from "./pathExpansion.ts";
export { buildCodexInitializeParams } from "./provider/codexAppServer.ts";
export { readCodexAccountSnapshot, resolveCodexModelForAccount } from "./provider/codexAccount.ts";
@@ -493,7 +494,7 @@ export class CodexAppServerManager extends EventEmitter<CodexAppServerManagerEve
cwd: resolvedCwd,
env: {
...process.env,
...(codexHomePath ? { CODEX_HOME: codexHomePath } : {}),
...(codexHomePath ? { CODEX_HOME: expandHomePath(codexHomePath) } : {}),
},
stdio: ["pipe", "pipe", "pipe"],
shell: process.platform === "win32",
@@ -1571,7 +1572,7 @@ function assertSupportedCodexCliVersion(input: {
cwd: input.cwd,
env: {
...process.env,
...(input.homePath ? { CODEX_HOME: input.homePath } : {}),
...(input.homePath ? { CODEX_HOME: expandHomePath(input.homePath) } : {}),
},
encoding: "utf8",
shell: process.platform === "win32",
@@ -8,6 +8,7 @@ import { sanitizeBranchFragment, sanitizeFeatureBranchName } from "@t3tools/shar
import { resolveAttachmentPath } from "../../attachmentStore.ts";
import { ServerConfig } from "../../config.ts";
import { expandHomePath } from "../../pathExpansion.ts";
import { TextGenerationError } from "@t3tools/contracts";
import {
type BranchNameGenerationInput,
@@ -184,7 +185,9 @@ const makeCodexTextGeneration = Effect.gen(function* () {
{
env: {
...process.env,
...(codexSettings?.homePath ? { CODEX_HOME: codexSettings.homePath } : {}),
...(codexSettings?.homePath
? { CODEX_HOME: expandHomePath(codexSettings.homePath) }
: {}),
},
cwd,
shell: process.platform === "win32",
+33
View File
@@ -0,0 +1,33 @@
import { homedir } from "node:os";
import { join } from "node:path";
import { describe, expect, it } from "vitest";
import { expandHomePath } from "./pathExpansion.ts";
describe("expandHomePath", () => {
it("returns an empty string unchanged", () => {
expect(expandHomePath("")).toBe("");
});
it("returns paths without a leading tilde unchanged", () => {
expect(expandHomePath("/absolute/path")).toBe("/absolute/path");
expect(expandHomePath("relative/path")).toBe("relative/path");
expect(expandHomePath("some~weird~path")).toBe("some~weird~path");
});
it("expands a lone tilde to the home directory", () => {
expect(expandHomePath("~")).toBe(homedir());
});
it("expands ~/ to a subpath of the home directory", () => {
expect(expandHomePath("~/.codex-work")).toBe(join(homedir(), ".codex-work"));
});
it("expands a Windows-style ~\\ prefix", () => {
expect(expandHomePath("~\\.codex")).toBe(join(homedir(), ".codex"));
});
it("does not expand ~user paths", () => {
expect(expandHomePath("~alice/foo")).toBe("~alice/foo");
});
});
+23
View File
@@ -0,0 +1,23 @@
import { homedir } from "node:os";
import { join } from "node:path";
/**
* Expand a leading `~` (or `~/…`, `~\…`) in a user-supplied path to the
* current user's home directory. Spawned processes don't get shell
* expansion, so env vars like `CODEX_HOME=~/.codex-work` would be passed
* verbatim and treated as relative paths by the receiver.
*
* Matches the behavior of the other `expandHomePath` helpers in the
* workspace layers and CLI bootstrap: `~` alone and both `~/` and `~\`
* separators are handled. Returns the input unchanged if it doesn't
* start with `~` or is empty. Does not handle `~user` (other-user)
* expansion.
*/
export function expandHomePath(value: string): string {
if (!value) return value;
if (value === "~") return homedir();
if (value.startsWith("~/") || value.startsWith("~\\")) {
return join(homedir(), value.slice(2));
}
return value;
}
@@ -47,6 +47,7 @@ import {
} from "../codexAccount.ts";
import { probeCodexDiscovery } from "../codexAppServer.ts";
import { CodexProvider } from "../Services/CodexProvider.ts";
import { expandHomePath } from "../../pathExpansion.ts";
import { ServerSettingsService } from "../../serverSettings.ts";
import { ServerSettingsError } from "@t3tools/contracts";
@@ -325,7 +326,7 @@ const runCodexCommand = Effect.fn("runCodexCommand")(function* (args: ReadonlyAr
shell: process.platform === "win32",
env: {
...process.env,
...(codexSettings.homePath ? { CODEX_HOME: codexSettings.homePath } : {}),
...(codexSettings.homePath ? { CODEX_HOME: expandHomePath(codexSettings.homePath) } : {}),
},
});
return yield* spawnAndCollect(codexSettings.binaryPath, command);
+2 -1
View File
@@ -2,6 +2,7 @@ import { spawn, spawnSync, type ChildProcessWithoutNullStreams } from "node:chil
import readline from "node:readline";
import type { ServerProviderSkill } from "@t3tools/contracts";
import { readCodexAccountSnapshot, type CodexAccountSnapshot } from "./codexAccount.ts";
import { expandHomePath } from "../pathExpansion.ts";
interface JsonRpcProbeResponse {
readonly id?: unknown;
@@ -115,7 +116,7 @@ export async function probeCodexDiscovery(input: {
const child = spawn(input.binaryPath, ["app-server"], {
env: {
...process.env,
...(input.homePath ? { CODEX_HOME: input.homePath } : {}),
...(input.homePath ? { CODEX_HOME: expandHomePath(input.homePath) } : {}),
},
stdio: ["pipe", "pipe", "pipe"],
shell: process.platform === "win32",