Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -319,16 +319,35 @@ describe('theme build --icons-specifier (spawned processes)', () => {
` icons: testIcons,\n` +
` tokens: {'--color-bg': '#0a0b0c'},\n` +
`};\n`;
fs.writeFileSync(themeFile, touched);
// Each touch is atomic: staged in a sibling file and renamed over the
// theme, as an editor's atomic save does (the watcher follows renames).
// A rebuild reads the theme twice, loading the module and then scanning
// its source for the icon import, and an in-place write truncates
// first: a re-touch landing between those two reads gives a rebuild
// that exits 0 with the new token and no import line.
const touch = () => {
const staged = `${themeFile}.tmp`;
fs.writeFileSync(staged, touched);
fs.renameSync(staged, themeFile);
};
touch();
// The rebuilt module as read when the new token showed up. `theme
// build` renames its outputs into place, so every read sees a whole
// module; the assertion reads this snapshot, the module the poll
// accepted, since a re-touch can queue one more rebuild after it.
let rebuiltModule = '';
const rebuilt = await waitFor(() => {
try {
if (fs.readFileSync(builtFile, 'utf-8').includes('#0a0b0c'))
const generated = fs.readFileSync(builtFile, 'utf-8');
if (generated.includes('#0a0b0c')) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The token is the only gate here, so this accepts the import-less module described above, and rebuiltModule keeps it. Staging the re-touch and renaming it over the theme removes the race, and the watcher already follows renames (packages/cli/clients/cli/commands/theme.mjs:206-219):

const staged = `${themeFile}.tmp`;
fs.writeFileSync(staged, touched);
fs.renameSync(staged, themeFile);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in b033699, as suggested: each touch is staged in a sibling .tmp file and renamed over the theme. A scratch copy of the test whose in-place re-touch holds its truncate open for 300 ms fails 5 of 5 runs with the CI error. The same copy with the atomic touch, its staged file held open the same 300 ms, passes 5 of 5.

rebuiltModule = generated;
return true;
}
} catch {
// JavaScript module mid-write; fall through to re-touch.
// Not readable yet; fall through to re-touch.
}
try {
fs.writeFileSync(themeFile, touched);
touch();
} catch {
// Retried on the next poll.
}
Expand All @@ -340,9 +359,7 @@ describe('theme build --icons-specifier (spawned processes)', () => {
// forwarding is what this test pins. A watch loop that dropped the flag
// would regenerate with the scraped './icons' here and ship the #4620
// bytes on every save.
expect(iconImportLine(fs.readFileSync(builtFile, 'utf8'))).toBe(
declaredImport,
);
expect(iconImportLine(rebuiltModule)).toBe(declaredImport);
} finally {
child.kill('SIGINT');
}
Expand Down
Loading