FuelLabs/sway · error · anyhow::Error

{:?}: invalid source map json: {}

Error message

{:?}: invalid source map json: {}

What it means

After reading the sourcemap file bytes, forc addr2line deserializes them with serde_json::from_slice::<SourceMap>; failure produces "<path>: invalid source map json: <serde error>" including the serde message (line/column of the parse failure). The file was readable but is not valid JSON shaped like a SourceMap.

Source

Thrown at forc/src/cli/commands/addr2line.rs:37

    #[clap(short = 'S', long, default_value = ".")]
    pub search_dir: PathBuf,
    /// Source file mapping in JSON format
    #[clap(short = 'g', long)]
    pub sourcemap_path: PathBuf,
    /// How many lines of context to show
    #[clap(short, long, default_value = "2")]
    pub context: usize,
    /// Opcode index
    #[clap(short = 'i', long)]
    pub opcode_index: usize,
}

pub(crate) fn exec(command: Command) -> ForcResult<()> {
    let contents = fs::read(&command.sourcemap_path)
        .map_err(|err| anyhow!("{:?}: could not read: {:?}", command.sourcemap_path, err))?;

    let sm: SourceMap = serde_json::from_slice(&contents).map_err(|err| {
        anyhow!(
            "{:?}: invalid source map json: {}",
            command.sourcemap_path,
            err
        )
    })?;

    if let Some((mut path, range)) = sm.addr_to_span(command.opcode_index) {
        if path.is_relative() {
            path = command.search_dir.join(path);
        }

        let rr = read_range(&path, range, command.context)
            .map_err(|err| anyhow!("{:?}: could not read: {:?}", path, err))?;

        let path_str = format!("{path:?}");
        let snippet = Snippet {
            title: None,
            footer: vec![],

View on GitHub (pinned to 47e5e902fa)

Solutions

  1. Point -g at the file produced by `forc build -g` for the same binary you are debugging
  2. Validate the file parses as JSON and contains SourceMap fields (the serde message names the offending position/field)
  3. Regenerate the sourcemap with the same forc version that built the bytecode

Example fix

# before
forc addr2line -g out/debug/my_pkg-abi.json -i 42

# after
forc addr2line -g out/debug/my_pkg.bin.json -i 42
Defensive patterns

Strategy: validation

Validate before calling

// Cheap pre-flight: the file must be JSON before handing it to addr2line.
let raw = std::fs::read(&sm_path)?;
serde_json::from_slice::<serde_json::Value>(&raw)
    .map_err(|e| anyhow::anyhow!("{} is not valid JSON ({e}) — is this the sourcemap from `forc build -g`?", sm_path.display()))?;

Type guard

fn looks_like_sourcemap(v: &serde_json::Value) -> bool {
    v.get("program").is_some() || v.get("asm").is_some() || v.is_array()
}

Try / catch

match serde_json::from_slice::<SourceMap>(&contents) {
    Ok(sm) => sm,
    Err(e) => anyhow::bail!("{e}: {} — likely the wrong artifact; use the .bin.json emitted by `forc build -g`", path.display()),
}

Prevention

When it happens

Trigger: Passing a non-sourcemap file to `forc addr2line -g` — e.g. the ABI json, the raw .bin bytecode, or a truncated/edited sourcemap — so serde_json errors at syntax or at a type mismatch within the SourceMap structure.

Common situations: Mixing up the several json artifacts forc produces (ABI vs sourcemap), sourcemaps produced by an incompatible forc version with a different schema, or files mangled by text-mode processing (line-ending/CRLF rewrites, leading BOM).

Related errors


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