{"record":{"id":"1687d9e1d1a8da21","repo":"FuelLabs/sway","slug":"could-not-find-sway-repo-root-directory","errorCode":null,"errorMessage":"Could not find Sway repo root directory","messagePattern":"Could not find Sway repo root directory","errorType":"console","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"scripts/mdbook-forc-documenter/src/lib.rs","lineNumber":173,"sourceCode":"\nfn find_sway_repo_root() -> anyhow::Result<PathBuf> {\n    let mut curr_path = std::env::current_dir().unwrap();\n    loop {\n        if curr_path.is_dir() {\n            // Some heuristics that should pass if we've found the sway repo.\n            if curr_path.join(\"Cargo.toml\").exists()\n                && curr_path.join(\"forc-plugins\").exists()\n                && curr_path\n                    .join(\"scripts\")\n                    .join(\"mdbook-forc-documenter\")\n                    .exists()\n            {\n                return Ok(curr_path);\n            }\n        }\n        curr_path = curr_path\n            .parent()\n            .ok_or_else(|| anyhow!(\"Could not find Sway repo root directory\"))?\n            .to_path_buf();\n    }\n}\n\nfn find_forc_cmd_examples_dir() -> anyhow::Result<PathBuf> {\n    let sway_dir = find_sway_repo_root()?;\n    let examples_dir = sway_dir\n        .join(\"scripts\")\n        .join(\"mdbook-forc-documenter\")\n        .join(\"examples\");\n    if !examples_dir.exists() || !examples_dir.is_dir() {\n        bail!(\n            \"Failed to find examples directory at {}\",\n            examples_dir.display()\n        );\n    }\n    Ok(examples_dir)\n}","sourceCodeStart":155,"sourceCodeEnd":191,"githubUrl":"https://github.com/FuelLabs/sway/blob/47e5e902faa42baf652dd6a0c88cd23390c1a614/scripts/mdbook-forc-documenter/src/lib.rs#L155-L191","documentation":"The mdbook-forc-documenter preprocessor locates the sway repo by walking up from the current working directory until it finds a directory that simultaneously contains Cargo.toml, forc-plugins/, and scripts/mdbook-forc-documenter/. This anyhow error is returned when the walk reaches the filesystem root (Path::parent() returns None) without ever matching those three markers. The error propagates through find_forc_cmd_examples_dir and load_examples (which unwraps it), aborting book builds that need the forc command examples directory.","triggerScenarios":"Invoking mdbook or the preprocessor binary with a working directory outside the sway checkout; running an installed copy of mdbook-forc-documenter from $HOME or another project; the repo layout changing so scripts/mdbook-forc-documenter no longer exists next to Cargo.toml and forc-plugins; std::env::current_dir() resolving into a directory tree that never contains all three markers.","commonSituations":"Building the forc book locally from the wrong shell/directory; cargo-installing the preprocessor and running it like a normal binary; CI checkouts that only fetch docs/ without the rest of the repo; renaming or moving the scripts directory during refactors.","solutions":["Run the book build from inside the sway repo, e.g. `cd ~/sway && mdbook build docs/book`, so the upward walk from docs/book hits the repo root.","Verify the three markers exist at the expected root: Cargo.toml, forc-plugins/, scripts/mdbook-forc-documenter/ — restore any that were moved or renamed.","If the repo layout legitimately changed, update the heuristic in find_sway_repo_root (scripts/mdbook-forc-documenter/src/lib.rs:161) to the new markers.","If you only need plain docs without example injection, remove the preprocessor from docs/book.toml."],"exampleFix":"// before\n$ cd ~ && mdbook build ~/sway/docs/book   # cwd outside repo -> error\n// after\n$ cd ~/sway && mdbook build docs/book        # walk from docs/book finds repo root","handlingStrategy":"validation","validationCode":"# run before mdbook build: confirm cwd is inside the sway repo\nwalk=$(pwd)\nwhile [ \"$walk\" != \"/\" ]; do\n  [ -f \"$walk/Cargo.toml\" ] && [ -d \"$walk/forc-plugins\" ] && [ -d \"$walk/scripts/mdbook-forc-documenter\" ] && break\n  walk=$(dirname \"$walk\")\ndone\n[ \"$walk\" != \"/\" ] || { echo \"run from inside the sway repo\" >&2; exit 1; }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always build the book from the repo root or a subdirectory of the checkout.","Keep the scripts/mdbook-forc-documenter path stable; update find_sway_repo_root markers if the layout moves.","In CI, `cd \"$SWAY_REPO\"` before any mdbook step rather than relying on where the runner starts."],"tags":["rust","sway","mdbook","preprocessor","working-directory","docs"],"backgroundTag":null,"analyzedSha":"47e5e902faa42baf652dd6a0c88cd23390c1a614","analyzedAt":"2026-08-16T07:57:45.555Z","schemaVersion":2},"datasetVersion":"2026-08-16T08:17:34.114Z"}