The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
fix(server): theme publish refuses a directory under the theme's file name (#39126)
Co-authored-by: Julius Marminge <julius0216@outlook.com>
Wout Stiens committed
Nov 19, 2026 at 15:18 UTC
25854935ba533557d58a6752deede6ae06b79e0e
3 files changed
+174
-11
apps/server/src/cli/theme.test.ts
+72
@@ -12,11 +12,14 @@ import * as Effect from "effect/Effect";
12
import * as Layer from "effect/Layer";
13
import * as TestConsole from "effect/testing/TestConsole";
14
import { Command } from "effect/cli";
15
+import { vi } from "vite-plus/test";
16
17
import { cli } from "../binCli.ts";
18
import { symlinksSupported } from "@t3tools/shared/testing/symlinks";
19
import * as HostProcess from "@t3tools/shared/HostProcess";
20
21
+vi.mock("node:fs", { spy: true });
22
+
23
// Windows chmod cannot force these directory or file permission failures.
24
const windowsHost = HostProcess.Platform.defaultValue() === "win32";
25
@@ -133,6 +136,75 @@ describe("t3 theme", () => {
136
}),
137
);
138
139
+ it.effect("refuses to publish over a directory with the theme's file name", () =>
140
+ Effect.gen(function* () {
141
+ const baseDir = makeBaseDir();
142
+ const taken = NodePath.join(baseDir, "userdata", "themes", "nightfall.json");
143
+ NodeFS.mkdirSync(taken, { recursive: true });
144
+ NodeFS.writeFileSync(NodePath.join(taken, "notes.txt"), "notes");
145
+ const themeFile = NodePath.join(baseDir, "nightfall.json");
146
+ NodeFS.writeFileSync(themeFile, NIGHTFALL_THEME_JSON);
147
+
148
+ const failure = yield* runCli(["theme", "set", themeFile, "--base-dir", baseDir]).pipe(
149
+ Effect.flip,
150
+ );
151
+
152
+ assert.include(String(failure), "is a directory");
153
+ assert.equal(NodeFS.readFileSync(NodePath.join(taken, "notes.txt"), "utf8"), "notes");
154
+ assert.deepEqual(NodeFS.readdirSync(NodePath.dirname(taken)), ["nightfall.json"]);
155
+ assert.equal(NodeFS.existsSync(settingsPathFor(baseDir)), false);
156
+ }),
157
+ );
158
+
159
+ it.effect("refuses to publish over a directory created just before backup", () =>
160
+ Effect.gen(function* () {
161
+ const baseDir = makeBaseDir();
162
+ const themeFile = NodePath.join(baseDir, "nightfall.json");
163
+ NodeFS.writeFileSync(themeFile, NIGHTFALL_THEME_JSON);
164
+ const themesDir = NodePath.join(baseDir, "userdata", "themes");
165
+ const destination = NodePath.join(themesDir, "nightfall.json");
166
+ const notes = NodePath.join(destination, "notes.txt");
167
+ const { renameSync } = yield* Effect.promise(() => vi.importActual<typeof NodeFS>("node:fs"));
168
+ using _insertDirectory = vi.spyOn(NodeFS, "renameSync").mockImplementationOnce((from, to) => {
169
+ NodeFS.mkdirSync(destination);
170
+ NodeFS.writeFileSync(notes, "notes");
171
+ renameSync(from, to);
172
+ });
173
+
174
+ const failure = yield* runCli(["theme", "set", themeFile, "--base-dir", baseDir]).pipe(
175
+ Effect.flip,
176
+ );
177
+
178
+ assert.include(String(failure), "is a directory");
179
+ assert.equal(NodeFS.readFileSync(notes, "utf8"), "notes");
180
+ assert.deepEqual(NodeFS.readdirSync(themesDir), ["nightfall.json"]);
181
+ assert.equal(NodeFS.existsSync(settingsPathFor(baseDir)), false);
182
+ }),
183
+ );
184
+
185
+ it.effect("publishes a theme despite a rollback file left by an earlier process", () =>
186
+ Effect.gen(function* () {
187
+ const baseDir = makeBaseDir();
188
+ const themeFile = NodePath.join(baseDir, "nightfall.json");
189
+ NodeFS.writeFileSync(themeFile, NIGHTFALL_THEME_JSON);
190
+ const themesDir = NodePath.join(baseDir, "userdata", "themes");
191
+ const destination = NodePath.join(themesDir, "nightfall.json");
192
+ const staleBackup = `${destination}.rollback-${process.pid}`;
193
+ NodeFS.mkdirSync(themesDir, { recursive: true });
194
+ NodeFS.writeFileSync(staleBackup, "previous theme");
195
+
196
+ yield* runCli(["theme", "set", themeFile, "--base-dir", baseDir]);
197
+
198
+ assert.equal(readSettings(baseDir).defaultTheme, "nightfall");
199
+ assert.equal(NodeFS.readFileSync(destination, "utf8"), NIGHTFALL_THEME_JSON);
200
+ assert.equal(NodeFS.readFileSync(staleBackup, "utf8"), "previous theme");
201
+ assert.deepEqual(NodeFS.readdirSync(themesDir).sort(), [
202
+ "nightfall.json",
203
+ NodePath.basename(staleBackup),
204
+ ]);
205
+ }),
206
+ );
207
+
208
it.effect("rejects a file that is not a theme and sets nothing", () =>
209
Effect.gen(function* () {
210
const baseDir = makeBaseDir();
apps/server/src/cli/theme.ts
+52
-11
@@ -1,6 +1,7 @@
1
// @effect-diagnostics nodeBuiltinImport:off - publish commits and rollbacks
2
// move exact directory entries with rename, which the FileSystem service does
3
// not expose atomically.
4
+import * as NodeCrypto from "node:crypto";
5
import * as NodeFS from "node:fs";
6
7
import {
@@ -120,6 +121,17 @@ export class ThemePublishError extends Schema.TaggedError<ThemePublishError>()(
121
}
122
}
123
124
+export class ThemeDestinationDirectoryError extends Schema.TaggedError<ThemeDestinationDirectoryError>()(
125
+ "ThemeDestinationDirectoryError",
126
+ { themeId: Schema.String, destinationPath: Schema.String },
127
+) {
128
+ override get message(): string {
129
+ return `${this.destinationPath} is a directory, so the theme cannot be published as "${this.themeId}". Move it, or pass another id with --id.`;
130
+ }
131
+}
132
+
133
+const isThemeDestinationDirectoryError = Schema.is(ThemeDestinationDirectoryError);
134
+
135
const INVALID_THEME_ID_REASON =
136
"is not a valid theme id (lowercase letters, digits, and hyphens; not an appearance keyword)";
137
@@ -306,8 +318,6 @@ const publishThemeFile = Effect.fn(function* (input: {
318
}
319
320
const destinationPath = path.join(input.themesDir, `${themeId}.json`);
309
- // Non-JSON suffixes hide these from the watcher; PIDs isolate concurrent publishers.
310
- const backupPath = `${destinationPath}.rollback-${process.pid}`;
321
const stagingPath = `${destinationPath}.staging-${process.pid}`;
322
yield* fs
323
.makeDirectory(input.themesDir, { recursive: true })
@@ -316,6 +326,14 @@ const publishThemeFile = Effect.fn(function* (input: {
326
const publishFailure = (cause: unknown) =>
327
new ThemePublishError({ themesDir: input.themesDir, cause });
328
329
+ const destinationIsDirectory = yield* Effect.try({
330
+ try: () => NodeFS.lstatSync(destinationPath, { throwIfNoEntry: false })?.isDirectory() ?? false,
331
+ catch: publishFailure,
332
+ });
333
+ if (destinationIsDirectory) {
334
+ return yield* new ThemeDestinationDirectoryError({ themeId, destinationPath });
335
+ }
336
+
337
// O_EXCL prevents following a symlink at the predictable staging path.
338
// Preserve bytes exactly: even an added newline could exceed the watcher's size limit.
339
const stagedIno = yield* Effect.try({
@@ -338,19 +356,42 @@ const publishThemeFile = Effect.fn(function* (input: {
356
catch: publishFailure,
357
});
358
341
- // Atomic rename avoids a check/replace race and preserves the exact entry for rollback.
342
- const hadPrevious = yield* Effect.try({
359
+ const backupPath = yield* Effect.try({
360
try: () => {
361
+ // A fresh path leaves any backup from a crashed publish intact.
362
+ const rollbackPath = `${destinationPath}.rollback-${NodeCrypto.randomUUID()}`;
363
+ // A rename onto a regular file rejects a directory even if it appeared after lstat.
364
+ const fd = NodeFS.openSync(rollbackPath, "wx", 0o600);
365
+ let saved = false;
366
try {
345
- NodeFS.renameSync(destinationPath, backupPath);
346
- return true;
367
+ NodeFS.closeSync(fd);
368
+ NodeFS.renameSync(destinationPath, rollbackPath);
369
+ saved = true;
370
+ return rollbackPath;
371
} catch (error) {
348
- if ((error as NodeJS.ErrnoException).code === "ENOENT") return false;
372
+ if ((error as NodeJS.ErrnoException).code === "ENOENT") return null;
373
+ if (NodeFS.lstatSync(destinationPath, { throwIfNoEntry: false })?.isDirectory()) {
374
+ throw new ThemeDestinationDirectoryError({ themeId, destinationPath });
375
+ }
376
throw error;
377
+ } finally {
378
+ if (!saved) {
379
+ try {
380
+ NodeFS.unlinkSync(rollbackPath);
381
+ } catch {}
382
+ }
383
}
384
},
352
- catch: publishFailure,
353
- });
385
+ catch: (cause) => (isThemeDestinationDirectoryError(cause) ? cause : publishFailure(cause)),
386
+ }).pipe(
387
+ Effect.onError(() =>
388
+ Effect.sync(() => {
389
+ try {
390
+ NodeFS.unlinkSync(stagingPath);
391
+ } catch {}
392
+ }),
393
+ ),
394
+ );
395
396
const revert = Effect.sync(() => {
397
try {
@@ -365,7 +406,7 @@ const publishThemeFile = Effect.fn(function* (input: {
406
return null;
407
}
408
})();
368
- if (hadPrevious) {
409
+ if (backupPath !== null) {
410
if (destinationIno === null || destinationIno === stagedIno) {
411
NodeFS.renameSync(backupPath, destinationPath);
412
} else {
@@ -378,7 +419,7 @@ const publishThemeFile = Effect.fn(function* (input: {
419
});
420
const cleanup = Effect.sync(() => {
421
try {
381
- if (hadPrevious) NodeFS.unlinkSync(backupPath);
422
+ if (backupPath !== null) NodeFS.unlinkSync(backupPath);
423
} catch {}
424
});
425
scripts/scenarios/cli-theme-publish-keeps-existing-directory.e2e.ts
new
+50
@@ -0,0 +1,50 @@
1
+/// <reference lib="dom" />
2
+import { expect } from "e2e";
3
+
4
+import { makeState, runPromise, test, WEB } from "./t3.ts";
5
+
6
+const NOTE = "userdata/themes/nightfall.json/notes.txt";
7
+const BODY = "Notes for the original Nightfall palette";
8
+
9
+test(
10
+ "Publishing a theme preserves files in an existing directory under the same name",
11
+ WEB,
12
+ async ({ t3, app, browser, screen }) => {
13
+ const project = await t3.project("theme-workspace", {
14
+ [NOTE]: BODY,
15
+ "nightfall.json": JSON.stringify({
16
+ name: "Nightfall",
17
+ appearance: "dark",
18
+ canvas: "#1a1b26",
19
+ accent: "#7aa2f7",
20
+ }),
21
+ });
22
+ const thread = await t3.thread({
23
+ project,
24
+ title: "Theme notes",
25
+ messages: [`See :codex-file-citation{path="${NOTE}" line_range_start="1"}`],
26
+ });
27
+ const { cli } = makeState(project);
28
+ await runPromise(cli(["theme", "set", `${project}/nightfall.json`], [0, 1]));
29
+
30
+ await t3.open(thread);
31
+ await screen.getByRole("button", /^Agent See/).tap();
32
+ await screen.getByRole("link", /^notes\.txt/).tap();
33
+
34
+ try {
35
+ await expect
36
+ .poll(() =>
37
+ browser.evaluate(() => {
38
+ for (const host of document.querySelectorAll("*")) {
39
+ const content = host.shadowRoot?.querySelector("[data-content]");
40
+ if (content) return content.textContent ?? "";
41
+ }
42
+ return null;
43
+ }),
44
+ )
45
+ .toContain(BODY);
46
+ } finally {
47
+ await app.screenshot("open-original-theme-notes");
48
+ }
49
+ },
50
+);