gitbutlerapp/gitbutler · error

Nothing to anchor a comment to in

Error message

Nothing to anchor a comment to in {scope}

What it means

create_comment() first resolves the comment's anchor (a file path plus optional line range) against the applied workspace. If the anchor resolution returns FileAnchor::Gone, the file the comment was anchored to no longer exists in the workspace diff, so there is nothing to attach the comment to and the library refuses to create it.

Solutions

  1. Refresh the workspace state and verify the target file is part of the commit/diff before creating the comment
  2. Use an anchor derived from the current applied workspace instead of a cached/stale file path
  3. If the file was deleted intentionally, archive or migrate the old comment rather than creating a new one
  4. Fall back to an unanchored (commit-level) comment if the API supports it

Example fix

// before
let comment = Comment::new(file_anchor("src/old.rs"), lines);
create_comment(&ctx, comment)?;
// after
if workspace.applied_files().iter().any(|f| f.path == "src/old.rs") {
    create_comment(&ctx, Comment::new(file_anchor("src/old.rs"), lines))?;
} else {
    eprintln!("File no longer in workspace; skipping comment");
}
Defensive patterns

Strategy: validation

Validate before calling

fn can_anchor(workspace: &Workspace, path: &str) -> bool {
    workspace.applied_files().iter().any(|f| f.path == path)
}

Type guard

if let FileAnchor::Lines(lines) = resolve_anchor(workspace, &comment) { /* anchorable */ }

Try / catch

match create_comment(&ctx, comment) {
    Err(e) if e.to_string().contains("Nothing to anchor") => skip_comment(),
    other => other?,
}

Prevention

When it happens

Trigger: Calling create_comment with a comment whose file anchor points to a file that is not present in the applied workspace's diff — e.g. the target file was removed, renamed, or the commit/change-id was reassigned so the path no longer appears.

Common situations: A UI comment left on a file that was later hunk-reassigned or deleted; a stale reference after a workspace refresh; commenting on a file in a commit where the file was not actually changed.

Understand the failure class

Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.

Related errors


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

Appendix: source

Thrown at crates/but-comments/src/lib.rs:138

pub fn create_comment(
    repo: &gix::Repository,
    workspace: &but_graph::Workspace,
    store: &CommentStore,
    comment: NewComment,
    context_lines: u32,
    now_ms: i64,
) -> anyhow::Result<DiffComment> {
    let scope = anchor_scope_display(&comment.commit_change_id, &comment.path);
    let mut diffs = ScopeDiffs::new(repo, workspace, context_lines);
    let Some(anchor) = diffs.file(comment.commit_change_id.as_deref(), &comment.path)? else {
        bail!(
            "Commit {} is not in the applied workspace",
            comment.commit_change_id.as_deref().unwrap_or_default()
        );
    };
    let file = match anchor {
        FileAnchor::Lines(lines) => lines,
        FileAnchor::Gone => bail!("Nothing to anchor a comment to in {scope}"),
        FileAnchor::Unanchorable => {
            bail!("Cannot comment on {scope}: the diff is binary or too large")
        }
    };
    let line = file
        .line_at(comment.side, comment.line_number)
        .with_context(|| {
            format!(
                "No line {} on the {} side in {scope}",
                comment.line_number,
                comment.side.as_str(),
            )
        })?;

    // Snapshot the same-side neighbouring lines too: they disambiguate between identical
    // lines when the comment is re-located later.
    let line_before = (comment.line_number > 1)
        .then(|| file.line_at(comment.side, comment.line_number - 1))

View on GitHub (pinned to 58e5313667)