Skip to content

fix: ensure vite build watch mode works - #16632

Merged
Rich-Harris merged 26 commits into
version-3from
fix-vite-build-watch
Aug 11, 2026
Merged

fix: ensure vite build watch mode works#16632
Rich-Harris merged 26 commits into
version-3from
fix-vite-build-watch

Conversation

@teemingc

@teemingc teemingc commented Aug 3, 2026

Copy link
Copy Markdown
Member

fixes #16624

Few changes were required:

  • we need to correctly handle the result of builder.build which can return a watcher instead of the build chunks. Added a normaliser to handle that. Unfortunately, the bundle has to be taken from the generateBundle hook.
  • ignore generated files during a build when the watch mode is enabled. Copied the config from the dev server watch config option
  • move build logic into a function and re-run that in a watcher listener since the buildApp hook doesn't re-run on watch
  • reset variables since Vite's watch mode doesn't rerun plugin initialisation

This only fixes the regression. There were additional existing bugs I've listed in #16708 but I don't think it's a high priority to fix them now when Vite's build watch mode support itself wasn't fully thought out

Questions:

  • this remains unfixed in v2. Do we need to fix it there too?
  • no test added because it seems more trouble than it's worth. That said, it did regress. I think we can add a test where we monitor the logs of the ongoing build --watch process and end it if the assertions pass? with a timeout so it doesn't go on indefinitely?

@pkg-svelte-dev

pkg-svelte-dev Bot commented Aug 3, 2026

Copy link
Copy Markdown

Install the latest version of @sveltejs/kit from f9ed999:

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

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

@changeset-bot

changeset-bot Bot commented Aug 3, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: f9ed999

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

@svelte-docs-bot

Copy link
Copy Markdown

@teemingc teemingc added the vite label Aug 3, 2026
@teemingc teemingc changed the title fix: ignore generated files during vite build --watch fix: ensure vite build watch mode works Aug 3, 2026
@teemingc
teemingc force-pushed the fix-vite-build-watch branch from 49a9699 to 96fe7e0 Compare August 3, 2026 18:55
@Nic-Polumeyv

Copy link
Copy Markdown
Contributor

What if the replacement happened in generateBundle instead, mutating chunk.code before write? That would make rebuilds correct by construction and remove normalise_build entirely. As it stands the chunk threading also still crashes; the second argument is evaluated before the watcher's first build, so it's permanently undefined (repro'd with the pkg build from 49a9699 on vite 8.0.12 and 8.2.0, same "chunks is not iterable" as #16624; the ERROR path has the same issue).

@teemingc
teemingc marked this pull request as draft August 3, 2026 19:14
@teemingc
teemingc force-pushed the fix-vite-build-watch branch from e99e640 to 849b73d Compare August 3, 2026 19:21
@teemingc

teemingc commented Aug 3, 2026

Copy link
Copy Markdown
Member Author

The real issue is that all our logic in the buildApp hook doesn't re-run when watch mode is enabled 🤦🏼 going to enquire about this in the Vite discord before continuing

@Nic-Polumeyv

Copy link
Copy Markdown
Contributor

Tested 849b73d. The crash is gone and the full pipeline runs, adapter output included. An error during the first build still hits the original TypeError though, because normalise_build resolves on the ERROR event and the fallback is still undefined at that point. Any syntax error in a route reproduces it, and the process dies where watch mode would normally keep running.

@teemingc

teemingc commented Aug 7, 2026

Copy link
Copy Markdown
Member Author

Thought I could just make it rely on a single watcher and re-run everything based on that (this is how we used to do it). Turns out, Rolldown's watchers are smarter now with Vite environments, so if you only changed files related to the service worker entrypoint, only those files would rebuild.

Going to sleep on this and try to divide up the build steps logically tomorrow (though it's a bit of a waterfall right now). Some notes I jotted down roughly:

`vite build` or file changed

clear output
validate params
server build

    replace manifest placeholder variables
    read vite server build manifest
    write SSR manifest
    build server nodes
    analyse
    write client manifest
    check if client build is required

        check if there are universal/server load functions
        client build

            copy server assets to client
            read vite client build manifest
            find styles to inline
            replace manifest placeholder variables/strings
            client build data/ serverside resolution/ bundle
            rewrite ssr manifest
            rebuild server nodes
    
        if no client build, copy server assets to client

            prerender
            replace manifest placeholder variables/strings
            treeshake prerendered remote functions
            rerewrite ssr manifest
        
            let other buildApp hooks run
        
                service worker build

                    adapt

EDIT: see PR description

@teemingc
teemingc marked this pull request as ready for review August 10, 2026 15:29
@teemingc

Copy link
Copy Markdown
Member Author

/autofix

Comment thread packages/kit/src/exports/vite/index.js
Comment thread packages/kit/src/exports/vite/index.js
@Rich-Harris
Rich-Harris merged commit 69a5bdf into version-3 Aug 11, 2026
25 of 26 checks passed
@Rich-Harris
Rich-Harris deleted the fix-vite-build-watch branch August 11, 2026 14:32
Rich-Harris pushed a commit that referenced this pull request Aug 11, 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 version-3, this PR
will be updated.

⚠️⚠️⚠️⚠️⚠️⚠️

`version-3` is currently in **pre mode** so this branch has prereleases
rather than normal releases. If you want to exit prereleases, run
`changeset pre exit` on `version-3`.

⚠️⚠️⚠️⚠️⚠️⚠️

# Releases
## @sveltejs/kit@3.0.0-next.20

### Major Changes

- breaking: move remote function types to `$app/server`
([#16740](#16740))

- breaking: remove `#lib` definition from `paths`; requires explicit
module extensions as a result
([#16736](#16736))

- breaking: move hooks-related types to `@sveltejs/kit/hooks`
([#16737](#16737))

- breaking: move env-related types to `@sveltejs/kit/env`
([#16739](#16739))

### Minor Changes

- feat: better response logging
([#16744](#16744))

### Patch Changes

- chore: bump `mrmime` to 2.0.1
([#16745](#16745))

- chore: bump `@sveltejs/acorn-typescript` to 1.0.12
([#16745](#16745))

- chore: bump `magic-string` to 1.1.0
([#16745](#16745))

- chore: bump `devalue` to 5.9.0
([#16745](#16745))

- chore: bump `cookie` to 2.0.1
([#16745](#16745))

- chore: bump `acorn` to 8.18.0
([#16745](#16745))

- fix: avoid infinite loop when building with `--watch` flag
([#16632](#16632))
## @sveltejs/adapter-cloudflare@8.0.0-next.6

### Patch Changes

- chore: bump `@cloudflare/worker-types` to 5.20260809.1
([#16745](#16745))
- Updated dependencies
[[`1742811`](1742811),
[`1611c61`](1611c61),
[`b361b81`](b361b81),
[`b361b81`](b361b81),
[`b361b81`](b361b81),
[`b361b81`](b361b81),
[`13e7b18`](13e7b18),
[`529346d`](529346d),
[`b361b81`](b361b81),
[`81d6319`](81d6319),
[`b361b81`](b361b81),
[`69a5bdf`](69a5bdf)]:
  - @sveltejs/kit@3.0.0-next.20
## @sveltejs/adapter-netlify@7.0.0-next.8

### Patch Changes

- chore: bump `rolldown` to 1.2.3
([#16745](#16745))
- Updated dependencies
[[`1742811`](1742811),
[`1611c61`](1611c61),
[`b361b81`](b361b81),
[`b361b81`](b361b81),
[`b361b81`](b361b81),
[`b361b81`](b361b81),
[`13e7b18`](13e7b18),
[`529346d`](529346d),
[`b361b81`](b361b81),
[`81d6319`](81d6319),
[`b361b81`](b361b81),
[`69a5bdf`](69a5bdf)]:
  - @sveltejs/kit@3.0.0-next.20
## @sveltejs/adapter-node@6.0.0-next.10

### Patch Changes

- chore: bump `rolldown` to 1.2.3
([#16745](#16745))
- Updated dependencies
[[`1742811`](1742811),
[`1611c61`](1611c61),
[`b361b81`](b361b81),
[`b361b81`](b361b81),
[`b361b81`](b361b81),
[`b361b81`](b361b81),
[`13e7b18`](13e7b18),
[`529346d`](529346d),
[`b361b81`](b361b81),
[`81d6319`](81d6319),
[`b361b81`](b361b81),
[`69a5bdf`](69a5bdf)]:
  - @sveltejs/kit@3.0.0-next.20
## @sveltejs/enhanced-img@1.0.0-next.5

### Patch Changes

- chore: bump `magic-string` to 1.1.0
([#16745](#16745))

- chore: bump `zimmerframe` to 1.1.4
([#16745](#16745))
## @sveltejs/package@3.0.0-next.6

### Patch Changes

- chore: bump `svelte2tsx` to 0.7.59
([#16745](#16745))

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

vite build --watch regression

3 participants