FuelLabs/sway · error · anyhow::Error

{:?}

Error message

{:?}

What it means

forc clean needs a starting directory to search upward for Forc.toml; with no --path flag it calls std::env::current_dir(), and failure (deleted cwd, permission, exotic platform errors) is reported via anyhow!("{:?}", e) — the raw io::Error debug string with no extra context.

Source

Thrown at forc/src/ops/forc_clean.rs:16

use crate::cli::CleanCommand;
use anyhow::{anyhow, bail, Result};
use forc_pkg::manifest::GenericManifestFile;
use forc_pkg::manifest::ManifestFile;
use forc_util::default_output_directory;
use std::path::PathBuf;
use sway_utils::{find_parent_manifest_dir, MANIFEST_FILE_NAME};

pub fn clean(command: CleanCommand) -> Result<()> {
    let CleanCommand { path } = command;

    // find manifest directory, even if in subdirectory
    let this_dir = if let Some(ref path) = path {
        PathBuf::from(path)
    } else {
        std::env::current_dir().map_err(|e| anyhow!("{:?}", e))?
    };

    let manifest_dir = match find_parent_manifest_dir(&this_dir) {
        Some(dir) => dir,
        None => {
            bail!(
                "could not find `{}` in `{}` or any parent directory",
                MANIFEST_FILE_NAME,
                this_dir.display(),
            )
        }
    };
    let manifest = ManifestFile::from_dir(manifest_dir)?;
    // If this is a workspace collect all member paths and clean each of them.
    let paths: Vec<PathBuf> = match manifest {
        ManifestFile::Package(_) => std::iter::once(this_dir).collect(),
        ManifestFile::Workspace(workspace) => workspace.member_paths()?.collect(),
    };

View on GitHub (pinned to 47e5e902fa)

Solutions

  1. cd into an existing directory inside your project and rerun `forc clean`
  2. Pass the project explicitly: `forc clean --path /abs/path/to/project`
  3. Fix the script so forc runs before the directory is removed

Example fix

# before (cwd deleted)
forc clean

# after
forc clean --path /home/me/my-project
Defensive patterns

Strategy: validation

Validate before calling

fn forc_clean(path: Option<&str>) -> anyhow::Result<()> {
    let dir = match path {
        Some(p) => std::path::PathBuf::from(p),
        None => {
            let cwd = std::env::current_dir()
                .map_err(|_| anyhow::anyhow!("cwd unavailable; pass --path explicitly"))?;
            if !cwd.is_dir() { anyhow::bail!("cwd {cwd:?} no longer exists"); }
            cwd
        }
    };
    // ... invoke `forc clean --path <dir>`
    Ok(())
}

Prevention

When it happens

Trigger: Running `forc clean` from a working directory that was deleted out from under the shell (common after `rm -rf` in another terminal or a script that cd's into a dir it then removes), or in environments where cwd cannot be determined.

Common situations: Scripts that create, enter, and delete a scratch directory and then invoke forc; container/sandbox setups restricting cwd metadata; shells left in removed directories.

Related errors


AI-assisted analysis of FuelLabs/sway@47e5e902fa (2026-08-16). Data as JSON: /api/errors/fafd99beee7d78cc. Report an issue: GitHub.