Skip to content

fix(vite): register service environments only after they are constructed - #4743

Merged
pi0 merged 3 commits into
nitrojs:mainfrom
reedjosh:fix/vite-service-env-registration
Oct 7, 2026
Merged

pi0 merged 3 commits into
nitrojs:mainfrom
reedjosh:fix/vite-service-env-registration

Conversation

@reedjosh

@reedjosh reedjosh commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

🔗 Linked issue

Resolves #4638

❓ Type of change

  • 🐞 Bug fix (a non-breaking change that fixes an issue)

📚 Description

When the env runner becomes ready, it replays every environment in ctx._viteEnvs to the dev worker, and the worker starts importing from an environment as soon as it hears about it. createServiceEnvironment added its environment to that map before constructing it. That allowed two races on a cold vite dev start:

Both the nitro and service environments now register themselves (for the replay) and announce themselves only once FetchableDevEnvironment.init() has finished.

The new test/vite/service-env-registration.test.ts starts a real Vite dev server and forces each race by holding startup until the worker has invoked every environment it was told about. Both tests fail on main, and pass with this change.

🤖 Generated with AI assistant

`createServiceEnvironment` added the environment to `ctx._viteEnvs` before building it. When the env runner became
ready in that window it announced the environment to the dev worker before its Vite transport existed, so the worker's
first module runner invoke (`getBuiltins`) was dropped and the environment never recovered. Register it after
construction, as `createNitroEnvironment` already does; the environment announces itself from `init()` once its
transport exists.

Resolves nitrojs#4638
@reedjosh
reedjosh requested a review from pi0 as a code owner October 7, 2026 12:04
@vercel

vercel Bot commented Oct 7, 2026

Copy link
Copy Markdown

@reedjosh is attempting to deploy a commit to the Nitro Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai

coderabbitai Bot commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Note

Currently processing new changes in this PR. This may take a few minutes, please wait...

⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 08c6563a-f901-4921-b6c8-dafd68f2872e
📥 Commits

Reviewing files that changed from the base of the PR and between 316f1c1 and bdecf59.

📒 Files selected for processing (5)
  • src/build/vite/dev.ts
  • src/build/vite/env.ts
  • test/vite/service-env-registration-fixture/entry-server.ts
  • test/vite/service-env-registration-fixture/vite.config.ts
  • test/vite/service-env-registration.test.ts
 _________________________________________________________________________________
< CodeRabbit is a psyop to make you think your code is being reviewed by a human. >
 ---------------------------------------------------------------------------------
  \
   \   \
        \ /\
        ( )
      .( o ).

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: f7cf690d-1193-4451-bb55-e645a60c284e
📥 Commits

Reviewing files that changed from the base of the PR and between ccf4db0 and 316f1c1.

📒 Files selected for processing (2)
  • src/build/vite/env.ts
  • test/unit/vite-service-env-registration.test.ts

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


📝 Walkthrough

Walkthrough

createServiceEnvironment now awaits dev environment construction before registering the service entry in _viteEnvs. A Vitest test checks that registration is absent during deferred construction and present after it resolves.

Changes

Service environment registration

Layer / File(s) Summary
Construct and register the service environment
src/build/vite/env.ts, test/unit/vite-service-env-registration.test.ts
createServiceEnvironment now registers the service entry after dev environment construction resolves. The test verifies the entry is absent before resolution and maps to "/entry-server.ts" afterward.

Priority: ➖ Normal

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Merge Risk: ⚪ Minimal · up to 316f1

The registration change is mergeable after normal checks. No startup failure from the changed ordering has been established.

Security Architecture Review

Security architecture risk: 🔵 Low · up to 316f1

The change narrows a development startup race without changing the configured service entries or their execution permissions. Remaining uncertainty concerns initialization and shutdown ordering, not an identified new security exposure.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — The demonstrated impact is scoped to configured service environments sharing a Nitro development runner. The changed path does not expand their entry selection, runner authority, or message payload; no new tenant, credential, or deployment boundary was established by the comparison.

Trust Boundaries and Controls

  • observed — The existing control-flow boundary is a host-to-worker environment announcement followed by worker-side module execution. The worker takes its entry path from that announcement and schedules its initial import asynchronously. This PR changes publication timing, not that authority transfer.

Resilience and Maintainability Implications

  • inferred — Completed registrations remain available for worker-restart replay. Shutdown still lacks coordination with pending environment construction, and registry publication has no per-instance generation check. These are residual lifecycle uncertainties rather than established PR-introduced security defects: the base already retained registrations through shutdown and lacked instance ownership.
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Issue #4638 requires registration after service-environment construction so the dev worker cannot receive an announcement before its transport exists. src/build/vite/env.ts now awaits construction b…
Out of Scope Changes check ✅ Passed The changes are limited to the #4638 registration-order fix and its regression test. Both directly support the linked issue.
Title check ✅ Passed The title follows the Conventional Commits format with the fix(vite) type and scope. It accurately summarizes the change to service-environment registration.
Description check ✅ Passed The description explains the service-environment registration race, its effect on SSR, and the fix and tests in the changeset.
  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autopilot · 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

Comment @coderabbitai help to get the list of available commands.

pi0 added 2 commits October 7, 2026 12:29
…erver

Replace the mocked unit test with a dev server test (real worker, real transport) where
the dev worker's first invoke arrives before the ssr service environment is constructed.
Without the fix the SSR request hangs; with it the page renders.
An environment registered in `_viteEnvs` after construction but before `init()` finished could
still be announced by the runner's ready replay. The dev worker then invoked `fetchModule` before
the plugin container existed and SSR kept failing. Register from `init()`, right before the
environment announces itself, for both the nitro and service environments.
@pkg-pr-new

pkg-pr-new Bot commented Oct 7, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/nitro@4743

commit: bdecf59

@pi0
pi0 merged commit f3df91e into nitrojs:main Oct 7, 2026
11 of 13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants