diff --git a/src/daemon/service-env.test.ts b/src/daemon/service-env.test.ts index be226a4f32..31a46c4990 100644 --- a/src/daemon/service-env.test.ts +++ b/src/daemon/service-env.test.ts @@ -1,5 +1,6 @@ +import os from "node:os"; import path from "node:path"; -import { afterEach, describe, expect, it } from "vitest"; +import { describe, expect, it } from "vitest"; import { resolveGatewayStateDir } from "./paths.js"; import { buildMinimalServicePath, @@ -9,16 +10,6 @@ import { getMinimalServicePathPartsFromEnv, } from "./service-env.js"; -const originalPlatform = process.platform; - -function setPlatform(platform: NodeJS.Platform): void { - Object.defineProperty(process, "platform", { value: platform, configurable: true }); -} - -afterEach(() => { - Object.defineProperty(process, "platform", { value: originalPlatform, configurable: true }); -}); - describe("getMinimalServicePathParts - Linux user directories", () => { it("includes user bin directories when HOME is set on Linux", () => { const result = getMinimalServicePathParts({ @@ -292,8 +283,7 @@ describe("buildServiceEnvironment", () => { } }); - it("forwards TMPDIR from the host environment on macOS", () => { - setPlatform("darwin"); + it("forwards TMPDIR from the host environment", () => { const env = buildServiceEnvironment({ env: { HOME: "/home/user", TMPDIR: "/var/folders/xw/abc123/T/" }, port: 18789, @@ -301,21 +291,12 @@ describe("buildServiceEnvironment", () => { expect(env.TMPDIR).toBe("/var/folders/xw/abc123/T/"); }); - it("does not forward TMPDIR on non-macOS services", () => { - setPlatform("linux"); - const env = buildServiceEnvironment({ - env: { HOME: "/home/user", TMPDIR: "/tmp/custom" }, - port: 18789, - }); - expect(env.TMPDIR).toBeUndefined(); - }); - - it("omits TMPDIR when not set in host environment", () => { + it("falls back to os.tmpdir when TMPDIR is not set", () => { const env = buildServiceEnvironment({ env: { HOME: "/home/user" }, port: 18789, }); - expect(env.TMPDIR).toBeUndefined(); + expect(env.TMPDIR).toBe(os.tmpdir()); }); it("uses profile-specific unit and label", () => { @@ -338,20 +319,18 @@ describe("buildNodeServiceEnvironment", () => { expect(env.HOME).toBe("/home/user"); }); - it("forwards TMPDIR for node services on macOS", () => { - setPlatform("darwin"); + it("forwards TMPDIR for node services", () => { const env = buildNodeServiceEnvironment({ env: { HOME: "/home/user", TMPDIR: "/tmp/custom" }, }); expect(env.TMPDIR).toBe("/tmp/custom"); }); - it("does not forward TMPDIR for node services on non-macOS platforms", () => { - setPlatform("linux"); + it("falls back to os.tmpdir for node services when TMPDIR is not set", () => { const env = buildNodeServiceEnvironment({ - env: { HOME: "/home/user", TMPDIR: "/tmp/custom" }, + env: { HOME: "/home/user" }, }); - expect(env.TMPDIR).toBeUndefined(); + expect(env.TMPDIR).toBe(os.tmpdir()); }); }); diff --git a/src/daemon/service-env.ts b/src/daemon/service-env.ts index b9590841c2..10fd4223c8 100644 --- a/src/daemon/service-env.ts +++ b/src/daemon/service-env.ts @@ -1,3 +1,4 @@ +import os from "node:os"; import path from "node:path"; import { VERSION } from "../version.js"; import { @@ -212,8 +213,8 @@ export function buildServiceEnvironment(params: { const systemdUnit = `${resolveGatewaySystemdServiceName(profile)}.service`; const stateDir = env.OPENCLAW_STATE_DIR; const configPath = env.OPENCLAW_CONFIG_PATH; - // launchd on macOS does not inherit shell TMPDIR by default; forward it explicitly there. - const tmpDir = process.platform === "darwin" ? env.TMPDIR : undefined; + // Keep a usable temp directory for supervised services even when the host env omits TMPDIR. + const tmpDir = env.TMPDIR?.trim() || os.tmpdir(); return { HOME: env.HOME, TMPDIR: tmpDir, @@ -237,8 +238,7 @@ export function buildNodeServiceEnvironment(params: { const { env } = params; const stateDir = env.OPENCLAW_STATE_DIR; const configPath = env.OPENCLAW_CONFIG_PATH; - // Keep TMPDIR propagation scoped to macOS launchd installs. - const tmpDir = process.platform === "darwin" ? env.TMPDIR : undefined; + const tmpDir = env.TMPDIR?.trim() || os.tmpdir(); return { HOME: env.HOME, TMPDIR: tmpDir,