Skip to content

fix: leave unchanged generated files alone, and replace changed ones atomically - #17310

Merged
Nic-Polumeyv merged 3 commits into
sveltejs:mainfrom
evdama:sync-write-unchanged-atomic
Oct 6, 2026
Merged

Nic-Polumeyv merged 3 commits into
sveltejs:mainfrom
evdama:sync-write-unchanged-atomic

Conversation

@evdama

@evdama evdama commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

closes #17309

write_if_changed only knew what the current process had written, so every new process that syncs (a vitest run, a second vite dev) rewrote every generated file with identical bytes. A running vite dev loads .svelte-kit/generated/dev/ and answered each of those rewrites with an SSR program 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_changed compares against the file on disk when it has no record of its own, and leaves an identical file alone.
  • write writes 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 for route_meta_data.json, here for everything that goes through write (including node_modules/$app/tsconfig.json, which a project's tsconfig.json extends). 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 in write_if_changed, so that write keeps meaning "write": core/adapt/builder.js calls it directly.

Tests. Five in utils.spec.js. The first one fails on main (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.js is byte-identical between 3.0.0 and main, so I dropped this PR's file into a stock sv create project on 3.0.0 with vite dev running:

one vitest run eleven vitest runs
3.0.0 18 of 27 generated files get a new mtime under an unchanged sha256 33 page reload and 22 program reload lines
this PR 3 of 27 (below) no reload line

Adding and removing a route while the dev server ran was still picked up (200, then 404), and no .tmp file was left behind.

Not in this PR. The three files still touched on every run are written elsewhere: route_meta_data.json and the route's $types.d.ts by write_types/index.js, which has its own fs.writeFileSync calls (#17095), and generated/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

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Ran locally, all passing: pnpm format, pnpm lint, pnpm check and pnpm -F @sveltejs/kit test:unit (77 files, 1058 tests). I did not run the Playwright suites locally.

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpm changeset and following the prompts. Changesets that add features should be minor and those that fix bugs should be patch. Please prefix changeset messages with feat:, fix:, or chore:.

Edits

  • Please ensure that 'Allow edits from maintainers' is checked. PRs without this option may be closed.

🤖 Generated with Claude Code

…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]>
Copilot AI balanced review requested due to automatic review settings October 2, 2026 20:02
@pkg-svelte-dev

pkg-svelte-dev Bot commented Oct 2, 2026 •

Copy link
Copy Markdown

Install the latest version of @sveltejs/kit from 8fec3b0:

pnpm add https://pkg.svelte.dev/@sveltejs/kit/c/8fec3b0388417d0c784df8389805d9e0ebc48b53

Open in pkg.svelte.dev: https://pkg.svelte.dev/repos/kit/pr/17310

Note

This PR is from a fork. A maintainer must approve approve each commit before it can be built and installed.

@changeset-bot

changeset-bot Bot commented Oct 2, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 8fec3b0

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@sveltejs/kit Patch

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

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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 High severity · 1 Low severity

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.

Comment thread packages/kit/src/core/sync/utils.js Outdated
Comment on lines +41 to +48
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', () => {
evdama and others added 2 commits October 2, 2026 22:07
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 Nic-Polumeyv left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks!

@teemingc

teemingc commented Oct 3, 2026

Copy link
Copy Markdown
Member

@Nic-Polumeyv do you know if this would reopen #17224 ? Would we still need your PR which removes caching?

@Nic-Polumeyv

Nic-Polumeyv commented Oct 3, 2026 •

Copy link
Copy Markdown
Member

It won't, write_types doesn't go through write_if_changed. I ran sync a few times on this branch and src/routes/$types.d.ts doesn't get deleted. So yes, #17231 is still needed.

@evdama

evdama commented Oct 6, 2026

Copy link
Copy Markdown
Contributor Author

Hi @teemingc - following up on your question about #17224: as @Nic-Polumeyv confirmed above, write_types doesn't go through write_if_changed, so this doesn't reopen it, and #17231 is still needed on its own.

Is there anything else you'd like changed here before it can go into a release? Happy to adjust. Thanks!

@Nic-Polumeyv
Nic-Polumeyv merged commit 9b56543 into sveltejs:main Oct 6, 2026
37 of 38 checks passed
@github-actions github-actions Bot mentioned this pull request Oct 6, 2026
teemingc added a commit that referenced this pull request Oct 14, 2026
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]>
Bewinxed added a commit to Bewinxed/cawco that referenced this pull request Oct 17, 2026
…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]>
dadezzz pushed a commit to dadezzz/events-cash-register that referenced this pull request Oct 18, 2026
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) | ![age](https://developer.mend.io/api/mc/badges/age/npm/@sveltejs%2fkit/3.1.0?slim=true) | ![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/@sveltejs%2fkit/3.0.1/3.1.0?slim=true) |

---

### Release Notes

<details>
<summary>sveltejs/kit (@&#8203;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 ([#&#8203;17398](sveltejs/kit#17398))

- feat: emit a `sveltekit.endpoint` span for `+server` handlers when server tracing is enabled ([#&#8203;17325](sveltejs/kit#17325))

##### Patch Changes

- fix: don't treat every module as server-only when the project is inside a `server` directory ([#&#8203;17376](sveltejs/kit#17376))

- fix: continue deployment recovery when service worker updates fail ([#&#8203;17440](sveltejs/kit#17440))

- fix: infer prerendered MIME types from generated files ([#&#8203;17381](sveltejs/kit#17381))

- fix: leave generated files alone when their contents are unchanged ([#&#8203;17310](sveltejs/kit#17310))

- fix: leave focus in the parent page when an app in an unfocused iframe starts or navigates ([#&#8203;17297](sveltejs/kit#17297))

- fix: don't warn about Storybook's Vite plugins that use `transformIndexHtml` ([#&#8203;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-->
renovate Bot added a commit to cigaleapp/cigale that referenced this pull request Oct 30, 2026