Skip to content

Commit 9a8321f

Browse files
t3dotggclaude
andcommitted
fix(server): theme publish does not write through a staging symlink
A link left at the staging path made the write land on the link's target. The staging file is now removed first and created exclusively. Co-Authored-By: Claude Fable 5.2 <[email protected]>
1 parent 89d3a31 commit 9a8321f

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

‎apps/server/src/environmentTheme.test.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,28 @@ it.layer(NodeServices.layer)("environment theme", (it) => {
156156
),
157157
);
158158

159+
it.effect.skipIf(!symlinksSupported)("does not write through a symlink at its staging path", () =>
160+
withEnvironmentThemes(
161+
{},
162+
Effect.gen(function* () {
163+
const { environmentThemesDir } = yield* ServerConfig.ServerConfig;
164+
const fs = yield* FileSystem.FileSystem;
165+
const path = yield* Path.Path;
166+
const themes = yield* EnvironmentTheme.EnvironmentThemeService;
167+
const outside = path.join(environmentThemesDir, "..", "outside.txt");
168+
yield* fs.writeFileString(outside, "keep");
169+
yield* fs.symlink(
170+
outside,
171+
path.join(environmentThemesDir, `look.json.staging-${process.pid}`),
172+
);
173+
174+
yield* themes.apply({ themeId: "look", theme: NIGHTFALL_THEME });
175+
assert.strictEqual(yield* fs.readFileString(outside), "keep");
176+
assert.deepEqual(yield* themes.current, [{ id: "look", ...NIGHTFALL_THEME }]);
177+
}),
178+
),
179+
);
180+
159181
it.effect.skipIf(!symlinksSupported)("ignores a symlinked theme file", () =>
160182
withEnvironmentThemes(
161183
{},

‎apps/server/src/environmentTheme.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,10 @@ const make = Effect.gen(function* () {
269269
const destination = `${environmentThemesDir}/${themeId}${THEME_FILE_SUFFIX}`;
270270
// The watcher reads only .json names, so it never sees a half-written file.
271271
const staging = `${destination}.staging-${process.pid}`;
272+
// Exclusive create: a link left at the staging path is removed, never written through.
272273
const replace = (next: string) =>
273-
fs.writeFileString(staging, next).pipe(
274+
fs.remove(staging, { force: true }).pipe(
275+
Effect.andThen(fs.writeFileString(staging, next, { flag: "wx" })),
274276
Effect.andThen(fs.rename(staging, destination)),
275277
Effect.tapError(() => fs.remove(staging, { force: true }).pipe(Effect.ignore)),
276278
);

0 commit comments

Comments
 (0)