Skip to content

feat(@angular/build): add manualRebuild option to the dev-server - #34742

Closed
DingWeizhe wants to merge 4 commits into
angular:mainfrom
DingWeizhe:feat/dev-server-manual-rebuild
Closed

DingWeizhe wants to merge 4 commits into
angular:mainfrom
DingWeizhe:feat/dev-server-manual-rebuild

Conversation

@DingWeizhe

Copy link
Copy Markdown

PR Checklist

PR Type

  • Feature

What is the current behavior?

ng serve either rebuilds on every file change (watch: true) or never rebuilds again after the initial build (watch: false). When many files are edited in a batch, for example by AI coding agents, the dev server rebuilds over and over while the edits are still in progress. The only other option is --watch=false followed by a restart, which throws away the in-memory incremental build state and means a full cold build every time.

Issue Number: #34743 (filed against angular/angular, but the implementation lives in this repo)

What is the new behavior?

@angular/build: new manualRebuild dev-server option

ng serve --manual-rebuild
  • The file watcher keeps running and change sets are merged in the background (add→remove becomes removed, remove→add becomes modified, and so on). Rebuilds no longer start on their own.
  • A rebuild of everything pending, reusing the existing esbuild/ngtsc incremental state, starts when one of these happens:
    • POST /__ng_rebuild is sent to the dev server. The response is sent after the rebuild finishes and the results have been pushed to clients:
      curl -X POST http://localhost:4200/__ng_rebuild
      # {"status":"success"}   (or "failure" | "no-changes" | "cancelled")
    • b is pressed in the terminal (a new Vite CLI shortcut).
  • Before a rebuild starts, the server waits until no file events have arrived for a short period (300 ms). This way edits made just before the request are still picked up by the watcher's debounce and included.
  • Requests that arrive while a rebuild is running are not resolved by that rebuild. They are handled by the next one, so a success response always covers every edit made before the request.
  • Browser requests carrying a cross-origin Origin header are rejected (403). Methods other than POST get a 405.
  • If watch is disabled, the option does nothing and a warning is logged.

Implementation notes:

  • utils/manual-rebuild.ts adds ManualRebuildController and waitForManualRebuilds(), which gates the watcher's change stream.
  • runEsBuildBuildAction takes an optional controller (as the internal application option manualRebuild) and resolves requests once a rebuild's results have been emitted.
  • The dev-server middleware sits at tools/vite/middlewares/rebuild-middleware.ts.

@angular/cli: MCP integration

  • devserver_start accepts manualRebuild: true, which starts ng serve --manual-rebuild.
  • New devserver_rebuild tool: an agent calls it once it has finished a batch of edits. It triggers the rebuild through the endpoint above, waits for it, and returns { status, logs }.

Does this PR introduce a breaking change?

  • No

The option defaults to false. Default behavior is unchanged.

Other information

  • Tests:
    • manual-rebuild_spec.ts (unit): change merging, request gating, the settle window, and cancellation.
    • dev-server/tests/options/manual-rebuild_spec.ts (integration): a change is not rebuilt until it is requested, and is served after the request. Also covers no-changes, 405 and 403.
    • devserver_spec.ts: the MCP start args and the rebuild tool.
  • Name and route (manualRebuild, /__ng_rebuild) are open to bikeshedding.
  • This PR does not update docs on angular.dev. I'm happy to send a follow-up if the API is accepted.
  • The pnpm-lock.yaml on main currently fails to parse (duplicate mapping key: minimatch@11.0.8), so I ran the tests locally with the previous commit's lockfile. This PR does not touch the lockfile.

🤖 Generated with Claude Code

d.weizhe and others added 2 commits November 18, 2026 11:02
When `manualRebuild` is enabled, the development server keeps tracking file
changes but no longer rebuilds automatically on every change. Instead, all
pending changes are accumulated and rebuilt incrementally (reusing the
in-memory build state) only when a rebuild is explicitly requested, either by
sending a `POST` request to the `/__ng_rebuild` endpoint or by pressing `b`
in the terminal.

The endpoint responds once the rebuild has completed with a JSON object
containing the outcome (`success`, `failure`, `no-changes` or `cancelled`).
Cross-origin browser requests to the endpoint are rejected.

This is useful when many files are edited in a batch (for example by AI
coding agents) to avoid repeated intermediate rebuilds without having to
restart the development server and lose the incremental build state.

Closes angular/angular#72079

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The `devserver_start` MCP tool now accepts a `manualRebuild` option which
starts `ng serve` with `--manual-rebuild`. A new `devserver_rebuild` tool
triggers an incremental rebuild of such a dev server once a batch of edits is
complete, waits for it to finish, and returns the build status and logs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@google-cla

google-cla Bot commented Nov 18, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request introduces a manual rebuild mode for the Angular development server, allowing file changes to be tracked but only rebuilt when explicitly requested via a POST request to /__ng_rebuild or by pressing 'b' in the terminal. This feature is supported by a new ManualRebuildController, a Vite middleware, and an MCP tool devserver_rebuild. The review feedback highlights several key improvements: ensuring pending manual rebuild requests are properly resolved as cancelled if the build loop is aborted, falling back to the :authority pseudo-header in the rebuild middleware to support HTTP/2 clients, and ensuring isBuilding() returns false if the devserver process has terminated.


// Reject cross-origin requests made by browsers to prevent arbitrary web pages from
// triggering rebuilds. Non-browser clients (e.g., curl or tooling) do not send an origin.
if (!isSameOrigin(req.headers.origin, req.headers.host)) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

In HTTP/2, the host header may be absent, and the :authority pseudo-header is used instead. To ensure compatibility with HTTP/2 clients, we should fall back to the :authority header when checking the origin.

    const host = req.headers.host ?? (req.headers[':authority'] as string | undefined);
    if (!isSameOrigin(req.headers.origin, host)) {

@alan-agius4

Copy link
Copy Markdown
Collaborator

Hi @DingWeizhe, thank you for putting together this PR and exploring an HTTP and MCP alternative to the file trigger approach in #33330.

Even without a sentinel file on disk, we do not plan to introduce a manual or paused rebuild mode to ng serve for several architectural reasons:

  1. Watch graph and event buffering complexity: The build watcher dynamically tracks watchFiles discovered from the previous compilation. Suspending builds and coalescing raw watcher events in memory over long intervals introduces subtle edge cases around newly created files not yet in the dependency graph, file renames, and error recovery.
  2. Dev server control endpoints and API surface: Exposing mutating HTTP control endpoints (POST /__ng_rebuild), dedicated builder schema options (manualRebuild), and specialized MCP tools adds significant maintenance and security surface area across @angular/build and @angular/cli for an external orchestration concern.

For automated or agent workflows making bursts of edits, we recommend one of the following:

  • Configuring a larger poll interval (for example 10000 ms) to throttle how frequently file changes are sampled and batched into incremental rebuilds while keeping the compiler state warm in memory.
  • Using the existing devserver_wait_for_build MCP tool alongside standard watch debouncing.
  • Running ng build (or the run_target MCP tool) once a batch of edits is complete, with persistent disk caching enabled.

Because of this, we will be closing this PR and the associated issue. Thank you again for your time and contribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants