{"record":{"id":"a0860e1ec7ebe434","repo":"gitbutlerapp/gitbutler","slug":"expected-to-be-in-open-workspace-mode","errorCode":null,"errorMessage":"Expected to be in open workspace mode","messagePattern":"Expected to be in open workspace mode","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/gitbutler-operating-modes/src/lib.rs","lineNumber":212,"sourceCode":"    let (outcome, conflict_kind) =\n        but_workspace::legacy::merge_worktree_with_workspace(ctx, gix_repo, &ws)?;\n    Ok(outcome\n        .conflicts\n        .iter()\n        .filter(|c| c.is_unresolved(conflict_kind))\n        .map(|c| c.ours.location().into())\n        .collect())\n}\n\npub fn in_open_workspace_mode(ctx: &Context, perm: &RepoShared) -> Result<bool> {\n    Ok(operating_mode(ctx, perm)? == OperatingMode::OpenWorkspace)\n}\n\npub fn ensure_open_workspace_mode(ctx: &Context, perm: &RepoShared) -> Result<()> {\n    if in_open_workspace_mode(ctx, perm)? {\n        Ok(())\n    } else {\n        bail!(\"Expected to be in open workspace mode\")\n    }\n}\n\npub fn in_edit_mode(ctx: &Context, perm: &RepoShared) -> Result<bool> {\n    Ok(matches!(operating_mode(ctx, perm)?, OperatingMode::Edit(_)))\n}\n\npub fn ensure_edit_mode(ctx: &Context, perm: &RepoShared) -> Result<EditModeMetadata> {\n    match operating_mode(ctx, perm)? {\n        OperatingMode::Edit(edit_mode_metadata) => Ok(edit_mode_metadata),\n        _ => Err(anyhow!(\"Expected to be in edit mode\")),\n    }\n}\n\npub fn in_outside_workspace_mode(ctx: &Context, perm: &RepoShared) -> Result<bool> {\n    Ok(matches!(\n        operating_mode(ctx, perm)?,\n        OperatingMode::OutsideWorkspace(_)","sourceCodeStart":194,"sourceCodeEnd":230,"githubUrl":"https://github.com/gitbutlerapp/gitbutler/blob/58e5313667b857ef39a730e380af31816a7b1768/crates/gitbutler-operating-modes/src/lib.rs#L194-L230","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Initialize/open the workspace for the project before calling these APIs.","Check in_open_workspace_mode (or operating_mode) first and branch your logic accordingly.","If in edit mode, exit edit mode first to return to open workspace mode.","Reload/refresh project state if mode metadata appears stale."],"exampleFix":"// before\nupdate_stack_order_with_perm(&ctx, order, &perm)?;\n// after\nensure_open_workspace_mode(&ctx, &perm)\n    .context(\"workspace must be open to reorder stacks\")?;\nupdate_stack_order_with_perm(&ctx, order, &perm)?;","handlingStrategy":"validation","validationCode":"if !in_open_workspace_mode(&ctx, &perm)? {\n    // initialize or open the workspace first\n}","typeGuard":"fn is_open_workspace(mode: &OperatingMode) -> bool {\n    matches!(mode, OperatingMode::OpenWorkspace(_))\n}","tryCatchPattern":"match update_stack_order_with_perm(&ctx, order, &perm) {\n    Err(e) if e.to_string().contains(\"open workspace mode\") => {\n        open_workspace(&ctx)?;\n        update_stack_order_with_perm(&ctx, order, &perm)?;\n    }\n    other => other?,\n}","preventionTips":["Initialize the GitButler workspace when a project is opened before issuing workspace operations","Check operating mode at the top of every workspace-mutating command","Exit edit mode explicitly before calling open-workspace-only APIs"],"tags":["git","workspace","operating-mode","precondition"],"backgroundTag":"invalid-state-transition","analyzedSha":"58e5313667b857ef39a730e380af31816a7b1768","analyzedAt":"2026-09-18T06:50:32.052Z","contentChangedAt":"2026-09-18T06:50:32.052Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}