Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions sdk/typescript/src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3399,12 +3399,13 @@ export async function resolvePluginPython(
);
}

const home = options.homeDirectory ?? homedir();
const cacheDirectory =
environmentValue(environment, "XDG_CACHE_HOME", true) ||
join(home, ".cache");
const managedRoots = options.managedRuntimeRoots ?? [
join(cacheDirectory, "codex-runtimes", "codex-primary-runtime"),
join(
environmentValue(environment, "XDG_CACHE_HOME", true) ||
join(options.homeDirectory ?? homedir(), ".cache"),
"codex-runtimes",
"codex-primary-runtime",
),
];
const relativeCandidates =
process.platform === "win32"
Expand Down Expand Up @@ -3872,16 +3873,16 @@ export function expandHome(
value: string,
environment: ProcessEnvironment = process.env,
): string {
const home =
const home = () =>
(process.platform === "win32"
? (environmentValue(environment, "USERPROFILE") ??
environmentValue(environment, "HOME"))
: (environmentValue(environment, "HOME") ??
environmentValue(environment, "USERPROFILE"))) ?? homedir();
if (value === "~") return home;
if (value.startsWith("~/")) return join(home, value.slice(2));
if (value === "~") return home();
if (value.startsWith("~/")) return join(home(), value.slice(2));
if (value.startsWith("~\\")) {
return join(home, ...value.slice(2).split("\\"));
return join(home(), ...value.slice(2).split("\\"));
}
return value;
}
Expand Down
26 changes: 26 additions & 0 deletions sdk/typescript/tests-ts/api-runtime-storage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,32 @@ import { PLUGIN_ROOT } from "./plugin-root.js";
const { temporaryDirectory, cleanup } = createApiTestFixtures();
afterEach(cleanup);

test("preflights with explicit storage paths when the account has no home directory", async () => {
const root = await temporaryDirectory();
const repository = join(root, "repository");
await mkdir(repository);
await writeFile(join(repository, "source.py"), "value = 1\n");
const client = new TestClient(
{},
{
environment: {
CODEX_HOME: join(root, "codex"),
CODEX_SECURITY_STATE_DIR: join(root, "state"),
},
},
);
const home = spyOn(os, "homedir").mockImplementation(() => {
throw new Error("No home directory for this account");
});
try {
const scan = await client.preflight(repository, { target: ["source.py"] });
expect(scan.target.paths).toEqual(["source.py"]);
} finally {
home.mockRestore();
await client.close();
}
});

function stubPluginInstall() {
return spyOn(runtime, "bootstrapPlugin").mockImplementation(
async (_home, pluginRoot) => ({
Expand Down
6 changes: 5 additions & 1 deletion sdk/typescript/tests-ts/runtime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6861,6 +6861,11 @@ describe("runtime directories and plugin Python boundary", () => {

testPosix("uses configured, inherited, and managed Python", async () => {
const root = await temporaryDirectory();
using _home = spyOn(await import("node:os"), "homedir").mockImplementation(
() => {
throw new Error("No home directory for this account");
},
);
const configured = join(root, "configured-python");
await writeFile(
configured,
Expand Down Expand Up @@ -6914,7 +6919,6 @@ describe("runtime directories and plugin Python boundary", () => {
expect(
await resolvePluginPython({
environment: { PATH: "", XDG_CACHE_HOME: cacheDirectory },
homeDirectory: join(root, "unused-home"),
}),
).toBe(cachedPython);
}
Expand Down
Loading