astral-sh/ruff · error

`initLogging` to only be called at most once.

Error message

`initLogging` to only be called at most once.

What it means

initLogging initializes console_log for the ruff WASM build; console_log::init_with_level errors if a logger is already installed, and the .expect turns that into a panic: '`initLogging` to only be called at most once.' (documented under ## Panics).

Source

Thrown at crates/ruff_wasm/src/lib.rs:243

    // When the `console_error_panic_hook` feature is enabled, we can call the
    // `set_panic_hook` function at least once during initialization, and then
    // we will get better error messages if our code ever panics.
    //
    // For more details see
    // https://github.com/rustwasm/console_error_panic_hook#readme
    #[cfg(feature = "console_error_panic_hook")]
    console_error_panic_hook::set_once();
}

/// Initializes the logger with the given log level.
///
/// ## Panics
/// If this function is called more than once.
#[wasm_bindgen(js_name = "initLogging")]
pub fn init_logging(level: LogLevel) {
    console_log::init_with_level(level.into())
        .expect("`initLogging` to only be called at most once.");
}

#[derive(Copy, Clone, Debug)]
#[wasm_bindgen]
pub enum LogLevel {
    Trace,
    Debug,
    Info,
    Warn,
    Error,
}

impl From<LogLevel> for log::Level {
    fn from(level: LogLevel) -> Self {
        match level {
            LogLevel::Trace => log::Level::Trace,
            LogLevel::Debug => log::Level::Debug,
            LogLevel::Info => log::Level::Info,

View on GitHub (pinned to d1087a4b9e)

Solutions

  1. Call initLogging exactly once during app bootstrap
  2. Guard with a module-level flag so repeat invocations are ignored
  3. On reconfiguration, keep the existing logger rather than re-initializing it

Example fix

// before
export function boot(level) { initLogging(level); } // called twice -> panic

// after
let loggingReady = false;
export function boot(level) {
  if (!loggingReady) { initLogging(level); loggingReady = true; }
}
Defensive patterns

Strategy: validation

Validate before calling

let loggingInitialized = false;
function boot(level) {
  if (loggingInitialized) return;
  initLogging(level);
  loggingInitialized = true;
}

Prevention

When it happens

Trigger: Calling the exported initLogging twice within one WASM instance - React StrictMode double-mount, hot-module reload re-running bootstrap, or two init code paths both executing.

Common situations: Playground/embedding setups that re-initialize on settings change; duplicated init logic in bundler entry points; HMR during development.

Related errors


AI-assisted analysis of astral-sh/ruff@d1087a4b9e (2026-08-20). Data as JSON: /api/errors/93c5addb651d4b68. Report an issue: GitHub.