Skip to content

fix(flame): restart the dev server when a workspace package rebuilds - #427

Merged
gitfromwildan merged 1 commit into
mainfrom
feat/flame-dev-restart-on-markdown
Oct 26, 2026
Merged

gitfromwildan merged 1 commit into
mainfrom
feat/flame-dev-restart-on-markdown

Conversation

@gitfromwildan

@gitfromwildan gitfromwildan commented Oct 26, 2026 •

Copy link
Copy Markdown
Member

Summary

flame dev was meant to pick up changes to sibling packages (@docubook/markdown and others) while it runs. That never worked. This PR makes it work end to end: rebuild a workspace package and the dev server restarts by itself, so server rendering and the browser bundle load the new code together. Open pages reload once the server answers again.

Projects that install flame from npm are unaffected: they keep a single-process dev server.

Why the old watcher never worked

The watcher was added in #347 (resolveMdxContentSrc() in server.ts and server.impl.ts) and had three problems:

  1. It never started. It called require.resolve("@docubook/markdown/package.json"), which throws ERR_PACKAGE_PATH_NOT_EXPORTED, because markdown's exports lists only . and ./styles.css.
  2. It watched the wrong files. It watched markdown/src, but flame imports the package from dist/ (exports["."]). Rebuilding flame's client bundle after a src edit therefore changed nothing.
  3. It rebuilt only the client bundle. Server rendering (mdx.ts) keeps the modules it imported at startup. A client-only rebuild would have mixed old SSR HTML with a new client, causing hydration mismatches.

What this PR does

1. Restart on rebuild: .docu/node/dev-watch.ts and bin/dev-supervisor.mjs

  • resolveWorkspacePackages() resolves @docubook/core, markdown, ui-react and themes-colors from their main entry, so no package.json export is needed. It walks up to the package root and skips installed copies under node_modules.
  • watchWorkspacePackages() watches each package's dist/ (recursively) and its root styles.css. After a 400 ms quiet period it compares a content fingerprint (SHA-256 over the built JS/CSS):
    • identical rebuilds are ignored (verified: vite build --watch starting up, and a whitespace-only source edit, cause no restart)
    • a half-written dist/ (entry missing) is retried later rather than triggering a restart
  • On a real change, both dev servers (server.ts for Bun, server.impl.ts for Node/Deno) exit with code 75.
  • flame dev runs the server under bin/dev-supervisor.mjs, which respawns it on code 75, forwards SIGINT/SIGTERM, and never restarts after a stop signal. The supervisor is only enabled when a sibling package is linked from the workspace. FLAME_DEV_SUPERVISE=0 opts out (it then logs "restart flame dev to load it"), and =1 forces the supervisor on.

2. HMR reconnect

Previously the browser reloaded 2 s after the HMR connection dropped, whether or not the server was back. A restart that takes longer (a cold client build here took up to 13 s) left the page on a connection error. The client now polls HEAD with backoff (250 ms up to 2 s) and reloads once the server answers.

3. Workspace wiring

  • core, markdown, themes-colors: new dev script, vite build --watch --emptyOutDir=false.
  • ui-react: its existing vite build --watch gains --emptyOutDir=false. A plain watch build empties dist/ and deletes the tsc declarations, which broke flame's typecheck while dev was running (reproduced here).
  • turbo.json: dev now depends on ^build.
  • Root pnpm dev: turbo dev --filter=@docubook/flame....
  • flame dev script: bun bin/cli.js dev (through the CLI, so the supervisor applies) instead of bun .docu/node/server.ts.
  • CONTRIBUTING.md and ARCHITECTURE.md document the workflow.

Verification

End-to-end, real processes, on Node and on Bun. Supervised flame dev, markdown's vite build --watch, and a Chromium page kept open throughout:

Step Node Bun
flame dev starts supervised; the child gets FLAME_DEV_SUPERVISED=1 ✅ ✅
vite build --watch initial build (identical output) 0 restarts 0 restarts
Whitespace-only edit in TabsMdx.tsx (identical output) 0 restarts 0 restarts
Temporary data-dev-probe attribute on the Tabs root 1 restart; SSR HTML and client bundle both contain it same
The open page reloads by itself after the restart 2 loads, probe in DOM, hydrated, 0 console errors same
Revert the edit 1 restart; SSR and client clean again same
SIGINT to flame dev supervisor and child exit, port freed same

The temporary edit was reverted, and git diff on packages/markdown/src is empty afterwards.

  • FLAME_DEV_SUPERVISE=0: no child process, and on a rebuild it logs the hint instead of restarting.
  • Root pnpm dev: builds the four dependencies, starts four watchers plus flame, and serves 200, with 0 restarts during startup.
  • @docubook/flame: 33 files, 666 tests passed (14 new). tsc --noEmit reports 0 errors, and lint and prettier pass.
    • dev-watch.test.ts: workspace vs installed resolution, fingerprint (identical, changed, mid-rebuild, styles.css), the watcher (one callback per real change, none for identical or half-written output), and the constants contract with the supervisor. Ran 10 times, 0 flakes.
    • dev-supervisor.test.ts: respawn on 75, the supervised env flag, passthrough of other exit codes, no restart after SIGTERM (fails without the !stopping guard), and workspace detection.
    • server-routing.test.ts: the HMR client runs against a fake EventSource, fetch and timers. It reloads exactly once, only after the server answers, and fails against the old script.
  • Library packages typecheck. pnpm test:release-plan passed (6 scenarios).
  • Deno not run locally (Deno is not installed). The supervisor starts deno run -A <entry> for it, and CI's Deno job only builds.

Review follow-up (pullfrog)

The recursive watcher on a sibling's dist/ dies silently when that directory is deleted and recreated.

Correct. fs.watch is bound to the directory it was opened on. The build/clean scripts of core, markdown and themes-colors run rm -rf dist, so a full rebuild of a sibling with identical output (no restart) left a dead watcher, and later real rebuilds never restarted the server. My first end-to-end check missed this because vite build --watch keeps the dist/ directory.

Fixed in this PR's single commit (the follow-up was squashed in). The package-root watcher already sees dist being removed or created, and now re-attaches the dist/ watcher at that point (and on a watcher error). A new test removes and recreates dist/ with identical output (no change reported), then writes a real change (reported). It fails without the re-attach and passed 10 of 10 runs.

End to end on Node: with supervised flame dev running, pnpm --filter @docubook/markdown build (rm -rf dist, identical output) caused 0 restarts, and the following real change through vite build --watch caused 1 restart with SSR and client both updated.

Notes for reviewers

  • Under pnpm exec/pnpm run, NODE_PATH points at the workspace store, so resolution falls back there. For an installed project that store is its own node_modules, so detection still says "installed". One test fixture had to account for this.
  • themes-colors also exports passthrough themes/* and syntax/* files outside dist/. Edits to those are not watched; only dist/ and styles.css are.
  • The supervisor constants (exit code 75, env flag, package list) are duplicated between bin/dev-supervisor.mjs, which the CLI loads as plain JS, and dev-watch.ts. A test asserts that they match.

Changeset

@docubook/flame: patch. The library packages only gain dev scripts, so they need no changeset.

🤖 Generated with Claude Code

@vercel

vercel Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated
docu Ignored Ignored Preview Oct 26, 2026 1:55pm UTC

@pullfrog pullfrog Bot left a comment

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.

Important

The recursive watcher on a sibling's dist/ dies silently when that directory is deleted and recreated — which core, markdown and themes-colors do on every build/clean (rm -rf dist). After a full rebuild with identical output, later real changes to that package no longer restart the dev server. Reproduced against dev-watch.ts; details inline.

Reviewed changes — the full diff at 895839c: the new restart-on-rebuild path, the supervisor, the HMR reconnect script, workspace wiring and tests.

  • dev-watch.ts — resolves the four @docubook/* siblings from their main entry (skipping node_modules copies), fingerprints dist/**/*.{js,mjs,css} + styles.css, and debounces fs.watch events into one onChange per real content change.
  • server.ts / server.impl.ts — the dead resolveMdxContentSrc() watcher is removed; a supervised server exits with code 75 on a package change, an unsupervised one logs a hint.
  • bin/dev-supervisor.mjs + cli.js — flame dev is respawned on exit 75 only when a sibling is workspace-linked; FLAME_DEV_SUPERVISE=0/1 overrides, SIGINT/SIGTERM are forwarded and end supervision.
  • HMR client (html.ts, html.shared.ts) — on SSE error it polls HEAD with backoff and reloads once the server answers, instead of a fixed 2 s reload. Both servers build the client bundle before listening, so the probe only succeeds when the new process is actually ready.
  • Workspace wiring — vite build --watch --emptyOutDir=false dev scripts, turbo dev → ^build, root pnpm dev, docs and a flame patch changeset.

Verified locally under Node 24: the three new/changed test files pass (74 tests), tsc --noEmit and oxlint are clean, and Vite's CLI does honor --emptyOutDir=false (a sentinel file in dist/ survives it and is wiped by a plain vite build).

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using claude-fable-5-1 | 𝕏

Comment thread packages/flame/.docu/node/dev-watch.ts Resolved
@gitfromwildan gitfromwildan self-assigned this Oct 26, 2026
gitfromwildan added a commit that referenced this pull request Oct 26, 2026
…ated

A recursive fs.watch is bound to the directory it was opened on. The
build and clean scripts of core, markdown and themes-colors run
`rm -rf dist`, so after a full rebuild of a sibling package (identical
output, no restart) the old dist/ watcher went silent and later real
rebuilds never restarted the dev server (pullfrog review on #427).

The package-root watcher already sees `dist` being removed or created;
re-attach the dist/ watcher then, and on a watcher error. Covered by a
test that removes and recreates dist/ before a real change.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@gitfromwildan
gitfromwildan force-pushed the feat/flame-dev-restart-on-markdown branch from fdae301 to 2f2ea88 Compare October 26, 2026 13:53
The dev server's component watcher never started: it resolved
@docubook/markdown/package.json, which the package does not export
(ERR_PACKAGE_PATH_NOT_EXPORTED). It also watched markdown/src, while
flame loads the package from dist/, and only rebuilt the client bundle
while server rendering kept the modules it loaded at startup.

- .docu/node/dev-watch.ts resolves workspace-linked @DocuBook packages
  (core, markdown, ui-react, themes-colors) from their main entry, skips
  installed copies, and watches dist/ plus styles.css. A content
  fingerprint ignores identical rebuilds and half-written output. The
  dist/ watcher is re-attached when dist/ is recreated, since build and
  clean scripts `rm -rf dist`.
- On a real change the Bun and Node/Deno dev servers exit with code 75.
  bin/dev-supervisor.mjs, used by `flame dev` only when a sibling
  package is linked from the workspace, starts a fresh server.
  FLAME_DEV_SUPERVISE=0/1 opts out or forces it.
- The HMR client now waits for the server to answer before reloading
  once, instead of reloading after a fixed 2 seconds.
- Library packages get `dev: vite build --watch --emptyOutDir=false`
  (keeps tsc declarations in dist; ui-react aligned), turbo dev depends
  on ^build, root `pnpm dev` runs flame with its dependencies, and
  flame's own dev script goes through the CLI.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@gitfromwildan
gitfromwildan force-pushed the feat/flame-dev-restart-on-markdown branch from 2f2ea88 to 40be040 Compare October 26, 2026 13:54

@pullfrog pullfrog Bot left a comment

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.

✅ No new issues found.

Reviewed changes — the delta since the prior review at 895839c (force-pushed into the same single commit, now 40be040, which is the same content rebased onto main): the fix for the dead dist/ watcher and its regression test.

  • Re-attached the dist/ watcher on removal/recreation — watchWorkspacePackages() keeps the recursive watcher in a per-package distWatcher; attachDist() closes and re-opens it when the root watcher reports dist (and on a watcher error), swallowing a watch() throw if dist/ vanished again. The root watcher now closes itself on error instead of crashing the process, and watchers became a Set so re-attached watchers are still closed by the stop function.
  • Added the remove-and-recreate regression test — rm -rf dist → identical rewrite → no change reported; a real change afterwards → one change.

Verified under Node 24: dev-watch.test.ts passed 5/5 runs, dev-supervisor + server-routing tests pass, tsc --noEmit and oxlint are clean. A direct fs.watch probe confirms the design assumptions: writes inside dist/ emit nothing on the non-recursive root watcher (so attachDist() does not churn during normal vite build --watch rebuilds), while rm -rf dist and mkdir dist each surface as exactly one rename:dist there — which is the event the re-attach keys on. The prior review thread is resolved.

Pullfrog  | View workflow run | Using claude-fable-5-1 | 𝕏

@pullfrog pullfrog Bot left a comment

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.

✅ Pullfrog addressed all of its review feedback on this PR.

No code changes were needed. The single thread in review 5652982979 (the dist/ watcher dying after rm -rf dist) is already fixed on the PR head 40be040: attachDist() in dev-watch.ts re-attaches the recursive watcher on the package root's dist event and on watcher error, and the "keeps reporting changes after a full rebuild recreates dist/" regression test is present and passing. The thread was already resolved; I left a short reply pointing at the current commit since the previously referenced SHA (2f2ea88) was squashed away.

Verification: npx vitest run .docu/__tests__/dev-watch.test.ts — 8 passed, 1 failed. The failure is resolves flame's sibling packages in this monorepo, which needs the sibling packages' dist/ built (not built in this shell) and is unrelated to the review thread.

Pullfrog  | View workflow run | Using claude-fable-5-1 | 𝕏

@gitfromwildan
gitfromwildan merged commit ee4e10e into main Oct 26, 2026
16 checks passed
@gitfromwildan
gitfromwildan deleted the feat/flame-dev-restart-on-markdown branch October 26, 2026 14:01
@github-actions github-actions Bot mentioned this pull request Oct 26, 2026
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