Skip to content

feat(vite): restart the app when the Vite dev server asks for it - #6208

Open
edusperoni wants to merge 2 commits into
mainfrom
feat/vite-restart-ipc
Open

edusperoni wants to merge 2 commits into
mainfrom
feat/vite-restart-ipc

Conversation

@edusperoni

@edusperoni edusperoni commented Nov 19, 2026 •

Copy link
Copy Markdown
Collaborator

PR Checklist

What is the current behavior?

The CLI spawns vite serve (the Vite HMR dev server) with stdio: "inherit", so the dev server has no channel back to the CLI. Some changes can't be applied as a hot update, and the dev server has no way to get the app process restarted for them.

In @nativescript/vite thin client mode (NativeScript/NativeScript#11650), most edits are applied in place: a hot update, or a soft reload that re-evaluates the app's own modules inside the running process. Some changes need a fresh process instead:

  • edits to code that is evaluated once per process and never re-evaluated by a soft reload (node_modules, linked core, the dev client);
  • a vite.config change, after which Vite restarts its server;
  • a module that called import.meta.hot.decline();
  • a failed in-process teardown.

What is the new behavior?

vite serve is spawned with an IPC channel: stdio: ["inherit", "inherit", "inherit", "ipc"]. Its output still goes straight to the terminal.

Contract. The dev server may send this message over that channel:

process.send({ type: "nativescript:restart", reason: "src/polyfills.ts changed" });

reason is a human-readable string. One message may stand for several requests that the dev server already collapsed. Any other message shape is ignored without a log, so newer @nativescript/vite versions can add message types without breaking this CLI.

Flow.

  1. BundlerCompilerService receives the message and debounces per platform. Requests that arrive within 300 ms collapse into one, with their distinct reasons joined by ; . It then emits the new BUNDLER_APP_RESTART_REQUESTED event with { projectDir, platform, reason }.
  2. RunController subscribes to that event in run() and unsubscribes in stop(). It looks up the session's devices on that platform, logs Restarting the app on <platform>: <reason> at info level, and calls the existing restartApplication({ projectDir, deviceIdentifiers }). That is the same path the r key shortcut uses: nothing is prepared, built or synced; it waits behind any sync or build already queued on the session's action chain; it goes through refreshApplication, so debug sessions get their debugger re-attached; and errors are reported per device. If the session has no device on that platform, nothing is restarted.
  3. Stopping the dev server, whether on session stop or a file-watcher pause, drops its message listener and any pending debounced request.

Backwards compatibility.

  • Older @nativescript/vite versions never send anything over the channel, so behaviour is unchanged. The only process.send use in released versions is the nativescript-cli-integration plugin, which is apply: 'build' and doesn't run under vite serve. Vite itself doesn't use the channel.
  • The channel doesn't change process lifetime. The serve child is still registered with the cleanup service and stopped through terminate() as before. A child with no message listener exits normally, and the parent exits once the child does. The debounce timer is unref'd.
  • Webpack, and Vite runs without HMR or without watch, don't start vite serve, so nothing changes for them.

Tests.

  • Bundler compiler service: the spawn options include IPC; a restart message emits the event; requests close together collapse into one; unknown messages are ignored, including the build watcher's emittedFiles shape; a pending request is dropped when the dev server stops.
  • Run controller: a request restarts only the session's devices on that platform; a request for a platform with no session device restarts nothing; a request waits for the action already running; the listener is removed when the session stops.

Related to NativeScript/NativeScript#11650.

Summary by CodeRabbit

  • New Features
    • Vite development-server restart requests now restart matching devices in the active session. Repeated requests are combined, and requests for other platforms are ignored.
  • Bug Fixes
    • Restart requests are discarded when the development server stops, and errors are logged without interrupting the run.

@coderabbitai

coderabbitai Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The compiler service now receives Vite restart messages, combines requests by platform, and emits restart events. During a live-sync run, RunController uses each event to restart matching session devices and removes its listener when the session stops.

Changes: Vite restart event flow

Layer / File(s) Summary
Vite restart message handling and event emission
lib/services/bundler/bundler.ts, lib/constants.ts, lib/services/bundler/bundler-compiler-service.ts, test/services/bundler/bundler-compiler-service.ts
The bundler restart request type and event are added. The compiler service handles nativescript:restart IPC messages, debounces requests by platform, combines distinct reasons, and cancels pending requests when the server stops. Tests cover message validation, emitted request data, stdio, debouncing, and cancellation.
Live-sync device restart handling
lib/controllers/run-controller.ts, test/controllers/run-controller.ts
RunController listens for restart events and restarts session devices matching the requested platform. It skips requests without a matching device, waits for the session action chain, catches restart errors, and removes the listener when the session stops.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Feature

Sequence Diagram(s)

sequenceDiagram
  participant ViteDevServer
  participant BundlerCompilerService
  participant RunController
  ViteDevServer->>BundlerCompilerService: Send nativescript:restart IPC message
  BundlerCompilerService->>BundlerCompilerService: Debounce and combine reasons by platform
  BundlerCompilerService->>RunController: Emit restart request event
  RunController->>RunController: Restart matching session device IDs
Loading

Possibly related PRs

Suggested reviewers: nathanwalker, fatme

Merge Risk: Minimal · up to 8bb8f

This change lets the Vite dev server ask the CLI to restart the app on the affected platform, and the CLI handles that request with a short debounce. The behavior is limited to Vite runs that start vite serve, and the author reports an iOS simulator test that passed. The reviewed code shows no merge-blocking risk.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title "feat(vite): restart the app when the Vite dev server asks for it" states the main change. The PR adds an IPC channel from the Vite dev server to the CLI. The CLI debounces the dev server's …
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title "feat(vite): restart the app when the Vite dev server asks for it" states the main change. The PR adds an IPC channel from the Vite dev server to the CLI. The CLI debounces the dev server's restart messages per platform and emits BUNDLER_APP_RESTART_REQUESTED. RunController then restarts the session's devices on that platform. The title matches this behavior and is specific enough for a reader scanning history.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

A rabbit hears Vite’s restart call,
And sends the news along the hall.
The platform waits; the reasons blend,
Then matching devices restart again.
When live-sync stops, the listener’s gone,
And quiet returns before the dawn.

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 62120216-4440-4498-b0b9-3349af2989d4
Commits

Reviewing files that changed from the base of the PR and between 313f1c6 and 8bb8f95.

Files selected for processing: 6

  • lib/constants.ts
  • lib/controllers/run-controller.ts
  • lib/services/bundler/bundler-compiler-service.ts
  • lib/services/bundler/bundler.ts
  • test/controllers/run-controller.ts
  • test/services/bundler/bundler-compiler-service.ts

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.

@edusperoni

Copy link
Copy Markdown
Collaborator Author

Verified end to end on an iOS 26.3 simulator. The Angular 22 fixture from NativeScript/NativeScript#11650 ran through this branch's CLI (NS_CLI_NO_LOCAL=1, so the project-local CLI doesn't take over):

  • Restart on request: touching vite.config.mts restarts Vite, and on reconnect @nativescript/vite sends { type: 'nativescript:restart', reason: 'the Vite dev server restarted' }. The CLI logs Restarting the app on ios: the Vite dev server restarted and restarts the app (pid 37412 → 38120). Angular boots again and the app reconnects to the same dev server.
  • HMR after the restart: a template edit applies in place, with no further restart.
  • No IPC, old behaviour: without this branch (the released CLI, so no IPC channel) the dev server falls back to its terminal line plus an on-device "Restart required" notice, as before.

`vite serve` is now spawned with an IPC channel. When it sends
`{ type: 'nativescript:restart', reason }`, the CLI logs the reason and
restarts the app on that platform's devices in the running session,
without rebuilding or syncing. Requests that arrive within 300 ms
collapse into one restart. The restart joins the session's action chain,
so it runs after any sync or build already in progress. It reuses
RunController.restartApplication, which keeps debug sessions attached.

Other message shapes are ignored, and dev servers that never send one
behave as before.
Its stdin depends on whether the CLI's key shortcuts will read the terminal, which the startViteDevServer specs pin. The spawn comment no longer claims a silent server behaves as with plain inherit.
@edusperoni
edusperoni force-pushed the feat/vite-restart-ipc branch from acb5ec6 to 8bb8f95 Compare December 4, 2026 01:56
@edusperoni
edusperoni marked this pull request as ready for review December 4, 2026 02:01

This branch has not been deployed

No deployments
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.

2 participants