Skip to content

Commit b033699

Browse files
committed
test(cli): the watch-mode --icons-specifier test touches the theme atomically
A rebuild reads the theme twice: it loads the module, then scans its source for the icon import. The test's re-touch was an in-place writeFileSync, which truncates first, so a re-touch landing between those two reads gave a rebuild an empty source to scan; with the test's empty icon registry that build exits 0 with the new token and no import line, and the poll accepted it. Each touch is now staged in a sibling file and renamed over the theme, as an editor's atomic save does (the watcher follows renames). The comment above the snapshot names the real race: theme build renames its outputs into place, so a read of the module was never half-written.
1 parent 7a9c932 commit b033699

1 file changed

Lines changed: 18 additions & 8 deletions

File tree

‎packages/cli/clients/cli/commands/build-theme.icons-specifier.test.mjs‎

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -319,12 +319,22 @@ describe('theme build --icons-specifier (spawned processes)', () => {
319319
` icons: testIcons,\n` +
320320
` tokens: {'--color-bg': '#0a0b0c'},\n` +
321321
`};\n`;
322-
fs.writeFileSync(themeFile, touched);
323-
// The rebuilt module as read when the new token showed up. The
324-
// assertion below reads this snapshot, not the file again: a re-touch
325-
// in this loop can queue one more rebuild, and a second read can land
326-
// inside that rewrite (writeFileSync truncates first) and see an empty
327-
// module.
322+
// Each touch is atomic: staged in a sibling file and renamed over the
323+
// theme, as an editor's atomic save does (the watcher follows renames).
324+
// A rebuild reads the theme twice, loading the module and then scanning
325+
// its source for the icon import, and an in-place write truncates
326+
// first: a re-touch landing between those two reads gives a rebuild
327+
// that exits 0 with the new token and no import line.
328+
const touch = () => {
329+
const staged = `${themeFile}.tmp`;
330+
fs.writeFileSync(staged, touched);
331+
fs.renameSync(staged, themeFile);
332+
};
333+
touch();
334+
// The rebuilt module as read when the new token showed up. `theme
335+
// build` renames its outputs into place, so every read sees a whole
336+
// module; the assertion reads this snapshot, the module the poll
337+
// accepted, since a re-touch can queue one more rebuild after it.
328338
let rebuiltModule = '';
329339
const rebuilt = await waitFor(() => {
330340
try {
@@ -334,10 +344,10 @@ describe('theme build --icons-specifier (spawned processes)', () => {
334344
return true;
335345
}
336346
} catch {
337-
// JavaScript module mid-write; fall through to re-touch.
347+
// Not readable yet; fall through to re-touch.
338348
}
339349
try {
340-
fs.writeFileSync(themeFile, touched);
350+
touch();
341351
} catch {
342352
// Retried on the next poll.
343353
}

0 commit comments

Comments
 (0)