FuelLabs/sway · error · anyhow::Error

Missing span for test \"{}\".

Error message

Missing span for test \"{}\".

What it means

While collecting a test's metadata, forc resolves the test's file path via engines.se().get_path(span.source_id()). If the stored span for the #[test] declaration carries no source id, there is no file to attribute the test to and forc bails with this message. It is an internal invariant failure: declarations parsed from real files always carry source ids.

Source

Thrown at forc-pkg/src/pkg.rs:2114

                            .map_err(|_| {
                                anyhow!(get_invalid_revert_code_error_msg(
                                    &test_function_decl.name,
                                    should_revert_arg
                                ))
                            })?,
                    ),
                    Err(_) => bail!(get_invalid_revert_code_error_msg(
                        &test_function_decl.name,
                        should_revert_arg
                    )),
                }
            }
            None => TestPassCondition::ShouldNotRevert,
        };

        let file_path =
            Arc::new(engines.se().get_path(span.source_id().ok_or_else(|| {
                anyhow!("Missing span for test \"{}\".", test_function_decl.name)
            })?));
        Ok(Self {
            pass_condition,
            span,
            file_path,
        })
    }
}

/// The suffix that helps identify the file which contains the hash of the binary file created when
/// scripts are built_package.
pub const SWAY_BIN_HASH_SUFFIX: &str = "-bin-hash";

/// The suffix that helps identify the file which contains the root hash of the binary file created
/// when predicates are built_package.
pub const SWAY_BIN_ROOT_SUFFIX: &str = "-bin-root";

/// Selects the build profile from all available build profiles in the workspace using build_opts.

View on GitHub (pinned to 47e5e902fa)

Solutions

  1. Write the tests in a real .sw file compiled from disk instead of generating declarations without source info
  2. If you use the compiler APIs directly, register every file with the SourceEngine (get_source_id/new_source) before parsing
  3. Update forc/sway to the latest patch release in case this is already fixed
  4. If it reproduces with stock `forc test`, open an issue with a minimal project
Defensive patterns

Strategy: try-catch

Try / catch

match pkg::CompiledTests::from_manifest(...) {
    Ok(tests) => run(tests),
    Err(e) if e.to_string().contains("Missing span for test") => {
        // generated declarations without source info: warn and skip instead of aborting
        log::warn!("skipping test discovery, tests lack spans: {e}");
    }
    Err(e) => return Err(e),
}

Prevention

When it happens

Trigger: Programmatic or LSP compilation where the declaration's span was synthesized without a source id (macro-generated or injected AST nodes), or tooling that constructs engines without registering the source file before parsing.

Common situations: Custom tools built on sway compiler APIs that fabricate declarations without file info; rarely a compiler refactor bug - then usually accompanied by other span-related errors.

Related errors


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