FuelLabs/fuels-rs · error · anyhow::Error

The end of the anchor appears before the beginning. End anch

Error message

The end of the anchor appears before the beginning. End anchor: {end:?}. Begin anchor: {begin:?}

What it means

Doc-checking error (scripts/check-docs): an ANCHOR/ANCHOR_END pair with matching names exists in the same file, but the end marker's line number is smaller than the start marker's — the pair is inverted. check_validity_of_anchor_pair rejects pairs where begin.line_no > end.line_no.

Source

Thrown at scripts/check-docs/src/lib.rs:153

        .collect::<Vec<_>>();

    let start_only = pairs.into_iter().map(|(begin, _)| begin).collect();

    (start_only, chain!(errors, additional_errors).collect())
}

pub fn filter_unused_ends<'a>(ends: &'a [Anchor], pairs: &[(Anchor, Anchor)]) -> Vec<&'a Anchor> {
    ends.iter()
        .filter(|end| {
            let end_used_in_pairs = pairs.iter().any(|(_, used_end)| *end == used_end);
            !end_used_in_pairs
        })
        .collect()
}

pub fn check_validity_of_anchor_pair(begin: &Anchor, end: &Anchor) -> Option<anyhow::Error> {
    if begin.line_no > end.line_no {
        Some(anyhow!(
            "The end of the anchor appears before the beginning. End anchor: {end:?}. Begin anchor: {begin:?}"
        ))
    } else {
        None
    }
}

#[derive(Debug, Clone, Hash, Eq, PartialEq)]
pub struct Anchor {
    pub line_no: usize,
    pub name: String,
    pub file: PathBuf,
}

pub fn extract_starts_and_ends(
    text_w_anchors: &str,
) -> anyhow::Result<(Vec<Anchor>, Vec<Anchor>), Error> {
    let apply_regex = |regex: Regex| {

View on GitHub (pinned to d9a250a518)

Solutions

  1. Open the file named in the error and find both markers by line numbers given in the Anchor debug output.
  2. Swap the two lines so ANCHOR: <name> precedes ANCHOR_END: <name>.
  3. Re-run check-docs; if the file legitimately needs the snippet order reversed, rename one pair instead of swapping.

Example fix

// before
// ANCHOR_END: snippet
fn b() {}
// ANCHOR: snippet
fn a() {}
// after
// ANCHOR: snippet
fn a() {}
// ANCHOR_END: snippet
fn b() {}
Defensive patterns

Strategy: validation

Validate before calling

let lines: Vec<&str> = src.lines().collect();
let pos = |m: &str| lines.iter().position(|l| l.contains(m));
if let (Some(b), Some(e)) = (pos("ANCHOR: n"), pos("ANCHOR_END: n")) {
    assert!(b < e, "ANCHOR_END appears before ANCHOR");
}

Prevention

When it happens

Trigger: A file contains ANCHOR_END: x physically above ANCHOR: x — markers swapped during editing, or the 'begin' marker collected from a grep of the file is the lower occurrence while the end sits above it.

Common situations: Hand-editing examples and flipping the marker comments; automated refactors that reorder blocks; duplicate names where start/end got interleaved.

Related errors


AI-assisted analysis of FuelLabs/fuels-rs@d9a250a518 (2026-08-16). Data as JSON: /api/errors/d3f089875e6e4426. Report an issue: GitHub.