gitbutlerapp/gitbutler · error

Expected to be in open workspace mode

Error message

Expected to be in open workspace mode

What it means

ensure_open_workspace_mode is a precondition guard asserting the repository is currently in open-workspace (virtual-branches) mode. Many workspace operations (stack reordering, unapplying stacks, entering edit mode, integration-branch work) are only valid in that mode; when operating_mode returns anything else the guard bails with this message.

Solutions

  1. Initialize/open the workspace for the project before calling these APIs.
  2. Check in_open_workspace_mode (or operating_mode) first and branch your logic accordingly.
  3. If in edit mode, exit edit mode first to return to open workspace mode.
  4. Reload/refresh project state if mode metadata appears stale.

Example fix

// before
update_stack_order_with_perm(&ctx, order, &perm)?;
// after
ensure_open_workspace_mode(&ctx, &perm)
    .context("workspace must be open to reorder stacks")?;
update_stack_order_with_perm(&ctx, order, &perm)?;
Defensive patterns

Strategy: validation

Validate before calling

if !in_open_workspace_mode(&ctx, &perm)? {
    // initialize or open the workspace first
}

Type guard

fn is_open_workspace(mode: &OperatingMode) -> bool {
    matches!(mode, OperatingMode::OpenWorkspace(_))
}

Try / catch

match update_stack_order_with_perm(&ctx, order, &perm) {
    Err(e) if e.to_string().contains("open workspace mode") => {
        open_workspace(&ctx)?;
        update_stack_order_with_perm(&ctx, order, &perm)?;
    }
    other => other?,
}

Prevention

When it happens

Trigger: Calling update_stack_order_with_perm, unapply_stack_v3_with_perm, works_on_integration_branch, or enter_edit_mode while the project is not in open workspace mode - e.g. no workspace initialized, the project is in edit mode, a previous operation left it in an intermediate mode, or the workspace was never assigned for this project.

Common situations: Opening a project that has no GitButler workspace set up; concurrent operations where one path exited workspace mode; tests calling helpers directly without seeding workspace state; after an undo/oplog restore switched modes.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of gitbutlerapp/gitbutler@58e5313667 (2026-09-18). Data as JSON: /api/errors/a0860e1ec7ebe434. Report an issue: GitHub.

Appendix: source

Thrown at crates/gitbutler-operating-modes/src/lib.rs:212

    let (outcome, conflict_kind) =
        but_workspace::legacy::merge_worktree_with_workspace(ctx, gix_repo, &ws)?;
    Ok(outcome
        .conflicts
        .iter()
        .filter(|c| c.is_unresolved(conflict_kind))
        .map(|c| c.ours.location().into())
        .collect())
}

pub fn in_open_workspace_mode(ctx: &Context, perm: &RepoShared) -> Result<bool> {
    Ok(operating_mode(ctx, perm)? == OperatingMode::OpenWorkspace)
}

pub fn ensure_open_workspace_mode(ctx: &Context, perm: &RepoShared) -> Result<()> {
    if in_open_workspace_mode(ctx, perm)? {
        Ok(())
    } else {
        bail!("Expected to be in open workspace mode")
    }
}

pub fn in_edit_mode(ctx: &Context, perm: &RepoShared) -> Result<bool> {
    Ok(matches!(operating_mode(ctx, perm)?, OperatingMode::Edit(_)))
}

pub fn ensure_edit_mode(ctx: &Context, perm: &RepoShared) -> Result<EditModeMetadata> {
    match operating_mode(ctx, perm)? {
        OperatingMode::Edit(edit_mode_metadata) => Ok(edit_mode_metadata),
        _ => Err(anyhow!("Expected to be in edit mode")),
    }
}

pub fn in_outside_workspace_mode(ctx: &Context, perm: &RepoShared) -> Result<bool> {
    Ok(matches!(
        operating_mode(ctx, perm)?,
        OperatingMode::OutsideWorkspace(_)

View on GitHub (pinned to 58e5313667)