Skip to content

fix: preserve empty process environment variables in DotenvPlugin - #22785

Closed
shuaiyafei wants to merge 1 commit into
webpack:mainfrom
shuaiyafei:fix/dotenv-empty-process-env
Closed

shuaiyafei wants to merge 1 commit into
webpack:mainfrom
shuaiyafei:fix/dotenv-empty-process-env

Conversation

@shuaiyafei

@shuaiyafei shuaiyafei commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Preserve empty process environment variables when no .env default exists, including with dotenv.dir: false. Check for undefined instead of using a truthiness check.

Actual build output for an environment variable set to "":

Source expression Before After
process.env.WEBPACK_DOTENV_EMPTY_PROCESS_ONLY undefined ""
import.meta.env.WEBPACK_DOTENV_EMPTY_PROCESS_ONLY undefined ""

What kind of change does this PR introduce?

fix

Did you add tests for your changes?

Yes. test/configCases/plugins/dotenv-empty-process-env covers empty variables, file overrides, and nonempty values with file loading enabled and disabled.

All 48 dotenv tests pass across ConfigTestCases and ConfigCacheTestCases. The regression fails in both suites before the fix.

yarn lint passes, including generated files, types, formatting, and spellcheck.

Does this PR introduce a breaking change?

No.

If relevant, what needs to be documented once your changes are merged or what have you already documented?

n/a

Use of AI

Codex helped reproduce the bug, prepare the patch and regression tests, and run the checks.

Summary by CodeRabbit

  • Bug Fixes
    • Empty process environment variables are now preserved instead of being replaced by values from .env files. Nonempty process values continue to take precedence.

@linux-foundation-easycla

Copy link
Copy Markdown

CLA Not Signed

@changeset-bot

changeset-bot Bot commented Oct 19, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: b91f4da

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

This PR includes changesets to release 1 package
Name Type
webpack 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

@coderabbitai

coderabbitai Bot commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository: webpack/webpack/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: fe8f07d1-3380-4b3b-830f-7491d13bad0a
📥 Commits

Reviewing files that changed from the base of the PR and between f9705a6 and b91f4da.

📒 Files selected for processing: 5
  • .changeset/070-dotenv-empty-process-env.md
  • lib/define/DotenvPlugin.js
  • test/configCases/plugins/dotenv-empty-process-env/.env
  • test/configCases/plugins/dotenv-empty-process-env/index.js
  • test/configCases/plugins/dotenv-empty-process-env/webpack.config.js

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

DotenvPlugin._getEnv now falls back to the parsed .env value when the matching process.env value is undefined. Tests cover empty and nonempty process values with dotenv enabled and disabled.

Changes: Dotenv Environment Value Resolution

Layer / File(s) Summary
Resolve and test process environment values
lib/define/DotenvPlugin.js, test/configCases/plugins/dotenv-empty-process-env/*, .changeset/070-dotenv-empty-process-env.md
_getEnv falls back to the parsed value when the process value is undefined. Tests check empty values, file-value overrides, import.meta.env property presence, and nonempty process values. The changeset records the patch.

Suggested reviewers: alexander-akait

Priority: ➖ Normal

Change: Bug fix

Merge Risk: ⚪ Minimal · up to b91f4

This fix preserves process environment variables set to an empty string in dotenv-defined values. The change is small and tested, and no actionable merge-blocking risk is evident.

Pre-merge checks: Passed 3 · Inconclusive 1

Failed Failed checks: 1 inconclusive

Check name Status Explanation Resolution
Title check Inconclusive The title uses the required Conventional Commit format and the allowed type fix. The branch prefix is not provided, so the type-to-branch-prefix match cannot be verified. Provide the branch name or confirm that its prefix is fix.
Passed Passed checks: 3 passed
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches: 💡 1
🛠️ Fix failing CI checks: 💡
  • Commit to this branch
  • Create a new PR
  • Autofix · 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.

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.

1 participant