zed-industries/zed · error

setting Canvas text direction

Error message

setting Canvas text direction: {error:?}

What it means

Thrown in TextCanvas::configure when the JS Reflect.set used to set `direction = 'ltr'` on the OffscreenCanvasRenderingContext2D (web-sys does not expose this property) throws a JsValue. This fires only if the browser rejects the property assignment — typically a detached/invalid 2D context or an unusual browser build — and makes the whole canvas text renderer unusable until a new canvas is created.

Solutions

  1. Retry creating a fresh OffscreenCanvas with a new 2D context, since the existing context is likely in an invalid state
  2. Fall back to skipping the direction assignment (Canvas defaults to 'ltr' direction) and continue rendering with the remaining configure steps
  3. Fall back to a non-Canvas text path (e.g. DOM-based measurement/rasterization) if canvas text keeps failing
  4. Log the JsValue error with its message (convert via js_sys::Error::from for details) to identify the browser-side cause
Defensive patterns

Strategy: fallback

When it happens

Trigger: Thrown at crates/gpui_web/src/canvas_text.rs:65 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of zed-industries/zed@916fc2b8cb (2026-09-19). Data as JSON: /api/errors/421d10257365ea1a. Report an issue: GitHub.

Appendix: source

Thrown at crates/gpui_web/src/canvas_text.rs:65

        Ok(Self { canvas, context })
    }

    fn configure(&self, css_font: &str) -> Result<()> {
        ensure!(!css_font.trim().is_empty(), "Canvas text font is empty");
        // An invalid CSS font assignment is ignored by Canvas. Reset first so
        // it cannot accidentally inherit the preceding request's font.
        self.context.set_font("10px sans-serif");
        self.context.set_font(css_font);
        self.context.set_text_align("left");
        self.context.set_text_baseline("alphabetic");
        self.context.set_fill_style_str("white");
        // web-sys does not expose direction on the offscreen 2D context.
        let assigned = js_sys::Reflect::set(
            self.context.as_ref(),
            &JsValue::from_str("direction"),
            &JsValue::from_str("ltr"),
        )
        .map_err(|error| anyhow!("setting Canvas text direction: {error:?}"))?;
        ensure!(assigned, "Canvas text direction could not be set");
        Ok(())
    }
}

fn with_canvas<T>(operation: impl FnOnce(&mut TextCanvas) -> Result<T>) -> Result<T> {
    CANVAS.with(|canvas| {
        let mut canvas = canvas
            .try_borrow_mut()
            .context("Canvas text renderer was called reentrantly")?;
        if canvas.is_none() {
            *canvas = Some(TextCanvas::new()?);
        }
        operation(
            canvas
                .as_mut()
                .context("Canvas text renderer is unavailable")?,
        )

View on GitHub (pinned to 916fc2b8cb)