FuelLabs/sway · error

failed to write toml file: {}

Error message

failed to write toml file: {}

What it means

In forc's dependency add/remove flow (DepModifier), Forc.toml is modified in memory and then validated by rebuilding a BuildPlan; if that validation fails, the tool restores the original Forc.toml from its in-memory backup. This error is the restore write failing - so when you see it, two things went wrong: the modified manifest was invalid (the original error) AND the filesystem prevented rollback (the wrapped write_err). The underlying plan error is masked by this one.

Source

Thrown at forc-pkg/src/manifest/dep_modifier.rs:120

    // write updates to toml doc
    std::fs::write(&package_manifest_dir, toml_doc.to_string())?;

    let updated_package_manifest = PackageManifestFile::from_file(&package_manifest_dir)?;

    let member_manifests = updated_package_manifest.member_manifests()?;

    let new_plan = pkg::BuildPlan::from_lock_and_manifests(
        &lock_path,
        &member_manifests,
        false,
        opts.offline,
        &opts.ipfs_node.clone().unwrap_or_default(),
    );

    new_plan.or_else(|e| {
        std::fs::write(&package_manifest_dir, backup_doc.to_string())
            .map_err(|write_err| anyhow!("failed to write toml file: {}", write_err))?;
        Err(e)
    })?;

    if opts.dry_run {
        info!("Dry run enabled. toml file not modified.");
        std::fs::write(&package_manifest_dir, backup_doc.to_string())?;

        let string = toml::ser::to_string_pretty(&old_lock)?;
        std::fs::write(&lock_path, string)?;

        return Ok(());
    }

    Ok(())
}

fn resolve_package_path(
    manifest_file: &ManifestFile,

View on GitHub (pinned to 47e5e902fa)

Solutions

  1. Restore Forc.toml manually - the backup content equals the original, or use git checkout -- Forc.toml / git restore Forc.toml.
  2. Fix filesystem writability (permissions, mount options, disk space) in the project directory.
  3. Re-run the forc add/remove command; with rollback now working you will see the real underlying plan error and can fix the dependency spec that caused it.
  4. Use --dry-run first to validate a dependency change without modifying files.

Example fix

# before: run where Forc.toml is not writable
$ chmod 444 Forc.toml && forc add foo
# error: failed to write toml file ...

# after
$ chmod 644 Forc.toml
$ git restore Forc.toml   # undo any partial change
$ forc add foo            # real validation error (if any) now visible
Defensive patterns

Strategy: try-catch

Validate before calling

// Before running forc add/remove, assert the project dir is writable:
use std::fs;
fn writable_dir(p: &std::path::Path) -> bool {
    fs::OpenOptions::new().write(true).open(p.join(".writetest".as_path()))
        .map(|f| { drop(f); fs::remove_file(p.join(".writetest")).ok(); true })
        .unwrap_or(false)
}

Try / catch

// The API returns anyhow::Result - handle Err and restore yourself, since the tool's
// own rollback may be what failed:
match modify_deps(opts) {
    Ok(_) => {}
    Err(e) => {
        if e.to_string().contains("failed to write toml file") {
            let _ = std::process::Command::new("git")
                .args(["restore", "Forc.toml"]).status();
        }
    }
}

Prevention

When it happens

Trigger: forc add / forc remove where BuildPlan::from_lock_and_manifests rejects the modified manifest and the subsequent fs::write of the backup Forc.toml fails - read-only project directory, deleted parent directory, changed permissions, or a full disk.

Common situations: Read-only or root-owned checkouts in CI containers; disk-full conditions; directories removed by another process mid-command; running without write permission on the project.

Related errors


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