Skip to content

fix(nextjs): call initVarlockEnv() in cached-env path#449

Draft
Copilot wants to merge 2 commits intomainfrom
copilot/fix-varlock-env-initialization
Draft

fix(nextjs): call initVarlockEnv() in cached-env path#449
Copilot wants to merge 2 commits intomainfrom
copilot/fix-varlock-env-initialization

Conversation

Copy link
Contributor

Copilot AI commented Mar 20, 2026

At runtime on Vercel (and similar platforms), loadEnvConfig() takes the cached-env branch because __VARLOCK_ENV is already set from the build. This path called resetRedactionMap() and patchGlobalConsole() but skipped initVarlockEnv(), leaving the ENV proxy uninitialized and throwing "varlock ENV not initialized" on every request.

Changes

  • next-env-compat.ts — add initVarlockEnv() to the useCachedEnv branch, before resetRedactionMap(), matching the order in the fresh-load path:
// cached-env path (runtime)
initVarlockEnv();          // ← was missing
resetRedactionMap(varlockLoadedEnv);
patchGlobalConsole();
Original prompt

This section details on the original issue you should resolve

<issue_title>initVarlockEnv() not called in cached-env path — ENV proxy uninitialized at runtime on Vercel</issue_title>
<issue_description>## Bug Description

When deploying a Next.js app to Vercel using the @next/env override (npm:@varlock/nextjs-integration), the ENV proxy from varlock/env is not initialized at runtime. Importing ENV and accessing any property throws:

Error: varlock ENV not initialized

The root cause is in next-env-compat.js's loadEnvConfig() function. There are two code paths:

  1. Fresh load (build time) — calls initVarlockEnv()
  2. Cached env (runtime, when __VARLOCK_ENV is set) — does not call initVarlockEnv()

The cached path only calls resetRedactionMap() and patchGlobalConsole(), but skips initVarlockEnv(), so the ENV proxy never gets initialized.

Relevant code in next-env-compat.js:

// Cached path (runtime on Vercel) — missing initVarlockEnv()
if (useCachedEnv) {
    if (!varlockLoadedEnv) {
      varlockLoadedEnv = JSON.parse(process.env.__VARLOCK_ENV || "{}");
      parsedEnv = Object.fromEntries(
        Object.entries(varlockLoadedEnv.config).map(([key, value]) => [key, value.value])
      );
      env.resetRedactionMap(varlockLoadedEnv);  // ← called
      // env.initVarlockEnv();                  // ← NOT called
      patchConsole.patchGlobalConsole();
    }
    combinedEnv = { ...exports.initialEnv, ...parsedEnv };
    return { combinedEnv, parsedEnv, loadedEnvFiles };
}

// Fresh load path (build time) — correctly calls initVarlockEnv()
// ...
env.initVarlockEnv();       // ← called
env.resetRedactionMap(varlockLoadedEnv);

Environment

  • varlock: 0.4.1 (also reproduced on 0.6.1)
  • @varlock/nextjs-integration: 1.0.0 (also reproduced on 0.2.3)
  • next: 16.2.0 (Next.js 14 appears unaffected — possibly calls loadEnvConfig differently at startup)
  • Deployed on Vercel serverless

Reproduction

  1. Set up a Next.js 16 app with the @next/env override
  2. Use ENV from varlock/env in a server route
  3. Deploy to Vercel
  4. Build succeeds, but any runtime request that accesses ENV throws "varlock ENV not initialized"

Workaround

Add an instrumentation hook that manually initializes varlock:

// src/instrumentation.ts
export async function register() {
  if (process.env.__VARLOCK_ENV) {
    const { initVarlockEnv } = await import("varlock/env");
    initVarlockEnv();
  }
}

Suggested Fix

Add env.initVarlockEnv() to the cached-env code path in next-env-compat.js, alongside the existing resetRedactionMap() call.

Related

Comments on the Issue (you are @copilot in this section)


📱 Kick off Copilot coding agent tasks wherever you are with GitHub Mobile, available on iOS and Android.

@changeset-bot
Copy link

changeset-bot bot commented Mar 20, 2026

🦋 Changeset detected

Latest commit: 7a29344

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@varlock/nextjs-integration Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Co-authored-by: philmillman <3722211+philmillman@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix ENV proxy uninitialized error in Vercel deployment fix(nextjs): call initVarlockEnv() in cached-env path Mar 20, 2026
Copilot AI requested a review from philmillman March 20, 2026 15:34
@pkg-pr-new
Copy link

pkg-pr-new bot commented Mar 20, 2026

Open in StackBlitz

npm i https://pkg.pr.new/@varlock/nextjs-integration@449

commit: 7a29344

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.

initVarlockEnv() not called in cached-env path — ENV proxy uninitialized at runtime on Vercel

2 participants