fix(daemon): avoid freezing Windows PATH in task scripts (#39139, thanks @Narcooo)

Co-authored-by: majx_mac <mjxnarco@pku.edu.cn>
This commit is contained in:
Peter Steinberger
2026-03-07 21:15:01 +00:00
parent f51cac277c
commit b9dd6e99b6
7 changed files with 74 additions and 9 deletions

View File

@@ -133,4 +133,22 @@ describe("installScheduledTask", () => {
).rejects.toThrow(/Task description cannot contain CR or LF/);
});
});
it("does not persist a frozen PATH snapshot into the generated task script", async () => {
await withUserProfileDir(async (_tmpDir, env) => {
const { scriptPath } = await installScheduledTask({
env,
stdout: new PassThrough(),
programArguments: ["node", "gateway.js"],
environment: {
PATH: "C:\\Windows\\System32;C:\\Program Files\\Docker\\Docker\\resources\\bin",
OPENCLAW_GATEWAY_PORT: "18789",
},
});
const script = await fs.readFile(scriptPath, "utf8");
expect(script).not.toContain('set "PATH=');
expect(script).toContain('set "OPENCLAW_GATEWAY_PORT=18789"');
});
});
});

View File

@@ -209,6 +209,9 @@ function buildTaskScript({
if (!value) {
continue;
}
if (key.toUpperCase() === "PATH") {
continue;
}
lines.push(renderCmdSetAssignment(key, value));
}
}

View File

@@ -268,7 +268,7 @@ describe("buildServiceEnvironment", () => {
});
expect(env.HOME).toBe("/home/user");
if (process.platform === "win32") {
expect(env.PATH).toBe("");
expect(env).not.toHaveProperty("PATH");
} else {
expect(env.PATH).toContain("/usr/bin");
}
@@ -331,6 +331,20 @@ describe("buildServiceEnvironment", () => {
expect(env.http_proxy).toBe("http://proxy.local:7890");
expect(env.all_proxy).toBe("socks5://proxy.local:1080");
});
it("omits PATH on Windows so Scheduled Tasks can inherit the current shell path", () => {
const env = buildServiceEnvironment({
env: {
HOME: "C:\\Users\\alice",
PATH: "C:\\Windows\\System32;C:\\Tools\\rg",
},
port: 18789,
platform: "win32",
});
expect(env).not.toHaveProperty("PATH");
expect(env.OPENCLAW_WINDOWS_TASK_NAME).toBe("OpenClaw Gateway");
});
});
describe("buildNodeServiceEnvironment", () => {

View File

@@ -30,7 +30,7 @@ type SharedServiceEnvironmentFields = {
stateDir: string | undefined;
configPath: string | undefined;
tmpDir: string;
minimalPath: string;
minimalPath: string | undefined;
proxyEnv: Record<string, string | undefined>;
nodeCaCerts: string | undefined;
nodeUseSystemCa: string | undefined;
@@ -297,16 +297,19 @@ function buildCommonServiceEnvironment(
env: Record<string, string | undefined>,
sharedEnv: SharedServiceEnvironmentFields,
): Record<string, string | undefined> {
return {
const serviceEnv: Record<string, string | undefined> = {
HOME: env.HOME,
TMPDIR: sharedEnv.tmpDir,
PATH: sharedEnv.minimalPath,
...sharedEnv.proxyEnv,
NODE_EXTRA_CA_CERTS: sharedEnv.nodeCaCerts,
NODE_USE_SYSTEM_CA: sharedEnv.nodeUseSystemCa,
OPENCLAW_STATE_DIR: sharedEnv.stateDir,
OPENCLAW_CONFIG_PATH: sharedEnv.configPath,
};
if (sharedEnv.minimalPath) {
serviceEnv.PATH = sharedEnv.minimalPath;
}
return serviceEnv;
}
function resolveSharedServiceEnvironmentFields(
@@ -328,7 +331,9 @@ function resolveSharedServiceEnvironmentFields(
stateDir,
configPath,
tmpDir,
minimalPath: buildMinimalServicePath({ env }),
// On Windows, Scheduled Tasks should inherit the current task PATH instead of
// freezing the install-time snapshot into gateway.cmd/node-host.cmd.
minimalPath: platform === "win32" ? undefined : buildMinimalServicePath({ env, platform }),
proxyEnv,
nodeCaCerts,
nodeUseSystemCa,