biomejs/biome · error

Tried to format a `format_once` at least twice. This is not

Error message

Tried to format a `format_once` at least twice. This is not allowed. You may want to use `format_with` or `format.memoized` instead.

What it means

Error "Tried to format a `format_once` at least twice. This is not allowed. You may want to use `format_with` or `format.memoized` instead." thrown in biomejs/biome.

Source

Thrown at crates/biome_formatter/src/builders.rs:2680

{
    FormatOnce {
        formatter: Cell::new(Some(formatter)),
        context: PhantomData,
    }
}

pub struct FormatOnce<T, Context> {
    formatter: Cell<Option<T>>,
    context: PhantomData<Context>,
}

impl<T, Context> Format<Context> for FormatOnce<T, Context>
where
    T: FnOnce(&mut Formatter<Context>) -> FormatResult<()>,
{
    #[inline(always)]
    fn fmt(&self, f: &mut Formatter<Context>) -> FormatResult<()> {
        let formatter = self.formatter.take().expect("Tried to format a `format_once` at least twice. This is not allowed. You may want to use `format_with` or `format.memoized` instead.");

        (formatter)(f)
    }
}

impl<T, Context> std::fmt::Debug for FormatOnce<T, Context> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_tuple("FormatOnce").field(&"{{formatter}}").finish()
    }
}

/// Builder to join together a sequence of content.
/// See [Formatter::join]
#[must_use = "must eventually call `finish()` on Format builders"]
pub struct JoinBuilder<'fmt, 'buf, Separator, Context> {
    result: FormatResult<()>,
    fmt: &'fmt mut Formatter<'buf, Context>,
    with: Option<Separator>,

View on GitHub (pinned to 405dedb0ff)

Solutions

  1. Use `format_with` or `format.memoized` for values that must be formatted more than once.
  2. Refactor the format call chain so each `format_once` node is emitted exactly once.

Example fix

let memo = content.memoized(); format_with(|f| { write!(f, [memo, memo]) })

When it happens

Trigger: Thrown at crates/biome_formatter/src/builders.rs:2680 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of biomejs/biome@405dedb0ff (2026-08-20). Data as JSON: /api/errors/8119932b76a654ca. Report an issue: GitHub.