Skip to content

Commit f6d5e2c

Browse files
committed
fix(systemd): parse all inline environment assignments
1 parent ba2827f commit f6d5e2c

3 files changed

Lines changed: 35 additions & 4 deletions

File tree

‎src/daemon/systemd-unit.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ export function parseSystemdExecStart(value: string): string[] {
104104
return splitArgsPreservingQuotes(value, { escapeMode: "backslash" });
105105
}
106106

107-
export function parseSystemdEnvAssignment(raw: string): { key: string; value: string } | null {
107+
function parseSystemdEnvAssignment(raw: string): { key: string; value: string } | null {
108108
const trimmed = raw.trim();
109109
if (!trimmed) {
110110
return null;

‎src/daemon/systemd.test.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1265,6 +1265,39 @@ describe("readSystemdServiceExecStart", () => {
12651265
expect(command?.environmentValueSources?.OPENCLAW_GATEWAY_TOKEN).toBe("inline-and-file");
12661266
});
12671267

1268+
it("reads all inline assignments in order before EnvironmentFile overrides", async () => {
1269+
mockReadGatewayServiceFile(
1270+
[
1271+
"[Service]",
1272+
"ExecStart=/usr/bin/openclaw gateway run",
1273+
'Environment=PLAIN=first "QUOTED=value with spaces" REPEATED=first',
1274+
"Environment='REPEATED=last value' INLINE_FILE=inline",
1275+
"EnvironmentFile=%h/.openclaw/.env",
1276+
],
1277+
{
1278+
[`${TEST_SERVICE_HOME}/.openclaw/.env`]: ["INLINE_FILE=file", "FILE_ONLY=from-file"].join(
1279+
"\n",
1280+
),
1281+
},
1282+
);
1283+
1284+
const command = await readSystemdServiceExecStart({ HOME: TEST_SERVICE_HOME });
1285+
expect(command?.environment).toEqual({
1286+
PLAIN: "first",
1287+
QUOTED: "value with spaces",
1288+
REPEATED: "last value",
1289+
INLINE_FILE: "file",
1290+
FILE_ONLY: "from-file",
1291+
});
1292+
expect(command?.environmentValueSources).toEqual({
1293+
PLAIN: "inline",
1294+
QUOTED: "inline",
1295+
REPEATED: "inline",
1296+
INLINE_FILE: "inline-and-file",
1297+
FILE_ONLY: "file",
1298+
});
1299+
});
1300+
12681301
it("ignores missing optional EnvironmentFile entries", async () => {
12691302
await expectExecStartWithoutEnvironment("EnvironmentFile=-%h/.openclaw/missing.env");
12701303
});

‎src/daemon/systemd.ts‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ import {
5858
} from "./systemd-unavailable.js";
5959
import {
6060
buildSystemdUnit,
61-
parseSystemdEnvAssignment,
6261
parseSystemdEnvAssignments,
6362
parseSystemdExecStart,
6463
renderSystemdEnvAssignment,
@@ -328,8 +327,7 @@ export async function readSystemdServiceExecStart(
328327
workingDirectory = line.slice("WorkingDirectory=".length).trim();
329328
} else if (line.startsWith("Environment=")) {
330329
const raw = line.slice("Environment=".length).trim();
331-
const parsed = parseSystemdEnvAssignment(raw);
332-
if (parsed) {
330+
for (const parsed of parseSystemdEnvAssignments(raw)) {
333331
inlineEnvironment[parsed.key] = parsed.value;
334332
}
335333
} else if (line.startsWith("EnvironmentFile=")) {

0 commit comments

Comments
 (0)