The best way to control Claude Code, Codex and more from any computer or phone
fix(cli): service.env updates keep quoted multiline values intact (#35976)
Julius Marminge committed
Nov 12, 2026 at 20:58 UTC
c54e5b31cebb524bda9ad787bba43a08d2488c8c
3 files changed
+49
-6
apps/server/src/cli/service.test.ts
+6
@@ -115,6 +115,12 @@ it.layer(Layer.mergeAll(NodeServices.layer, NetService.layer))("service commands
115
env: "T3CODE_HOST=::\n",
116
expected: "T3CODE_HOST=::\n",
117
},
118
+ {
119
+ label: "a quoted multiline value",
120
+ installed: false,
121
+ env: 'CUSTOM="first\nT3CODE_LAN=example\nlast"\n',
122
+ expected: 'CUSTOM="first\nT3CODE_LAN=example\nlast"\nT3CODE_LAN=false\n',
123
+ },
124
])("install keeps $label off the local network only when it is new", (input) =>
125
Effect.gen(function* () {
126
const fs = yield* FileSystem.FileSystem;
apps/server/src/cli/service.ts
+29
-6
@@ -47,14 +47,37 @@ export const upsertServiceEnv = (
47
entries: Readonly<Record<string, string | undefined>>,
48
): string => {
49
const written = new Set<string>();
50
- const lines = contents.split("\n").flatMap((line) => {
51
- const key = SERVICE_ENV_KEY.exec(line)?.[1];
52
- if (key === undefined || !Object.hasOwn(entries, key)) return [line];
50
+ const source = contents.split("\n");
51
+ const lines: string[] = [];
52
+ for (let index = 0, offset = 0; index < source.length;) {
53
+ const line = source[index]!;
54
+ const match = SERVICE_ENV_KEY.exec(line);
55
+ let end = index + 1;
56
+ if (match !== null) {
57
+ // util.parseEnv lets a quoted value run to its closing quote across lines,
58
+ // skipping only spaces, tabs and CRs before the opening quote.
59
+ const rest = line.slice(match[0].length).replace(/^[ \t\r]+/u, "");
60
+ const quote = rest[0];
61
+ if ((quote === '"' || quote === "'" || quote === "`") && !rest.includes(quote, 1)) {
62
+ const closing = contents.indexOf(quote, offset + line.length);
63
+ if (closing !== -1) {
64
+ end += contents.slice(offset + line.length, closing).split("\n").length - 1;
65
+ }
66
+ }
67
+ }
68
+ const span = source.slice(index, end);
69
+ offset += span.reduce((length, part) => length + part.length + 1, 0);
70
+ index = end;
71
+ const key = match?.[1];
72
+ if (key === undefined || !Object.hasOwn(entries, key)) {
73
+ lines.push(span.join("\n"));
74
+ continue;
75
+ }
76
const value = entries[key];
54
- if (value === undefined || written.has(key)) return [];
77
+ if (value === undefined || written.has(key)) continue;
78
written.add(key);
56
- return [`${key}=${value}`];
57
- });
79
+ lines.push(`${key}=${value}`);
80
+ }
81
while (lines.at(-1) === "") lines.pop();
82
for (const [key, value] of Object.entries(entries)) {
83
if (value !== undefined && !written.has(key)) lines.push(`${key}=${value}`);
apps/server/src/cli/up.test.ts
+14
@@ -413,4 +413,18 @@ it("rewrites only the named service.env keys and keeps the user's lines", () =>
413
["# my settings", "T3CODE_PORT=4000", "T3CODE_LAN=true", ""].join("\n"),
414
);
415
expect(upsertServiceEnv("", { T3CODE_LAN: "false" })).toBe("T3CODE_LAN=false\n");
416
+ expect(
417
+ upsertServiceEnv('T3CODE_LAN="a\nb"\nCUSTOM="x\nT3CODE_LAN=y"\nT3CODE_LAN=true\n', {
418
+ T3CODE_LAN: "false",
419
+ }),
420
+ ).toBe('T3CODE_LAN=false\nCUSTOM="x\nT3CODE_LAN=y"\n');
421
+ expect(
422
+ upsertServiceEnv('T3CODE_LAN=\u00a0"unused\nCUSTOM=keep\nEND="\nT3CODE_LAN=true\n', {
423
+ T3CODE_LAN: "false",
424
+ }),
425
+ ).toBe('T3CODE_LAN=false\nCUSTOM=keep\nEND="\n');
426
+ const long = `CUSTOM="${"\n".repeat(130_000)}"\n`;
427
+ expect(upsertServiceEnv(`${long}T3CODE_LAN=true\n`, { T3CODE_LAN: "false" })).toBe(
428
+ `${long}T3CODE_LAN=false\n`,
429
+ );
430
});