Skip to content

Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.

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

Alton Johnson, OSCP, OSCE20f346d8ef58
diff --git a/apps/server/src/codexAppServerManager.ts b/apps/server/src/codexAppServerManager.ts
index 6d98264c9..ba776890e 100644
--- a/apps/server/src/codexAppServerManager.ts
+++ b/apps/server/src/codexAppServerManager.ts
@@ -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",
diff --git a/apps/server/src/git/Layers/CodexTextGeneration.ts b/apps/server/src/git/Layers/CodexTextGeneration.ts
index be1c6798c..b629d91c1 100644
--- a/apps/server/src/git/Layers/CodexTextGeneration.ts
+++ b/apps/server/src/git/Layers/CodexTextGeneration.ts
@@ -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",
diff --git a/apps/server/src/pathExpansion.test.ts b/apps/server/src/pathExpansion.test.ts
new file mode 100644
index 000000000..40ca25e6c
--- /dev/null
+++ b/apps/server/src/pathExpansion.test.ts
@@ -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");
+ });
+});
diff --git a/apps/server/src/pathExpansion.ts b/apps/server/src/pathExpansion.ts
new file mode 100644
index 000000000..18060c3e5
--- /dev/null
+++ b/apps/server/src/pathExpansion.ts
@@ -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;
+}
diff --git a/apps/server/src/provider/Layers/CodexProvider.ts b/apps/server/src/provider/Layers/CodexProvider.ts
index de4aceeac..ab2f962ea 100644
--- a/apps/server/src/provider/Layers/CodexProvider.ts
+++ b/apps/server/src/provider/Layers/CodexProvider.ts
@@ -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);
diff --git a/apps/server/src/provider/codexAppServer.ts b/apps/server/src/provider/codexAppServer.ts
index 24a9e29c5..7806e135f 100644
--- a/apps/server/src/provider/codexAppServer.ts
+++ b/apps/server/src/provider/codexAppServer.ts
@@ -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",