Skip to content

Commit 00d3fdc

Browse files
committed
Allow linking rustc and rustdoc against the same single tracing crate
By consecutively initializing `tracing` and `rustc_log`, Rustdoc assumes that these involve 2 different tracing crates. I would like to be able to build rustdoc against the same tracing crate that rustc_log is also built against. Previously this arrangement would crash rustdoc: thread 'main' panicked at rust/compiler/rustc_log/src/lib.rs:142:65: called `Result::unwrap()` on an `Err` value: SetGlobalDefaultError("a global default trace dispatcher has already been set") stack backtrace: 0: rust_begin_unwind 1: core::panicking::panic_fmt 2: core::result::unwrap_failed 3: rustc_log::init_logger 4: rustc_driver_impl::init_logger 5: rustdoc::main note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace. error: the compiler unexpectedly panicked. this is a bug. note: we would appreciate a bug report: https://github.com/rust-lang/rust/issues/new?labels=C-bug%2C+I-ICE%2C+T-rustdoc&template=ice.md note: please make sure that you have updated to the latest nightly query stack during panic: end of query stack
1 parent d7df5bd commit 00d3fdc

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

compiler/rustc_log/src/lib.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ use std::env::{self, VarError};
3737
use std::fmt::{self, Display};
3838
use std::io::{self, IsTerminal};
3939

40+
use tracing::dispatcher::SetGlobalDefaultError;
4041
use tracing_core::{Event, Subscriber};
4142
use tracing_subscriber::filter::{Directive, EnvFilter, LevelFilter};
4243
use tracing_subscriber::fmt::FmtContext;
@@ -131,10 +132,10 @@ pub fn init_logger(cfg: LoggerConfig) -> Result<(), Error> {
131132
.without_time()
132133
.event_format(BacktraceFormatter { backtrace_target });
133134
let subscriber = subscriber.with(fmt_layer);
134-
tracing::subscriber::set_global_default(subscriber).unwrap();
135+
tracing::subscriber::set_global_default(subscriber)?;
135136
}
136137
Err(_) => {
137-
tracing::subscriber::set_global_default(subscriber).unwrap();
138+
tracing::subscriber::set_global_default(subscriber)?;
138139
}
139140
};
140141

@@ -180,6 +181,7 @@ pub enum Error {
180181
InvalidColorValue(String),
181182
NonUnicodeColorValue,
182183
InvalidWraptree(String),
184+
AlreadyInit(SetGlobalDefaultError),
183185
}
184186

185187
impl std::error::Error for Error {}
@@ -199,6 +201,13 @@ impl Display for Error {
199201
formatter,
200202
"invalid log WRAPTREE value '{value}': expected a non-negative integer",
201203
),
204+
Error::AlreadyInit(tracing_error) => Display::fmt(tracing_error, formatter),
202205
}
203206
}
204207
}
208+
209+
impl From<SetGlobalDefaultError> for Error {
210+
fn from(tracing_error: SetGlobalDefaultError) -> Self {
211+
Error::AlreadyInit(tracing_error)
212+
}
213+
}

src/librustdoc/lib.rs

+21-5
Original file line numberDiff line numberDiff line change
@@ -170,12 +170,28 @@ pub fn main() {
170170
// NOTE: this compiles both versions of tracing unconditionally, because
171171
// - The compile time hit is not that bad, especially compared to rustdoc's incremental times, and
172172
// - Otherwise, there's no warning that logging is being ignored when `download-rustc` is enabled
173-
// NOTE: The reason this doesn't show double logging when `download-rustc = false` and
174-
// `debug_logging = true` is because all rustc logging goes to its version of tracing (the one
175-
// in the sysroot), and all of rustdoc's logging goes to its version (the one in Cargo.toml).
176173

177-
init_logging(&early_dcx);
178-
rustc_driver::init_logger(&early_dcx, rustc_log::LoggerConfig::from_env("RUSTDOC_LOG"));
174+
crate::init_logging(&early_dcx);
175+
match rustc_log::init_logger(rustc_log::LoggerConfig::from_env("RUSTDOC_LOG")) {
176+
Ok(()) => {}
177+
// With `download-rustc = true` there are definitely 2 distinct tracing crates in the
178+
// dependency graph: one in the downloaded sysroot and one built just now as a dependency of
179+
// rustdoc. So the sysroot's tracing is definitely not yet initialized here.
180+
//
181+
// But otherwise, depending on link style, there may or may not be 2 tracing crates in play.
182+
// The one we just initialized in `crate::init_logging` above is rustdoc's direct dependency
183+
// on tracing. When rustdoc is built by x.py using Cargo, rustc_driver's and rustc_log's
184+
// tracing dependency is distinct from this one and also needs to be initialized (using the
185+
// same RUSTDOC_LOG environment variable for both). Other build systems may use just a
186+
// single tracing crate throughout the rustc and rustdoc build.
187+
//
188+
// The reason initializing 2 tracings does not show double logging when `download-rustc =
189+
// false` and `debug_logging = true` is because all rustc logging goes only to its version
190+
// of tracing (the one in the sysroot) and all of rustdoc's logging only goes to its version
191+
// (the one in Cargo.toml).
192+
Err(rustc_log::Error::AlreadyInit(_)) => {}
193+
Err(error) => early_dcx.early_fatal(error.to_string()),
194+
}
179195

180196
let exit_code = rustc_driver::catch_with_exit_code(|| {
181197
let at_args = rustc_driver::args::raw_args(&early_dcx);

0 commit comments

Comments
 (0)