Skip to content

Commit cf91d4c

Browse files
authored
Merge pull request #1806 from ntdatt812/fix/posix-service-sqlite-home
fix(service): keep an absolute POSIX sqlite home literal in POSIX service files
2 parents 16bffe2 + 8c6fa81 commit cf91d4c

2 files changed

Lines changed: 70 additions & 8 deletions

File tree

‎src/service.ts‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { execFileSync, execSync, spawnSync } from "node:child_process";
99
import { findLiveProxy, proxyIdentityAt, SERVICE_STOP_LIVENESS } from "./server/proxy-liveness";
1010
import { chmodSync, existsSync, mkdirSync, mkdtempSync, readFileSync, rmdirSync, unlinkSync, writeFileSync } from "node:fs";
1111
import { homedir, tmpdir } from "node:os";
12-
import { dirname, join, resolve, win32 } from "node:path";
12+
import { dirname, join, posix, resolve, win32 } from "node:path";
1313
import { expandUserPath, getConfigDir, readPid, removePid, removeRuntimePort, verifyPidIdentity } from "./config";
1414
import { loadConfig } from "./config";
1515
import { restoreNativeCodex, restoreNativeCodexAsync } from "./codex/inject";
@@ -113,12 +113,19 @@ function currentCodexSqliteHomeAbsolute(target: "native" | "windows" = "native")
113113
const raw = process.env.CODEX_SQLITE_HOME?.trim();
114114
if (!raw) return undefined;
115115
const expanded = expandUserPath(raw);
116-
// Windows service artifacts can be rendered by cross-platform tests and
117-
// repair tooling. Preserve an already-absolute drive/UNC path instead of
118-
// anchoring it beneath the current POSIX worktree.
119-
return target === "windows" && win32.isAbsolute(expanded)
120-
? win32.normalize(expanded)
121-
: resolve(expanded);
116+
// Service artifacts can be rendered by cross-platform tests and repair tooling, so an
117+
// already-absolute path for the TARGET platform is preserved rather than re-anchored
118+
// against the writing host. `resolve()` is host-relative in both directions: on a POSIX
119+
// host it turns `C:\data` into `<cwd>/C:\data`, and on a Windows host it turns `/tmp/x`
120+
// into `D:\tmp\x` — neither is a path the target can use. A relative value still resolves,
121+
// because a service unit has no meaningful working directory.
122+
//
123+
// CODEX_HOME and OPENCODEX_HOME are carried through literally, so without this the same
124+
// generated file disagreed with itself about two variables holding the same kind of value.
125+
if (target === "windows") {
126+
return win32.isAbsolute(expanded) ? win32.normalize(expanded) : resolve(expanded);
127+
}
128+
return posix.isAbsolute(expanded) ? posix.normalize(expanded) : resolve(expanded);
122129
}
123130

124131
function currentOpenCodexHome(): string {

‎tests/service.test.ts‎

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { afterEach, describe, expect, spyOn, test } from "bun:test";
22
import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
33
import { tmpdir } from "node:os";
4-
import { join } from "node:path";
4+
import { isAbsolute, join, posix, win32 } from "node:path";
55
import * as serviceModule from "../src/service";
66
import { saveConfig } from "../src/config";
77
import { windowsEnvIndirectBatchValue } from "../src/lib/win-paths";
@@ -663,6 +663,61 @@ describe("launchd service plist", () => {
663663
else process.env.OPENCODEX_API_AUTH_TOKEN = oldApiAuthToken;
664664
}
665665
});
666+
667+
// A POSIX unit must carry the literal POSIX path no matter which host writes it. The two
668+
// cases above are where this actually bites: on a Windows host `resolve("/tmp/x")` anchors
669+
// to the current drive and the generated file said `D:\tmp\codex-sqlite-home`, while
670+
// CODEX_HOME beside it kept `/tmp/codex-home`. The same file disagreed with itself about two
671+
// variables holding the same kind of value. This states the rule directly so the intent
672+
// survives; on a POSIX host `resolve()` is identity here, so only Windows can catch it.
673+
test("carries an absolute POSIX sqlite home into POSIX units without host anchoring", () => {
674+
const inherited = process.env.CODEX_SQLITE_HOME;
675+
try {
676+
process.env.CODEX_SQLITE_HOME = "/var/lib/opencodex/codex-sqlite";
677+
678+
expect(buildPlist()).toContain(
679+
"<key>CODEX_SQLITE_HOME</key><string>/var/lib/opencodex/codex-sqlite</string>",
680+
);
681+
expect(buildUnit()).toContain(
682+
'Environment="CODEX_SQLITE_HOME=/var/lib/opencodex/codex-sqlite"',
683+
);
684+
} finally {
685+
if (inherited === undefined) delete process.env.CODEX_SQLITE_HOME;
686+
else process.env.CODEX_SQLITE_HOME = inherited;
687+
}
688+
});
689+
690+
// The relative case is why the resolve() is there at all: a service unit has no meaningful
691+
// working directory, so a relative home must still be made absolute.
692+
test("still absolutizes a relative sqlite home", () => {
693+
const inherited = process.env.CODEX_SQLITE_HOME;
694+
try {
695+
process.env.CODEX_SQLITE_HOME = "relative-sqlite-home";
696+
const plist = buildPlist();
697+
698+
// Assert the emitted value is actually absolute. Rejecting only the raw string would
699+
// stay green for any other non-absolute transform, which is the whole thing this test
700+
// exists to catch. The two artifact formats differ, so each is extracted on its own
701+
// terms: launchd is XML, systemd is a quoted Environment= line.
702+
const plistValue = /<key>CODEX_SQLITE_HOME<\/key>\s*<string>([^<]*)<\/string>/.exec(plist)?.[1];
703+
expect(plistValue).toBeDefined();
704+
expect(
705+
isAbsolute(plistValue!) || posix.isAbsolute(plistValue!) || win32.isAbsolute(plistValue!),
706+
).toBe(true);
707+
expect(plistValue!.endsWith("relative-sqlite-home")).toBe(true);
708+
709+
const unit = buildUnit();
710+
const unitValue = /Environment="CODEX_SQLITE_HOME=([^"]*)"/.exec(unit)?.[1];
711+
expect(unitValue).toBeDefined();
712+
expect(
713+
isAbsolute(unitValue!) || posix.isAbsolute(unitValue!) || win32.isAbsolute(unitValue!),
714+
).toBe(true);
715+
expect(unitValue!.endsWith("relative-sqlite-home")).toBe(true);
716+
} finally {
717+
if (inherited === undefined) delete process.env.CODEX_SQLITE_HOME;
718+
else process.env.CODEX_SQLITE_HOME = inherited;
719+
}
720+
});
666721
});
667722

668723
describe("service lifecycle cleanup ordering", () => {

0 commit comments

Comments
 (0)