Repository navigation
fix: leave unchanged generated files alone, and replace changed ones atomically - #17310
Conversation
…atomically write_if_changed only knew what the current process had written, so every new process that syncs rewrote every generated file with identical bytes, and a running dev server reloaded on each. It now compares against the file on disk. write goes through a temporary file and a rename, so a concurrent reader never sees an empty or partially written file. closes sveltejs#17309 Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
|
Install the latest version of pnpm add https://pkg.svelte.dev/@sveltejs/kit/c/8fec3b0388417d0c784df8389805d9e0ebc48b53Open in Note This PR is from a fork. A maintainer must approve approve each commit before it can be built and installed. |
🦋 Changeset detectedLatest commit: 8fec3b0 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
Temporary-write failures can trigger a destructive direct-write fallback, and the new failure paths lack coverage.
Review effort: Balanced
Findings: 1
Open (2)
What changed in this PR
Prevents unnecessary generated-file rewrites and introduces atomic replacement.
Changes:
- Compares generated content with files on disk.
- Writes through temporary files before renaming.
- Adds unit tests and a patch changeset.
| File | Description |
|---|---|
packages/kit/src/core/sync/utils.js |
Adds disk comparison and atomic writes. |
packages/kit/src/core/sync/utils.spec.js |
Tests generated-file write behavior. |
.changeset/quiet-files-rest.md |
Documents the patch. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| try { | ||
| fs.writeFileSync(tmp, code); | ||
| fs.renameSync(tmp, file); | ||
| } catch { | ||
| // replacing a file that another process has open can fail on Windows | ||
| fs.rmSync(tmp, { force: true }); | ||
| fs.writeFileSync(file, code); | ||
| } |
| expect(fs.statSync(file).mtime).not.toEqual(long_ago); | ||
| }); | ||
|
|
||
| test('write creates missing directories and leaves no temporary file behind', () => { |
A failure to write the temporary file now cleans up and rethrows, leaving the existing file untouched, instead of falling through to a direct write. Adds tests for both failure paths. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
The in-process map is redundant once the comparison reads the file, and it kept a file from being recreated after another process deleted it. The temporary file and rename had no captured failure behind them and added failure modes of their own (an unguarded cleanup in the rename fallback, per-pid temporary names, and temporary files in adapter output), so write stays what it was.
|
@Nic-Polumeyv do you know if this would reopen #17224 ? Would we still need your PR which removes caching? |
|
It won't, |
|
Hi @teemingc - following up on your question about #17224: as @Nic-Polumeyv confirmed above, Is there anything else you'd like changed here before it can go into a release? Happy to adjust. Thanks! |
This PR was opened by the [Changesets release](https://github.com/changesets/action) GitHub action. When you're ready to do a release, you can merge this and the packages will be published to npm automatically. If you're not ready to do a release yet, that's fine, whenever you add more changesets to main, this PR will be updated. # Releases ## @sveltejs/[email protected] ### Minor Changes - feat: support `report-sha256`/`report-sha384`/`report-sha512` CSP hash-reporting keywords ([#17398](#17398)) - feat: emit a `sveltekit.endpoint` span for `+server` handlers when server tracing is enabled ([#17325](#17325)) ### Patch Changes - fix: don't treat every module as server-only when the project is inside a `server` directory ([#17376](#17376)) - fix: continue deployment recovery when service worker updates fail ([#17440](#17440)) - fix: infer prerendered MIME types from generated files ([#17381](#17381)) - fix: leave generated files alone when their contents are unchanged ([#17310](#17310)) - fix: leave focus in the parent page when an app in an unfocused iframe starts or navigates ([#17297](#17297)) - fix: don't warn about Storybook's Vite plugins that use `transformIndexHtml` ([#17373](#17373)) ## @sveltejs/[email protected] ### Patch Changes - fix: start the dev server on runtimes such as Deno that define `caches` as a getter-only global ([#17401](#17401)) - fix: start one platform proxy for concurrent Vite servers and keep it alive until the last one closes ([#17368](#17368)) - fix: support `Span#setStatus` and `Span#updateName` on local emulation of Cloudflare `tracing` ([#17400](#17400)) - Updated dependencies: - @sveltejs/[email protected] ## @sveltejs/[email protected] ### Patch Changes - fix: keep the detected Bun minor in the resolved runtime ([#17331](#17331)) - Updated dependencies: - @sveltejs/[email protected] ## @sveltejs/[email protected] ### Patch Changes - fix: emit component types for the installed Svelte version, rather than the oldest one the `svelte` dependency range allows ([#17330](#17330)) --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Tee Ming <[email protected]>
…ds loads on a stage Invariant: every Vite config that Vite's default (`bundle`) loader loads also loads in a stage, and the stage writes nothing into node_modules. - The stage host runs on the agent's own bun on every machine (boundary.ts bunScriptRuntime, BUN_SCRIPT_ENV: a binary install's `<binary root>/run` with BUN_BE_BUN=1, bun in a checkout; the boundary's own scripts already run on it). The lockfile's bun-or-node choice is gone. `configLoader: "native"` imports the config with that bun, which reads TS enums, extensionless relative imports and `__dirname` in ESM; under node the same config failed "TypeScript enum is not supported in strip-only mode". No release fixes the alternatives: Vite 8.3.3/8.3.4 change no config loading, `bundle` still throws on EROFS (vitejs/vite#18839 open), and `runner` fails on any tsconfig `extends` oxc can't follow (oxc-resolver#1185, vitejs/vite#21852). - @sveltejs/kit ^3.1.0: "fix: leave generated files alone when their contents are unchanged" (sveltejs/kit#17310: "It now compares against the file on disk"). Kit 3.0.0 rewrote node_modules/$app/tsconfig.json at every start and died on a read-only node_modules ("EACCES: permission denied, mkdir '…/node_modules/$app'"). - vite ^8.3.4 (latest), its overrides' floor with it. Measured with every node_modules dir read-only (SvelteKit 3 dashboard, Astro 7.4.0, Nuxt 4.6.2, plain Vite 8.3.4 with a TS config holding an extensionless import, an enum and __dirname), on bun and on a compiled bun binary with BUN_BE_BUN=1: each ready, its sheet page and component 200, nothing under node_modules newer than the start. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This PR contains the following updates: | Package | Change | [Age](https://docs.renovatebot.com/merge-confidence/) | [Confidence](https://docs.renovatebot.com/merge-confidence/) | |---|---|---|---| | [@sveltejs/kit](https://svelte.dev) ([source](https://github.com/sveltejs/kit/tree/HEAD/packages/kit)) | [`3.0.1` → `3.1.0`](https://renovatebot.com/diffs/npm/@sveltejs%2fkit/3.0.1/3.1.0) |  |  | --- ### Release Notes <details> <summary>sveltejs/kit (@​sveltejs/kit)</summary> ### [`v3.1.0`](https://github.com/sveltejs/kit/blob/HEAD/packages/kit/CHANGELOG.md#310) [Compare Source](https://github.com/sveltejs/kit/compare/@sveltejs/[email protected]...@sveltejs/[email protected]) ##### Minor Changes - feat: support `report-sha256`/`report-sha384`/`report-sha512` CSP hash-reporting keywords ([#​17398](sveltejs/kit#17398)) - feat: emit a `sveltekit.endpoint` span for `+server` handlers when server tracing is enabled ([#​17325](sveltejs/kit#17325)) ##### Patch Changes - fix: don't treat every module as server-only when the project is inside a `server` directory ([#​17376](sveltejs/kit#17376)) - fix: continue deployment recovery when service worker updates fail ([#​17440](sveltejs/kit#17440)) - fix: infer prerendered MIME types from generated files ([#​17381](sveltejs/kit#17381)) - fix: leave generated files alone when their contents are unchanged ([#​17310](sveltejs/kit#17310)) - fix: leave focus in the parent page when an app in an unfocused iframe starts or navigates ([#​17297](sveltejs/kit#17297)) - fix: don't warn about Storybook's Vite plugins that use `transformIndexHtml` ([#​17373](sveltejs/kit#17373)) </details> --- ### Configuration 📅 **Schedule**: (UTC) - Branch creation - At any time (no schedule defined) - Automerge - At any time (no schedule defined) 🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied. ♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this PR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR has been generated by [Mend Renovate CLI](https://github.com/renovatebot/renovate). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0NC4xNjUuMSIsInVwZGF0ZWRJblZlciI6IjQ0LjE2NS4xIiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6W119-->


closes #17309
write_if_changedonly knew what the current process had written, so every new process that syncs (avitest run, a secondvite dev) rewrote every generated file with identical bytes. A runningvite devloads.svelte-kit/generated/dev/and answered each of those rewrites with an SSRprogram reload. Measurements and a reproduction on a stock project are in #17309.Two changes, both in
packages/kit/src/core/sync/utils.js:write_if_changedcompares against the file on disk when it has no record of its own, and leaves an identical file alone.writewrites to a temporary file in the same directory and renames it over the target, so a concurrent reader never sees an empty or partially written file. That is the window sync: concurrent runs can read a truncated route_meta_data.json (JSON.parse error) #17095 describes forroute_meta_data.json, here for everything that goes throughwrite(includingnode_modules/$app/tsconfig.json, which a project'stsconfig.jsonextends). If the rename fails - replacing a file that another process has open can fail on Windows - it falls back to writing the file directly, as before. If the temporary file cannot be written, the error is rethrown and the existing file is left untouched.In the issue I sketched the comparison inside
write. Here it lives inwrite_if_changed, so thatwritekeeps meaning "write":core/adapt/builder.jscalls it directly.Tests. Five in
utils.spec.js. The first one fails onmain(the mtime of a file with identical contents moves) and passes with this change; the last two cover the failed rename and the failed temporary write, added after the automated review.Measured end to end.
utils.jsis byte-identical between 3.0.0 andmain, so I dropped this PR's file into a stocksv createproject on 3.0.0 withvite devrunning:vitest runvitest runspage reloadand 22program reloadlinesAdding and removing a route while the dev server ran was still picked up (200, then 404), and no
.tmpfile was left behind.Not in this PR. The three files still touched on every run are written elsewhere:
route_meta_data.jsonand the route's$types.d.tsbywrite_types/index.js, which has its ownfs.writeFileSynccalls (#17095), andgenerated/dev/app-manifest.js, which seems to be written twice per start with different contents. The dev server did not reload on any of them. I am happy to split the two changes into separate PRs if you prefer.Please don't delete this checklist! Before submitting the PR, please make sure you do the following:
Tests
pnpm testand lint the project withpnpm lintandpnpm checkRan locally, all passing:
pnpm format,pnpm lint,pnpm checkandpnpm -F @sveltejs/kit test:unit(77 files, 1058 tests). I did not run the Playwright suites locally.Changesets
pnpm changesetand following the prompts. Changesets that add features should beminorand those that fix bugs should bepatch. Please prefix changeset messages withfeat:,fix:, orchore:.Edits
🤖 Generated with Claude Code