refactor: add EnvConfig for centralized env var management#556
Merged
Conversation
4232806 to
9c1c7e8
Compare
7ad3717 to
b948a4a
Compare
branchseer
approved these changes
Feb 9, 2026
Introduce EnvConfig for centralized environment variable management: - EnvConfig::init() in main(), EnvConfig::get() anywhere (OnceLock) - EnvConfig::test_scope() for thread-local test overrides (parallel-safe) - No function signature changes needed Migrated crates: - vite_shared: home.rs, tracing.rs use EnvConfig::get() - vite_install: config.rs removes LazyLock<NPM_REGISTRY>, uses EnvConfig - vite_js_runtime: download.rs (CI check), node.rs (dist mirror) - vite_global_cli: config.rs (version resolution), use.rs (eval enable), registry.rs (npm_registry) Remaining: vite_global_cli tests (22 #[serial] tests in config.rs need EnvConfig::test_scope migration), shim/dispatch.rs env mutations
…g::test_guard Remove #[serial] and unsafe env::set_var/remove_var from: - config.rs: all 22 serial tests converted - package_metadata.rs: 3 serial tests converted Remaining serial tests in other files (~40) follow the same pattern and can be converted in follow-up commits.
…stall tests to EnvConfig - Add user_home, fish_version, ps_module_path fields to EnvConfig - Export TestEnvGuard from vite_shared - Convert setup.rs: 11 tests, no more unsafe HOME mutation - Convert use.rs: detect_shell() reads from EnvConfig, 3 tests converted - Convert bin_config.rs: 4 tests using test_guard(for_test_with_home) - Convert global_install.rs: 1 test using test_guard - Convert package_metadata.rs: 2 remaining tests Removed 218 lines of unsafe env var manipulation. All #[serial] removed from converted tests. Remaining #[serial] tests are in dispatch.rs (deferred), doctor.rs (own EnvGuard pattern), exec.rs, and js_executor.rs.
…cing
VITE_LOG is only read once during tracing initialization via OnceLock,
so there's no benefit from caching it in EnvConfig. Restored the original
direct std::env::var("VITE_LOG") read in init_tracing().
Add vite_shared::env_vars module with constants for all vite-plus environment variable names. Replace string literals across all crates: - env_config.rs: 10 var names → env_vars constants - tracing.rs: VITE_LOG → env_vars::VITE_LOG - dispatch.rs: VITE_PLUS_BYPASS (13x), RECURSION_ENV_VAR, DEBUG_SHIM, ACTIVE_NODE, RESOLVE_SOURCE → env_vars constants - doctor.rs: VITE_PLUS_BYPASS (5x), VITE_PLUS_HOME, NODE_VERSION - exec.rs: VITE_PLUS_TOOL_RECURSION (2x) - config.rs: VERSION_ENV_VAR → env_vars::VITE_PLUS_NODE_VERSION - mod.rs: SHIM_TOOL_ENV_VAR → env_vars::VITE_PLUS_SHIM_TOOL - js_executor.rs: JS_SCRIPTS_DIR, CLI_BIN Standard system vars (PATH, HOME, CI, etc.) intentionally excluded.
b948a4a to
4ef8184
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Phase 1+2 of env var refactoring. Introduces
EnvConfigstruct that reads all known environment variables once at startup, enabling testable, parallel-safe code.Problem
std::envcalls across 7 crates, 140 areset_var/remove_varenv::set_varisunsafesince Rust 1.83#[serial]or#[ignore]due to global env mutationChanges
New:
vite_shared::EnvConfigEnvConfig::from_env()— read from real environment (production)EnvConfig::for_test()— sensible defaults, zero env accessEnvConfig::for_test_with_home(path)— convenience for temp dir testsEnvConfig { is_ci: true, ..EnvConfig::for_test() }Updated:
home.rsget_vite_plus_home_with_config(&EnvConfig)— config-based, no env accessget_vite_plus_home()preserved for backward compat#[ignore]test, replaced with parallel-safe config testUpdated:
tracing.rsinit_tracing_with_config(&EnvConfig)— config-basedinit_tracing()preservedinit_tracing_inner()Test Results
cargo test -p vite_shared: 19 passed + 2 doc-tests ✅cargo check(full project): compiles ✅Next Phases
vite_install+vite_js_runtimevite_global_cli(biggest change)unsafe env::set_var