-
-
Notifications
You must be signed in to change notification settings - Fork 257
Closed
Copy link
Description
What problem does this feature solve?
Description
Currently, loadEnv() merges environment variables from .env,.env[.local] .env[.mode], and .env[.mode[.local]] files into process.env. While useful for build processes, this causes environment pollution when using custom CLI tools outside rsbuild's build/dev flows.
The current loadEnv() + cleanup() pattern has limitations when environment variables already exist before loading:
- cleanup simply deletes injected variables rather than restoring original values
- Pre-existing values get permanently erased when using this pattern
Proposal
Add a dryRun boolean option to loadEnv(). When enabled:
- Files are still loaded/merged using existing priority rules
- Parsed values are returned in the
parsedfield - No modifications are made to
process.env
(Note: loadEnv() May need a separate processEnv configuration for advanced environment injection control,like:https://dotenvx.com/docs/advanced/parse-process-env)
What does the proposed API look like?
Use Case
Creating custom CLI tools that need env values without side effects:
# .env
HELLO=WORLD
import { loadEnv } from '@rsbuild/core';
// In custom CLI script
const { parsed } = loadEnv({
dryRun: true, // Prevent env injection
mode: 'production'
});
console.log(parsed.HELLO); // WORLD Value from .env files
console.log(process.env.HELLO); // undefined (no pollution)Behavior Comparison
| Option | Returns parsed | Modifies process.env |
|---|---|---|
| dryRun:true | ✅ | ❌ |
| Default | ✅ | ✅ |
Implementation Notes
- Should maintain full compatibility with existing API
- Should preserve existing environment variable merging logic
- Documentation update needed for new option
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels