Skip to content

build(desktop): keep dist mtimes stable so unchanged embeds skip a full Rust rebuild - #566

Merged
huichen merged 2 commits into
mainfrom
claude/desktop-dist-mtime
Sep 12, 2026
Merged

huichen merged 2 commits into
mainfrom
claude/desktop-dist-mtime

Conversation

@huichen

@huichen huichen commented Sep 12, 2026

Copy link
Copy Markdown
Collaborator

Problem

On Windows, make install (or any tauri build) spent ~130s recompiling the desktop crate even when nothing changed at all — no source edit, no frontend edit, warm target/.

Measured before the fix (make install on a warm tree, no changes):

step before
CLI cargo 0.27s
npm + Vite + worker check ~6s
desktop cargo ~130s
skills update ~2s
total 144.6s

Root cause

npm run build runs Vite, and Vite rewrites every file under desktop/dist/. Tauri embeds dist/ into the desktop crate (tauri::generate_context! expands to include_bytes! for every asset), and cargo invalidates that crate whenever an embedded file's mtime changes.

Measured on a no-op build:

before files=380   after files=380
identical content + same mtime : 0
identical content + NEW mtime  : 380   <-- needless invalidation
different content              : 0

So the desktop crate (lib + Thin-LTO link of the 35MB binary) was rebuilt on every install, even when every embedded byte was unchanged. Cargo stays clean (0.5s) when dist mtimes are left alone.

Fix

npm run build now runs scripts/stabilize-dist-mtimes.mjs right after Vite: for every output whose bytes are unchanged it writes the recorded timestamp back, so cargo sees an untouched frontend.

  • Timestamps only, never content — a stale dist/ cannot be embedded. A changed asset keeps its fresh mtime and the crate rebuilds as it must.
  • State lives in desktop/node_modules/.cache (gitignored). CI and fresh clones have none, so their first build behaves exactly as before; the pass is also a no-op when dist/ is missing.
  • utimes round-trips through seconds-as-double with ~400ns error, so the pass stores the timestamp actually on disk after writing and re-applies that value; pushing a value through the conversion a second time is a fixed point, which is what keeps cargo's fingerprint stable.

Verification

Desktop JS checks on the merged tree: tsc --noEmit, eslint "src/**/*.{ts,tsx}", eslint scripts/stabilize-dist-mtimes.mjs, stylelint, vitest run (877 tests) — all pass.

Stabilizer behavior (worktree, real builds):

  • run 1 (no state): 380 changed/new; runs 2-3: 380 unchanged (mtime preserved) with byte-identical content and identical mtimes.
  • editing desktop/index.html: 379 unchanged, 1 changed/new and exactly that asset kept a fresh mtime — a real change still invalidates the crate.
  • reverting the edit: 1 changed/new; the following no-op build: 380 unchanged.

End-to-end make install on the merged tree (#3-#5 are steady state, after the one-time normalization plus a genuine CLI rebuild caused by the new commit hash):

run total desktop cargo
#1 (first after the change) 203.9s 2m09s
#2 144.8s 2m08s
#3 13.5s 0.46s
#4 15.9s 0.42s
#5 13.2s 0.40s

Steady-state make install therefore drops from ~145s to ~13s; the remainder is npm (~6s), Vite (~1.7s), copies and the skills update. Further wins are available separately (skip npm install on Windows when manifests are unchanged, avoid the network skills update on a plain install), but the desktop rebuild was ~90% of the cost.

…ll Rust rebuild

Vite rewrites every file under `dist/` on each build, so byte-identical outputs come out with brand-new mtimes. Tauri embeds `dist/` into the desktop crate (`tauri::generate_context!` -> `include_bytes!`), and cargo invalidates that crate whenever an embedded file's mtime changes. Measured on Windows: after a `vite build` with no source or frontend change, 380/380 dist files were byte-identical but 380/380 carried a new mtime, and the following no-op cargo build recompiled the crate for 130-133s.

`npm run build` now runs `scripts/stabilize-dist-mtimes.mjs` after Vite and writes the recorded timestamp back for outputs whose bytes did not change. Timestamps only, never content, so a stale `dist/` cannot be embedded: a changed asset keeps its fresh mtime and the crate rebuilds as it must (verified: editing `index.html` moved exactly that one asset's mtime, 379 preserved). With the pass in place the no-op cargo build is 0.5s and stays there across runs.

State lives in `desktop/node_modules/.cache` (gitignored); CI and fresh clones have none, so their first build behaves exactly as before.
@huichen
huichen enabled auto-merge (squash) September 12, 2026 04:44
@huichen
huichen merged commit 26bea98 into main Sep 12, 2026
12 checks passed
@huichen
huichen deleted the claude/desktop-dist-mtime branch September 12, 2026 04:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant