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
- Write the tests in a real .sw file compiled from disk instead of generating declarations without source info
- If you use the compiler APIs directly, register every file with the SourceEngine (get_source_id/new_source) before parsing
- Update forc/sway to the latest patch release in case this is already fixed
- 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
- Register every source file with the SourceEngine before parsing so spans carry source ids
- Avoid fabricating test declarations without file information
- Keep forc and sway compiler crates version-matched in tooling
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
- Invalid revert code for test \"{}\". A revert code must be a
- The allocator cannot resolve a register mapping for this pro
- The allocator cannot resolve a register mapping for this pro
- Arrays in storage have not been implemented yet.
- abi_encode_size_hint for [{}]
AI-assisted analysis of FuelLabs/sway@47e5e902fa (2026-08-16).
Data as JSON: /api/errors/467f7b2730227ab3.
Report an issue: GitHub.