{"record":{"id":"a6d9057bc938991d","repo":"gitbutlerapp/gitbutler","slug":"nothing-to-anchor-a-comment-to-in-scope","errorCode":null,"errorMessage":"Nothing to anchor a comment to in {scope}","messagePattern":"Nothing to anchor a comment to in (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/but-comments/src/lib.rs","lineNumber":138,"sourceCode":"pub fn create_comment(\n    repo: &gix::Repository,\n    workspace: &but_graph::Workspace,\n    store: &CommentStore,\n    comment: NewComment,\n    context_lines: u32,\n    now_ms: i64,\n) -> anyhow::Result<DiffComment> {\n    let scope = anchor_scope_display(&comment.commit_change_id, &comment.path);\n    let mut diffs = ScopeDiffs::new(repo, workspace, context_lines);\n    let Some(anchor) = diffs.file(comment.commit_change_id.as_deref(), &comment.path)? else {\n        bail!(\n            \"Commit {} is not in the applied workspace\",\n            comment.commit_change_id.as_deref().unwrap_or_default()\n        );\n    };\n    let file = match anchor {\n        FileAnchor::Lines(lines) => lines,\n        FileAnchor::Gone => bail!(\"Nothing to anchor a comment to in {scope}\"),\n        FileAnchor::Unanchorable => {\n            bail!(\"Cannot comment on {scope}: the diff is binary or too large\")\n        }\n    };\n    let line = file\n        .line_at(comment.side, comment.line_number)\n        .with_context(|| {\n            format!(\n                \"No line {} on the {} side in {scope}\",\n                comment.line_number,\n                comment.side.as_str(),\n            )\n        })?;\n\n    // Snapshot the same-side neighbouring lines too: they disambiguate between identical\n    // lines when the comment is re-located later.\n    let line_before = (comment.line_number > 1)\n        .then(|| file.line_at(comment.side, comment.line_number - 1))","sourceCodeStart":120,"sourceCodeEnd":156,"githubUrl":"https://github.com/gitbutlerapp/gitbutler/blob/58e5313667b857ef39a730e380af31816a7b1768/crates/but-comments/src/lib.rs#L120-L156","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Refresh the workspace state and verify the target file is part of the commit/diff before creating the comment","Use an anchor derived from the current applied workspace instead of a cached/stale file path","If the file was deleted intentionally, archive or migrate the old comment rather than creating a new one","Fall back to an unanchored (commit-level) comment if the API supports it"],"exampleFix":"// before\nlet comment = Comment::new(file_anchor(\"src/old.rs\"), lines);\ncreate_comment(&ctx, comment)?;\n// after\nif workspace.applied_files().iter().any(|f| f.path == \"src/old.rs\") {\n    create_comment(&ctx, Comment::new(file_anchor(\"src/old.rs\"), lines))?;\n} else {\n    eprintln!(\"File no longer in workspace; skipping comment\");\n}","handlingStrategy":"validation","validationCode":"fn can_anchor(workspace: &Workspace, path: &str) -> bool {\n    workspace.applied_files().iter().any(|f| f.path == path)\n}","typeGuard":"if let FileAnchor::Lines(lines) = resolve_anchor(workspace, &comment) { /* anchorable */ }","tryCatchPattern":"match create_comment(&ctx, comment) {\n    Err(e) if e.to_string().contains(\"Nothing to anchor\") => skip_comment(),\n    other => other?,\n}","preventionTips":["Re-resolve anchors from the live workspace before commenting","Purge or migrate comments when their files are deleted/reassigned","Disable inline commenting on files absent from the diff","Subscribe to workspace-change events to invalidate stale comment UI"],"tags":["git","comments","stale-anchor"],"backgroundTag":"resource-not-found","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"}