Skip to content

fix(config): apply $production/$development layers when NODE_ENV is unset#4066

Open
jlgrimes wants to merge 1 commit intonitrojs:mainfrom
jlgrimes:fix/issue-3052-env-layer-default
Open

fix(config): apply $production/$development layers when NODE_ENV is unset#4066
jlgrimes wants to merge 1 commit intonitrojs:mainfrom
jlgrimes:fix/issue-3052-env-layer-default

Conversation

@jlgrimes
Copy link

@jlgrimes jlgrimes commented Mar 2, 2026

🔗 Linked issue

Resolves #3052

❓ Type of change

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

📚 Description

Nitro uses c12 for config loading, but when NODE_ENV is unset it currently forwards no envName, so c12 skips $production and $development layers entirely.

This causes production deployments (e.g. Cloudflare Pages) to ignore $production config blocks unless NODE_ENV is manually set.

What changed

  • Default c12 envName from configOverrides.dev:
    • development when dev: true
    • production when dev: false
  • Keep opts.c12.envName override support intact (explicit override still wins).
  • Added regression tests that verify both $production and $development layers are applied correctly when NODE_ENV is unset.

Validation

  • pnpm vitest test/unit/config-loader-env.test.ts

@jlgrimes jlgrimes requested a review from pi0 as a code owner March 2, 2026 03:32
@vercel
Copy link

vercel bot commented Mar 2, 2026

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

A member of the Team first needs to authorize it.

@coderabbitai
Copy link

coderabbitai bot commented Mar 2, 2026

📝 Walkthrough

Walkthrough

The changes introduce an envName parameter to the config loader that defaults to "development" or "production" based on the dev flag, enabling environment-based configuration layers to function without requiring NODE_ENV to be set. Unit tests validate the correct layer application based on the dev flag.

Changes

Cohort / File(s) Summary
Configuration Loader Enhancement
src/config/loader.ts
Adds envName parameter derived from opts.c12?.envName with default fallback to "development" (when dev is true) or "production", passed to loadConfig call.
Environment Layer Tests
test/unit/config-loader-env.test.ts
Introduces comprehensive unit tests for environment-based config layers, including fixture creation with multi-layer configurations ($production, $development, base) and tests verifying correct layer application based on NODE_ENV and dev flag states.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

🚥 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%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The pull request title 'fix(config): apply $production/$development layers when NODE_ENV is unset' follows conventional commits format with a clear fix prefix and descriptive summary of the change.
Description check ✅ Passed The pull request description is well-related to the changeset, explaining the issue, what changed, and providing validation details about environment layer application.
Linked Issues check ✅ Passed The PR successfully addresses issue #3052 by implementing envName defaults (development/production) based on dev flag when NODE_ENV is unset, with regression tests verifying both layers are applied correctly.
Out of Scope Changes check ✅ Passed All changes are in scope: environment layer defaulting logic in src/config/loader.ts and comprehensive regression tests in test/unit/config-loader-env.test.ts directly address the linked issue requirements.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Tip

Try Coding Plans. Let us write the prompt for your AI agent so you can ship faster (with fewer bugs).
Share your feedback on Discord.


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 and usage tips.

@jlgrimes jlgrimes changed the title fix(config): apply / layers when NODE_ENV is unset fix(config): apply $production/$development layers when NODE_ENV is unset Mar 2, 2026
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
test/unit/config-loader-env.test.ts (1)

60-61: Consider verifying base layer is preserved alongside env layer.

The fixture defines a base /base route. Adding an assertion for it would confirm that env-specific layers are merged correctly without losing base configuration.

💡 Optional enhancement
     expect(options.routeRules["/prod"]?.headers?.["x-env"]).toBe("production");
     expect(options.routeRules["/dev"]).toBeUndefined();
+    expect(options.routeRules["/base"]?.headers?.["x-env"]).toBe("base");
   });

   it("applies $development when NODE_ENV is unset and dev=true", async () => {
     // ...
     expect(options.routeRules["/dev"]?.headers?.["x-env"]).toBe("development");
     expect(options.routeRules["/prod"]).toBeUndefined();
+    expect(options.routeRules["/base"]?.headers?.["x-env"]).toBe("base");
   });

Also applies to: 71-72

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@test/unit/config-loader-env.test.ts` around lines 60 - 61, Add assertions
that the base route from the fixture is still present after env layering: check
options.routeRules["/base"] exists and its original properties (e.g., headers,
upstreams, or whatever the fixture defines) match expected values to ensure the
env layer merged rather than replaced the base layer; update both places where
routeRules for env tests are asserted (the blocks that currently check
"/prod"/"/dev") to include these "/base" assertions referencing
options.routeRules.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@test/unit/config-loader-env.test.ts`:
- Around line 60-61: Add assertions that the base route from the fixture is
still present after env layering: check options.routeRules["/base"] exists and
its original properties (e.g., headers, upstreams, or whatever the fixture
defines) match expected values to ensure the env layer merged rather than
replaced the base layer; update both places where routeRules for env tests are
asserted (the blocks that currently check "/prod"/"/dev") to include these
"/base" assertions referencing options.routeRules.

ℹ️ Review info

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between bfbb207 and 395a111.

📒 Files selected for processing (2)
  • src/config/loader.ts
  • test/unit/config-loader-env.test.ts

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.

$development and $production config keys don't work without setting NODE_ENV

1 participant