Skip to content

Vite symlink-GVS serving compat (#315) - #318

Merged
colinhacks merged 5 commits into
mainfrom
vite-build
Jul 5, 2026
Merged

colinhacks merged 5 commits into
mainfrom
vite-build

Conversation

@colinhacks

Copy link
Copy Markdown
Contributor

Vite dev servers 403 store-resident modules under nub's machine-global virtual store (~/.cache/nub/pm/virtual-store, outside the project root): 403 … is outside of Vite serving allow list. Vite realpath-checks every /@fs-served module against server.fs.allow (default [workspaceRoot]), and a store-resident dep served raw (framework client entries, dev-toolbar modules, SSR/optimizeDeps-excluded deps) falls outside it. pnpm never hits this — its virtual store is project-local, under the workspace root.

The fix

One mechanism, PM-agnostic, works-without-nub, CAS-pristine — default-on, NUB_VITE_COMPAT=0 opts out. Gated on Vite being in the graph.

  • Unit A — node_modules/.modules.yaml. On install, whenever Vite is anywhere in the graph, write JSON {"virtualStoreDir":"<abs store>"} at the workspace root. Vite 8.1+ reads it natively (fix: apply correct fs restrictions for pnpm gvs vitejs/vite#22415) and allows the store. Covers direct and library-embedded Vite.
  • Unit B — dist backport for Vite < 8.1. For a directly-declared Vite, the linker force-materializes it project-local (the shared CAS store is untouched — only the ejected copy is modified), and nub codegen-inserts Vite's own 8.1 .modules.yaml sniff at its fs.allow-default site (let allowDirs = server.fs.allow; for v6/v7, [searchForWorkspaceRoot(root)] for v5). The inserted sniff is YAML-tolerant and PM-agnostic — it reads whatever virtualStoreDir any tool wrote; nub's store path lives only in the data file, never in the patch. Idempotent, fail-open (a missing anchor is a no-op, never a broken Vite).

The fix lives on disk in node_modules and injects nothing at runtime, so it works whether or not nub is in the process — a teammate on plain Vite, or CI, gets the same result.

Library-embedded frameworks

A framework like Astro is store-resident and loads its Vite through a store-to-store sibling symlink — the shared store copy, not the project-local ejected copy — so the dist backport can't reach it CAS-safely. Unit A covers those for Vite ≥ 8.1 (the store Vite reads .modules.yaml natively), which is the reported #315 shape: Astro 7 + @astrojs/react pins Vite 8.1.3. A framework pinning Vite < 8.1 that does not set its own fs.allow (VitePress already does) is the remaining gap — it shrinks as the ecosystem moves to Vite 8.1+; closing it would mean patching the shared store copy, a separate call.

Validation

Matrix harness under tests/vite-compat/ (README + driver). Verified against real installs:

case Vite result
Astro 7 + @astrojs/react (the #315 repro) 8.1.3 @astrojs/react/dist/client.js via /@fs 403→200, works-without-nub (npx astro dev)
create-vite ×7 (react/vue/svelte/solid/preact/lit/vanilla) 8.1.3 pass (native sniff)
SvelteKit, Storybook (Vite builder) 8.1.3 pass (native)
VitePress 5.4.21 pass (self-mitigates)
direct-dep vite dev, bare CLI, no nub 5.4.21 / 6.4.3 / 7.3.6 403→200, backport applied
NUB_VITE_COMPAT=0 7.3.6 no .modules.yaml, no eject, byte-identical default
install ×2 7.3.6 second is "Already up to date" — no re-link, patch not doubled

Closes #315

Write node_modules/.modules.yaml and backport Vite's own fs.allow sniff
into ejected <8.1 vite so the dev server serves nub's global virtual
store. Default-on, NUB_VITE_COMPAT=0 opt-out. Works-without-nub, CAS
pristine.

Claude-Session: https://claude.ai/code/session_01Xuh9u8b93eMNu53fTQTWX7
The first cut only detected a top-level node_modules/vite, missing the
primary #315 case: library-embedded frameworks (Astro/SvelteKit/VitePress)
whose vite is a transitive dep with no top-level symlink. apply() now
discovers vite across top-level + node_modules/.nub/vite@* and writes
.modules.yaml whenever vite is anywhere in the graph. Force-materialize
is scoped to a directly-declared vite (the only shape whose loaded vite is
the ejected copy the backport patches); a library-embedded framework loads
its vite from the shared store, covered by Unit A for Vite >= 8.1.

Also fixes the matrix driver (require() cannot load a .yaml file;
transitive-vite detection; realpath eject classification).

Claude-Session: https://claude.ai/code/session_01Xuh9u8b93eMNu53fTQTWX7
…obber (#315)

Self-review (correctness + impact-analysis) surfaced four issues, all fixed:

- P0 (Linux/Windows CAS corruption): the ejected dist files are HARDLINKS to
  the content-addressed store, so an in-place fs::write (O_TRUNC) truncated the
  shared inode and corrupted the global store (masked on macOS by copy-on-write
  materialization). patch_one now writes a temp sibling + atomic rename, severing
  the hardlink so the CAS blob is never touched. Adds a hardlink-integrity
  regression test that fails if the in-place write ever returns.
- Vite compat now runs on nub add / nub update, not only nub install.
- write_modules_yaml refuses to clobber a foreign (pnpm) .modules.yaml state
  file — writes only when absent or over its own single-key JSON stub.
- Root resolution targets the dir that actually holds node_modules, so a stray
  ancestor lockfile can't point the writer at a node_modules-less parent.

Claude-Session: https://claude.ai/code/session_01Xuh9u8b93eMNu53fTQTWX7
Copilot AI review requested due to automatic review settings July 5, 2026 19:20
@vercel

vercel Bot commented Jul 5, 2026 •

Copy link
Copy Markdown

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

Project Deployment Actions Updated (UTC)
nub Ready Ready Preview, Comment Jul 5, 2026 7:22pm

Request Review

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@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 critical issues — two observations worth a look, both non-blocking.

Reviewed changes — a PM-agnostic, works-without-nub fix for Vite dev servers rejecting store-resident modules (403 … outside of Vite serving allow list) when nub's virtual store lives outside the project root.

  • Add vite_compat module — new crates/nub-cli/src/pm_engine/vite_compat.rs with two units: .modules.yaml write (Unit A, all Vite versions) and a CAS-safe dist/node codegen backport for Vite < 8.1 (Unit B), both gated on vite-in-graph + the NUB_VITE_COMPAT opt-out.
  • Wire post-install hook — apply_vite_compat runs after a successful run_install / run_add / run_update (never run_ci, whose store is already project-local), resolving the install root from session.detected.dir / cwd.
  • Conditional force-materialize — nub_setting_defaults appends vite to forceMaterializePackages only when store_locality == Default and the manifest declares vite directly, so the < 8.1 backport reaches the ejected project-local copy.
  • Docs + NUB_VITE_COMPAT — install docs gain a Vite section and the knob is listed in the nub-identity config surface.
  • Matrix harness — tests/vite-compat/ (README + driver.sh + scaffold.sh) validates both tiers against real framework installs at the HTTP layer.

The mechanism is sound and the seams are clean: aube never writes .modules.yaml itself (it uses .aube-state), so Unit A doesn't collide with engine state; forceMaterializePackages matches on exact package name (HashSet::contains), so ,vite is safe; the CAS-hardlink regression guard test is a genuine safety net. Fail-open throughout and idempotent across re-installs.

ℹ️ Workspace-member vite doesn't get the < 8.1 backport

The force-materialize decision reads only the resolved identity root's manifest, which in a monorepo is the workspace root. If vite is declared directly by a member package (not the root), manifest_declares_vite returns false, so vite is never ejected project-local and Unit B's dist backport can't fire for that member. Vite ≥ 8.1 is still covered by Unit A's .modules.yaml, so this only bites a member on Vite < 8.1 — a narrow case adjacent to the already-documented library-embedded gap.

Technical details
# Workspace-member direct-dep vite is not force-materialized under GVS

## Affected sites
- `crates/nub-cli/src/pm_engine/mod.rs:2170-2177` — `force_materialize` gate calls `manifest_declares_vite(detected.map(|d| d.dir).unwrap_or(cwd))`; `detected.dir` is the identity walk-up dir (workspace root), not the member.
- `crates/nub-cli/src/pm_engine/vite_compat.rs:186-196` — `manifest_declares_vite` reads a single `package.json` at `root`.

## Required outcome
- Decide whether a member-declared vite under a workspace should get the < 8.1 backport. If yes, the eject decision needs to see member manifests, not just the root.

## Open questions for the human
- Is the workspace-member shape in scope for #315, or is it acceptable to leave it in the same documented-gap bucket as library-embedded Vite < 8.1? The reported repro (Astro 7 pinning Vite 8.1.3) is covered by Unit A regardless, so this may be intentional deferral.

ℹ️ Harness default NUB path is worktree-specific

driver.sh and the README default NUB to ~/.cache/nub/worktrees/vite-build-target/fast/nub — the author's build path, not something a fresh checkout produces. It's overridable via the NUB env var and this is a manual dev harness (not CI-wired), so it's low-stakes, but the next person running it will hit a missing-binary error unless they read closely.

Technical details
# tests/vite-compat harness defaults to a machine-specific nub path

## Affected sites
- `tests/vite-compat/driver.sh:33` — `NUB="${NUB:-$HOME/.cache/nub/worktrees/vite-build-target/fast/nub}"`
- `tests/vite-compat/README.md:42` — `export NUB=~/.cache/nub/worktrees/vite-build-target/fast/nub   # or target/fast/nub`

## Required outcome
- A default that a plain checkout produces (e.g. `target/fast/nub` or `target/release/nub`), or drop the default and require `NUB` explicitly so the failure is a clear "set NUB" message.

Pullfrog  | Fix it ➔ | View workflow run | Using Claude Opus | 𝕏

@colinhacks
colinhacks merged commit 7c4970b into main Jul 5, 2026
59 checks passed
colinhacks added a commit that referenced this pull request Jul 6, 2026
The disk-materialize embedder default seeds a version-blind "vite" name for
any direct-dep vite, but vite >= 8.1 reads node_modules/.modules.yaml from the
shared store natively (#318 Unit A, written post-install regardless of eject)
and needs no eject. The name-seed dragged vite plus its whole ancestor-closure
project-local for zero benefit, defeating global-store dedup for the vite
frameworks (kit, plugin-svelte, vitefu, svelte-check, adapter-auto).

Prune the "vite" name-seed in the version-aware closure hook plan_from_flags,
which sees the resolved version. vite < 8.1 stays covered by the existing
vite_lt_8_1 dep_path auto-seed, so its eject is unchanged; vite >= 8.1 now
stays symlinked to the global store and is served via .modules.yaml.

Claude-Session: https://claude.ai/code/session_01Xuh9u8b93eMNu53fTQTWX7
colinhacks added a commit that referenced this pull request Jul 6, 2026
* fix(install): stop force-materializing vite >= 8.1 and its closure

The disk-materialize embedder default seeds a version-blind "vite" name for
any direct-dep vite, but vite >= 8.1 reads node_modules/.modules.yaml from the
shared store natively (#318 Unit A, written post-install regardless of eject)
and needs no eject. The name-seed dragged vite plus its whole ancestor-closure
project-local for zero benefit, defeating global-store dedup for the vite
frameworks (kit, plugin-svelte, vitefu, svelte-check, adapter-auto).

Prune the "vite" name-seed in the version-aware closure hook plan_from_flags,
which sees the resolved version. vite < 8.1 stays covered by the existing
vite_lt_8_1 dep_path auto-seed, so its eject is unchanged; vite >= 8.1 now
stays symlinked to the global store and is served via .modules.yaml.

Claude-Session: https://claude.ai/code/session_01Xuh9u8b93eMNu53fTQTWX7

* test(install): cover the mixed vite <8.1/>=8.1 graph; note provenance-blind prune

Self-review follow-up: add a mixed-graph test (embedded vite<8.1 + direct
vite>=8.1) asserting the name-keyed executor still materializes both copies,
locking in that the prune never regresses the mixed case. Note in the comment
that the "vite" filter is provenance-blind (also strips a user's explicit seed),
which is fine since <8.1 is re-seeded and >=8.1 works symlinked.

Claude-Session: https://claude.ai/code/session_01Xuh9u8b93eMNu53fTQTWX7
@colinhacks

Copy link
Copy Markdown
Contributor Author

This branch was successfully deployed

1 active deployment
Preview — 1db5ad3c Deployed Jul 5, 2026 by vercel[bot]
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.

Shared (global) virtual store

2 participants