slint-ui/slint · error

EditorSession must have at least one preview

Error message

EditorSession must have at least one preview

What it means

`primary_preview()` returns the first element of `EditorSession::previews` and panics via `expect` when the list is empty. The invariant is that an `EditorSession` always has at least one live preview connection (the editor always opens at least one preview). A session created or drained without a preview violates construction invariants.

Solutions

  1. Ensure the preview connection is registered before using the session (go through the normal editor-preview startup path)
  2. Use `previews.first()` / `session.preview(0)` and handle `None` instead of `primary_preview()`
  3. In tests, construct the session via the API that seeds at least one `PreviewConnection`
  4. Report upstream if the main editor itself closed the last preview and still calls this

Example fix

// before
let preview = session.primary_preview();
// after
let preview = session.previews.first()
    .ok_or_else(|| anyhow::anyhow!("no preview connection available"))?;
Defensive patterns

Strategy: type-guard

Validate before calling

if session.previews.is_empty() { /* cannot use primary_preview */ }

Type guard

fn has_preview(s: &EditorSession) -> bool { !s.previews.is_empty() }

Prevention

When it happens

Trigger: Calling `session.primary_preview()` on an `EditorSession` whose `previews` Vec is empty — e.g. the session was constructed via `Default`/`EditorSession::new` without registering a preview connection, or the only preview was removed/closed beforehand.

Common situations: Hit when tooling embeds the editor-preview crate directly (tests, custom preview harnesses) and creates a session without going through the normal editor startup that spawns a preview; also after code changes that allow closing the last preview.

Understand the failure class

Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.

Related errors


AI-assisted analysis of slint-ui/slint@bb937076de (2026-09-16). Data as JSON: /api/errors/5e0aaac6a350111c. Report an issue: GitHub.

Appendix: source

Thrown at internal/editor-preview/editor_session.rs:49

    pub to_show: Option<PreviewComponent>,
}

/// The documents currently being edited, together with the state the preview
/// needs to follow along.
pub struct EditorSession {
    pub document_cache: crate::DocumentCache,
    pub preview_config: PreviewConfig,
    /// File currently open in the editor
    pub open_urls: HashSet<lsp_types::Url>,
    pub previews: Vec<PreviewConnection>,
    /// Files to recompile after all other operations are done
    /// (i.e. recompilations triggered by updates to unopened files)
    pub pending_recompile: HashSet<lsp_types::Url>,
}

impl EditorSession {
    pub fn primary_preview(&self) -> &PreviewConnection {
        self.previews.first().expect("EditorSession must have at least one preview")
    }

    pub fn primary_preview_mut(&mut self) -> &mut PreviewConnection {
        self.previews.first_mut().expect("EditorSession must have at least one preview")
    }

    pub fn preview(&self, preview_index: usize) -> Option<&PreviewConnection> {
        let preview = self.previews.get(preview_index);
        if preview.is_none() {
            tracing::warn!(
                "Preview index {preview_index} is out of bounds for {} previews",
                self.previews.len()
            );
        }
        preview
    }

    pub fn preview_mut(&mut self, preview_index: usize) -> Option<&mut PreviewConnection> {

View on GitHub (pinned to bb937076de)