gitbutlerapp/gitbutler · error

Expected to be in edit mode

Error message

Expected to be in edit mode

What it means

ensure_edit_mode returns the EditModeMetadata only when the repository's current operating mode is Edit; any other mode (outside-workspace, review, etc.) yields this error. It guards operations that only make sense while editing a workspace.

Solutions

  1. Enter edit mode before performing the operation (the mode check precedes these calls).
  2. Check operating_mode() to see which mode the repo is actually in.
  3. If another process changed the mode, refresh/resync the app state.
  4. Handle the error gracefully — the operation is invalid in the current mode, not retryable as-is.

Example fix

// before
let meta = ensure_edit_mode(&ctx, &perm)?; // fails if not editing
// after
if !in_edit_mode(&ctx, &perm)? {
    enter_edit_mode(&ctx, &perm)?;
}
let meta = ensure_edit_mode(&ctx, &perm)?;
Defensive patterns

Strategy: validation

Validate before calling

if !in_edit_mode(&ctx, &perm)? {
    // enter edit mode first or skip the operation
    return Err(anyhow!("operation requires edit mode"));
}

Try / catch

match ensure_edit_mode(&ctx, &perm) {
    Err(e) if e.to_string() == "Expected to be in edit mode" => {
        // recover: refresh mode state or re-enter edit mode
    }
    r => r?,
}

Prevention

When it happens

Trigger: Calling save_and_return_to_workspace, abort_and_return_to_workspace, or starting_index_state when the repo is not in edit mode — e.g. before entering edit mode, after it was exited by another client/process, or after the mode marker was cleaned up.

Common situations: Two GitButler clients or a CLI and app both touching the same repo, one exiting edit mode; stale app state after a crash removed the mode marker; calling the API without first entering edit mode.

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/824e7a9ad2a44273. Report an issue: GitHub.

Appendix: source

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

    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)