Skip to content

Commit b913a30

Browse files
committed
fix: avoid SetGlobalDefaultError panic when rolldown devtools is enabled
Skip installing a global tracing subscriber during NAPI module init when VITE_LOG is not set. Previously, init_tracing() always installed a no-op subscriber (empty Targets filter) that occupied the global default slot. When rolldown devtools was later enabled via build.rolldownOptions.devtools, DebugTracer::init() would panic trying to claim the already-occupied slot. Now the global default slot stays free for other components unless VITE_LOG is explicitly set, and even then try_init() is used for graceful fallback. Closes #1356
1 parent a94deea commit b913a30

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

crates/vite_shared/src/tracing.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,34 @@ use crate::env_vars;
1414
/// Uses `OnceLock` to ensure tracing is only initialized once,
1515
/// even if called multiple times.
1616
///
17+
/// Only sets the global default subscriber when `VITE_LOG` is set.
18+
/// When unset, the global default slot is left free so that other
19+
/// subscribers (e.g., rolldown devtools) can claim it without panicking.
20+
///
21+
/// Uses `try_init()` instead of `init()` so that even when `VITE_LOG` is
22+
/// set, a previously-installed global subscriber does not cause a panic.
23+
///
1724
/// # Environment Variables
1825
/// - `VITE_LOG`: Controls log filtering (e.g., "debug", "vite_task=trace")
1926
pub fn init_tracing() {
2027
static TRACING: OnceLock<()> = OnceLock::new();
2128
TRACING.get_or_init(|| {
29+
let Ok(env_var) = std::env::var(env_vars::VITE_LOG) else {
30+
// No VITE_LOG set — skip subscriber installation so the global
31+
// default slot stays available for other components.
32+
return;
33+
};
34+
35+
use std::str::FromStr;
2236
tracing_subscriber::registry()
2337
.with(
24-
std::env::var(env_vars::VITE_LOG)
25-
.map_or_else(
26-
|_| Targets::new(),
27-
|env_var| {
28-
use std::str::FromStr;
29-
Targets::from_str(&env_var).unwrap_or_default()
30-
},
31-
)
38+
Targets::from_str(&env_var)
39+
.unwrap_or_default()
3240
// disable brush-parser tracing
3341
.with_targets([("tokenize", LevelFilter::OFF), ("parse", LevelFilter::OFF)]),
3442
)
3543
.with(tracing_subscriber::fmt::layer())
36-
.init();
44+
.try_init()
45+
.ok();
3746
});
3847
}

0 commit comments

Comments
 (0)