@devries-cameron20 / t3code-1 / commits / 9e180e5569
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(server): file editor saves keep a UTF-8 BOM (#36552)

Co-authored-by: Julius Marminge <julius0216@outlook.com>

Wout Stiens committed Nov 14, 2026 at 09:20 UTC 9e180e55693badee0d9f71322f7ded79b1829710
2 files changed +40 -1
apps/server/src/workspace/WorkspaceFileSystem.test.ts
+12
@@ -236,6 +236,18 @@ it.layer(layerTest, { excludeTestServices: true })("WorkspaceFileSystemLive", (i
236 truncated: false,
237 });
238
239 + const bomContents = "\uFEFFexport const answer = 42;\n";
240 + yield* writeTextFile(cwd, "bom.ts", bomContents);
241 + const bomRead = yield* workspaceFileSystem.readFile({ cwd, relativePath: "bom.ts" });
242 + yield* workspaceFileSystem.writeFile({
243 + cwd,
244 + relativePath: "bom.ts",
245 + contents: bomRead.contents.replace("42", "43"),
246 + });
247 + const fs = yield* FileSystem.FileSystem;
248 + const saved = yield* fs.readFile(`${cwd}/bom.ts`);
249 + expect([...saved]).toEqual([...new TextEncoder().encode(bomContents.replace("42", "43"))]);
250 +
251 yield* writeTextFile(cwd, "report.txt", "ordinary file");
252 for (const relativePath of whitespaceFilePaths) {
253 yield* writeTextFile(cwd, relativePath, "whitespace file");
apps/server/src/workspace/WorkspaceFileSystem.ts
+28 -1
@@ -135,6 +135,27 @@ export class WorkspaceFileSystem extends Context.Service<
135 }
136 >()("t3/workspace/WorkspaceFileSystem") {}
137
138 +const UTF8_BOM = "\uFEFF";
139 +
140 +async function startsWithUtf8Bom(filePath: string): Promise<boolean> {
141 + const handle = await NodeFSP.open(
142 + filePath,
143 + // O_NONBLOCK keeps a FIFO with no writer from hanging the save.
144 + NodeFS.constants.O_RDONLY |
145 + (NodeFS.constants.O_NOFOLLOW ?? 0) |
146 + (NodeFS.constants.O_NONBLOCK ?? 0),
147 + ).catch(() => null);
148 + if (!handle) return false;
149 + try {
150 + const { bytesRead, buffer } = await handle.read(Buffer.alloc(3), 0, 3, 0);
151 + return bytesRead === 3 && buffer[0] === 0xef && buffer[1] === 0xbb && buffer[2] === 0xbf;
152 + } catch {
153 + return false;
154 + } finally {
155 + await handle.close();
156 + }
157 +}
158 +
159 /** @public */
160 export const make = Effect.gen(function* () {
161 const fileSystem = yield* FileSystem.FileSystem;
@@ -561,6 +582,12 @@ export const make = Effect.gen(function* () {
582 const replacesFile = yield* fileSystem
583 .exists(realTargetPath)
584 .pipe(Effect.orElseSucceed(() => false));
585 + // readFile decodes the BOM away, so an editor save would otherwise drop it from the file.
586 + const keepsBom =
587 + replacesFile &&
588 + !input.contents.startsWith(UTF8_BOM) &&
589 + (yield* Effect.promise(() => startsWithUtf8Bom(realTargetPath)));
590 + const contents = keepsBom ? UTF8_BOM + input.contents : input.contents;
591
592 yield* fileSystem.makeDirectory(path.dirname(realTargetPath), { recursive: true }).pipe(
593 Effect.mapError(
@@ -578,7 +605,7 @@ export const make = Effect.gen(function* () {
605 yield* Effect.tryPromise({
606 // A symlink swapped in after the check is not followed. Windows lacks O_NOFOLLOW.
607 try: () =>
581 - NodeFSP.writeFile(realTargetPath, input.contents, {
608 + NodeFSP.writeFile(realTargetPath, contents, {
609 flag:
610 NodeFS.constants.O_WRONLY |
611 NodeFS.constants.O_CREAT |