Skip to content

Commit 31dee69

Browse files
committed
fix(install): inject execPath for nvm detection in service env builder
1 parent 66143c0 commit 31dee69

File tree

2 files changed

+40
-11
lines changed

2 files changed

+40
-11
lines changed

src/daemon/service-env.test.ts

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -421,16 +421,18 @@ describe("buildNodeServiceEnvironment", () => {
421421
});
422422

423423
describe("shared Node TLS env defaults", () => {
424+
// Pass an explicit non-nvm execPath so tests are deterministic regardless of
425+
// whether the test runner itself runs under nvm.
424426
const builders = [
425427
{
426428
name: "gateway service env",
427429
build: (env: Record<string, string | undefined>, platform?: NodeJS.Platform) =>
428-
buildServiceEnvironment({ env, port: 18789, platform }),
430+
buildServiceEnvironment({ env, port: 18789, platform, execPath: "/usr/bin/node" }),
429431
},
430432
{
431433
name: "node service env",
432434
build: (env: Record<string, string | undefined>, platform?: NodeJS.Platform) =>
433-
buildNodeServiceEnvironment({ env, platform }),
435+
buildNodeServiceEnvironment({ env, platform, execPath: "/usr/bin/node" }),
434436
},
435437
] as const;
436438

@@ -499,16 +501,25 @@ describe("resolveLinuxSystemCaBundle", () => {
499501
});
500502

501503
describe("shared Node TLS env — Linux nvm detection", () => {
504+
const nvmExecPath = "/home/user/.nvm/versions/node/v22.22.0/bin/node";
505+
const nonNvmExecPath = "/usr/bin/node";
506+
502507
const builders = [
503508
{
504509
name: "gateway service env",
505-
build: (env: Record<string, string | undefined>, platform?: NodeJS.Platform) =>
506-
buildServiceEnvironment({ env, port: 18789, platform }),
510+
build: (
511+
env: Record<string, string | undefined>,
512+
platform?: NodeJS.Platform,
513+
execPath?: string,
514+
) => buildServiceEnvironment({ env, port: 18789, platform, execPath }),
507515
},
508516
{
509517
name: "node service env",
510-
build: (env: Record<string, string | undefined>, platform?: NodeJS.Platform) =>
511-
buildNodeServiceEnvironment({ env, platform }),
518+
build: (
519+
env: Record<string, string | undefined>,
520+
platform?: NodeJS.Platform,
521+
execPath?: string,
522+
) => buildNodeServiceEnvironment({ env, platform, execPath }),
512523
},
513524
] as const;
514525

@@ -518,15 +529,27 @@ describe("shared Node TLS env — Linux nvm detection", () => {
518529
it.each(builders)(
519530
"$name defaults NODE_EXTRA_CA_CERTS on Linux when NVM_DIR is set",
520531
({ build }) => {
521-
const env = build({ HOME: "/home/user", NVM_DIR: "/home/user/.nvm" }, "linux");
532+
const env = build(
533+
{ HOME: "/home/user", NVM_DIR: "/home/user/.nvm" },
534+
"linux",
535+
nonNvmExecPath,
536+
);
537+
expect(env.NODE_EXTRA_CA_CERTS).toBe(expectedCaBundle);
538+
},
539+
);
540+
541+
it.each(builders)(
542+
"$name defaults NODE_EXTRA_CA_CERTS on Linux when execPath is under nvm",
543+
({ build }) => {
544+
const env = build({ HOME: "/home/user" }, "linux", nvmExecPath);
522545
expect(env.NODE_EXTRA_CA_CERTS).toBe(expectedCaBundle);
523546
},
524547
);
525548

526549
it.each(builders)(
527550
"$name does not default NODE_EXTRA_CA_CERTS on Linux without nvm",
528551
({ build }) => {
529-
const env = build({ HOME: "/home/user" }, "linux");
552+
const env = build({ HOME: "/home/user" }, "linux", nonNvmExecPath);
530553
expect(env.NODE_EXTRA_CA_CERTS).toBeUndefined();
531554
},
532555
);
@@ -541,6 +564,7 @@ describe("shared Node TLS env — Linux nvm detection", () => {
541564
NODE_EXTRA_CA_CERTS: "/custom/ca-bundle.crt",
542565
},
543566
"linux",
567+
nvmExecPath,
544568
);
545569
expect(env.NODE_EXTRA_CA_CERTS).toBe("/custom/ca-bundle.crt");
546570
},

src/daemon/service-env.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -290,10 +290,12 @@ export function buildServiceEnvironment(params: {
290290
port: number;
291291
launchdLabel?: string;
292292
platform?: NodeJS.Platform;
293+
/** Override process.execPath for nvm detection (testing). */
294+
execPath?: string;
293295
}): Record<string, string | undefined> {
294296
const { env, port, launchdLabel } = params;
295297
const platform = params.platform ?? process.platform;
296-
const sharedEnv = resolveSharedServiceEnvironmentFields(env, platform);
298+
const sharedEnv = resolveSharedServiceEnvironmentFields(env, platform, params.execPath);
297299
const profile = env.OPENCLAW_PROFILE;
298300
const resolvedLaunchdLabel =
299301
launchdLabel || (platform === "darwin" ? resolveGatewayLaunchAgentLabel(profile) : undefined);
@@ -314,10 +316,12 @@ export function buildServiceEnvironment(params: {
314316
export function buildNodeServiceEnvironment(params: {
315317
env: Record<string, string | undefined>;
316318
platform?: NodeJS.Platform;
319+
/** Override process.execPath for nvm detection (testing). */
320+
execPath?: string;
317321
}): Record<string, string | undefined> {
318322
const { env } = params;
319323
const platform = params.platform ?? process.platform;
320-
const sharedEnv = resolveSharedServiceEnvironmentFields(env, platform);
324+
const sharedEnv = resolveSharedServiceEnvironmentFields(env, platform, params.execPath);
321325
const gatewayToken =
322326
env.OPENCLAW_GATEWAY_TOKEN?.trim() || env.CLAWDBOT_GATEWAY_TOKEN?.trim() || undefined;
323327
return {
@@ -356,6 +360,7 @@ function buildCommonServiceEnvironment(
356360
function resolveSharedServiceEnvironmentFields(
357361
env: Record<string, string | undefined>,
358362
platform: NodeJS.Platform,
363+
execPath?: string,
359364
): SharedServiceEnvironmentFields {
360365
const stateDir = env.OPENCLAW_STATE_DIR;
361366
const configPath = env.OPENCLAW_CONFIG_PATH;
@@ -371,7 +376,7 @@ function resolveSharedServiceEnvironmentFields(
371376
env.NODE_EXTRA_CA_CERTS ??
372377
(platform === "darwin"
373378
? "/etc/ssl/cert.pem"
374-
: platform === "linux" && isNvmNode(env)
379+
: platform === "linux" && isNvmNode(env, execPath ?? process.execPath)
375380
? resolveLinuxSystemCaBundle()
376381
: undefined);
377382
const nodeUseSystemCa = env.NODE_USE_SYSTEM_CA ?? (platform === "darwin" ? "1" : undefined);

0 commit comments

Comments
 (0)