Skip to content

Reload env on the first Vite dev restart when root is not the cwd - #1262

Merged
theoephraim merged 2 commits into
dmno-dev:mainfrom
fengmk2:fix-vite-first-restart-reload
Oct 30, 2026
Merged

theoephraim merged 2 commits into
dmno-dev:mainfrom
fengmk2:fix-vite-first-restart-reload

Conversation

@fengmk2

@fengmk2 fengmk2 commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1261.

When Vite's root is not the working directory (for example vite app), the first config hook call reloads from that root, but it does not set configHookCalled. On the first dev restart, the root now equals loadedConfigDir, so the hook takes the first-call branch: it sets the flag and skips the reload. The server keeps stale env values until the second restart. This started in 1.5.0 with #985, which changed the check from process.cwd() to loadedConfigDir. The same stale restart happens with rootDir when _VARLOCK_ENV_KEY is already set, because then the dev-key lifecycle hook does not load rootDir before the main hook.

The fix replaces configHookCalled with a flag that every load sets and the main config hook clears once it has used that load. The hook reloads when the root differs, or on a dev resolve when the current load was already used (a restart). Any loader (the module-level load, the dev-key hook, an integration's refresh) now counts without needing its own flag. This also removes the second load on Nuxt dev restarts, where refreshVarlockEnv already reloaded before the config hook.

Tests cover restarts with the root equal to and different from the cwd, rootDir with and without a preset key, Nuxt-style refreshes, and builds, and assert load counts so redundant or skipped reloads show up.

One behavior change: in a dev or Vitest run whose first config has a different root, a later config call with the same root now reloads. That already happens when the root is the working directory.

When the first config hook call reloads from a root that differs from the loaded directory, it did not set configHookCalled. The next call, the first dev restart, then took the first-call branch and skipped its reload, so the server kept stale env values until the second restart.

Fixes dmno-dev#1261

@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 new issues found.

Reviewed changes Reviewed the restart-state fix, both regression tests, and the patch release entry.

  • Restart tracking: Marks the first root-correcting config reload as a completed hook call so the next dev restart reloads env values.
  • Regression coverage: Tests repeated config resolution for a Vite root outside cwd and for rootDir with an existing encryption key. All 26 Vite integration tests passed via bunx turbo run test:ci --filter=@varlock/vite-integration; filesystem-watched restarts were not independently exercised.
  • Release scope: Includes a patch release entry for @varlock/vite-integration.

Pullfrog  | View workflow run | Using gpt-6.1-sol | 𝕏

@theoephraim

Copy link
Copy Markdown
Member

thanks for this!

@pkg-pr-new

pkg-pr-new Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/@varlock/astro-integration@1262
npm i https://pkg.pr.new/@varlock/cloudflare-integration@1262
npm i https://pkg.pr.new/@varlock/nuxt-integration@1262
npm i https://pkg.pr.new/@varlock/vite-integration@1262

commit: 289620d

Every load marks itself unused and the main config hook consumes it, so any
loader (module-level, dev-key hook, an integration's refresh) counts without
its own flag. This also drops the second load on Nuxt dev restarts. Adds
restart tests that assert load counts.

@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 new issues found.

Reviewed changes Reviewed the load-consumption refactor and added regression coverage since the previous review.

  • Replaced restart tracking: Tracked whether the current load has been consumed by the config hook, preserving first-restart reloads while reusing a preceding framework refresh.
  • Expanded regression coverage: Added load-count checks for repeated resolves, dev-key root correction, Nuxt-style refreshes, and same-root builds. All 31 Vite integration tests passed via bunx turbo run test:ci --filter=@varlock/vite-integration; filesystem-watched restarts were not independently exercised.

Pullfrog  | View workflow run | Using gpt-6.1-sol | 𝕏

@theoephraim
theoephraim merged commit 4ce8395 into dmno-dev:main Oct 30, 2026
30 checks passed
@fengmk2
fengmk2 deleted the fix-vite-first-restart-reload branch November 3, 2026 16:06
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.

[BUG]: Vite plugin keeps stale env on first dev restart when root is not cwd

2 participants