Skip to content

Commit a493b94

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 a493b94

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

crates/vite_shared/src/tracing.rs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Tracing initialization for vite-plus
22
3-
use std::sync::OnceLock;
3+
use std::{str::FromStr, sync::OnceLock};
44

55
use tracing_subscriber::{
66
filter::{LevelFilter, Targets},
@@ -14,25 +14,30 @@ 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+
///
1721
/// # Environment Variables
1822
/// - `VITE_LOG`: Controls log filtering (e.g., "debug", "vite_task=trace")
1923
pub fn init_tracing() {
2024
static TRACING: OnceLock<()> = OnceLock::new();
2125
TRACING.get_or_init(|| {
26+
let Ok(env_var) = std::env::var(env_vars::VITE_LOG) else {
27+
// No VITE_LOG set — skip subscriber installation so the global
28+
// default slot stays available for other components.
29+
return;
30+
};
31+
2232
tracing_subscriber::registry()
2333
.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-
)
34+
Targets::from_str(&env_var)
35+
.unwrap_or_default()
3236
// disable brush-parser tracing
3337
.with_targets([("tokenize", LevelFilter::OFF), ("parse", LevelFilter::OFF)]),
3438
)
3539
.with(tracing_subscriber::fmt::layer())
36-
.init();
40+
.try_init()
41+
.ok();
3742
});
3843
}

0 commit comments

Comments
 (0)