Skip to content

fix(@angular/build): keep dev-server Vite cache worktree-local - #33976

Open
clydin wants to merge 1 commit into
angular:mainfrom
clydin:fix-worktree-dev-server-cache
Open

fix(@angular/build): keep dev-server Vite cache worktree-local#33976
clydin wants to merge 1 commit into
angular:mainfrom
clydin:fix-worktree-dev-server-cache

Conversation

@clydin

@clydin clydin commented Aug 31, 2026

Copy link
Copy Markdown
Member

When running within a Git worktree, the persistent build cache is shared across worktrees by resolving relative to the common Git directory (main repository root). While this works for location-agnostic compilation caches (such as ng build's SourceFileCache), it breaks ng serve when server-side rendering (SSR) is enabled.

In SSR mode, Vite prebundles dependencies into deps_ssr/. Any external or unbundled dependencies inside those prebundles contain bare imports (e.g., firebase-admin/data-connect). Because Node.js resolves bare imports relative to the prebundled file's location, placing cacheDir under the main repository root causes Node.js to resolve imports against the main repository's node_modules instead of the worktree's node_modules, resulting in Cannot find module errors and HTTP 500 responses.

To resolve this, NormalizedCachedOptions now exposes localBasePath and localPath which always resolve relative to the active workspace root. The dev-server's Vite configuration now uses localPath for cacheDir, ensuring prebundles remain within the worktree hierarchy where Node.js can resolve the worktree's dependencies. Additionally, cache purging and watcher ignore lists are updated to handle both shared and local cache paths when they diverge.

Fixes #33968

@clydin clydin added the target: patch This PR is targeted for the next patch release label Aug 31, 2026

@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 workspace-local cache options (localPath and localBasePath) to support environments like Git worktrees where the cache should resolve relative to the current worktree root rather than the main repository root. It updates the build watcher, Vite dev server, cache normalization, and cache purging logic to handle these new local paths. A critical bug was identified in the cache purging logic (purge-cache.ts) where an undefined localPath could fall back to path, leading to the accidental deletion of the active local cache directory. A fix was suggested to skip purging if currentPath is not defined.

Comment thread packages/angular/build/src/utils/purge-cache.ts Outdated
When running within a Git worktree, the persistent build cache is shared
across worktrees by resolving relative to the common Git directory (main
repository root). While this works for location-agnostic compilation
caches (such as `ng build`'s SourceFileCache), it breaks `ng serve` when
server-side rendering (SSR) is enabled.

In SSR mode, Vite prebundles dependencies into `deps_ssr/`. Any external
or unbundled dependencies inside those prebundles contain bare imports
(e.g., `firebase-admin/data-connect`). Because Node.js resolves bare imports
relative to the prebundled file's location, placing `cacheDir` under the
main repository root causes Node.js to resolve imports against the main
repository's `node_modules` instead of the worktree's `node_modules`,
resulting in `Cannot find module` errors and HTTP 500 responses.

To resolve this, `NormalizedCachedOptions` now exposes `localBasePath`
and `localPath` which always resolve relative to the active workspace root.
The dev-server's Vite configuration now uses `localPath` for `cacheDir`,
ensuring prebundles remain within the worktree hierarchy where Node.js can
resolve the worktree's dependencies. Additionally, cache purging and
watcher ignore lists are updated to handle both shared and local cache
paths when they diverge.

Fixes angular#33968
@clydin
clydin force-pushed the fix-worktree-dev-server-cache branch from 28af968 to 35af069 Compare August 31, 2026 14:29
@clydin clydin added the action: review The PR is still awaiting reviews from at least one requested reviewer label Aug 31, 2026
@clydin
clydin requested a review from alan-agius4 August 31, 2026 14:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

action: review The PR is still awaiting reviews from at least one requested reviewer area: @angular/build target: patch This PR is targeted for the next patch release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ng serve with SSR is broken in every git worktree since 22.1.0 (shared cache resolves deps_ssr outside the worktree)

1 participant