FuelLabs/fuels-rs · error
Failed to load the contract
Error message
Failed to load the contract
What it means
For each contract declared in setup_program_test!, the generated code calls Contract::load_from(bin_path, load_config) synchronously and expects success. The panic means the compiled contract binary could not be read or parsed: the file is missing, empty, or not a valid fuel binary.
Source
Thrown at packages/fuels-macros/src/setup_program_test/code_gen.rs:155
// These lines must be inside the `quote!` macro, otherwise the salt remains
// identical between macro compilation, causing contract id collision.
::fuels::test_helpers::generate_random_salt()
}
} else {
quote! { [0; 32] }
};
quote! {
let salt: [u8; 32] = #salt;
let #contract_instance_name = {
let load_config = ::fuels::programs::contract::LoadConfiguration::default().with_salt(salt);
let loaded_contract = ::fuels::programs::contract::Contract::load_from(
#bin_path,
load_config
)
.expect("Failed to load the contract");
let response = loaded_contract.deploy_if_not_exists(
&#wallet_name,
::fuels::types::transaction::TxPolicies::default()
)
.await
.expect("Failed to deploy the contract");
#contract_struct_name::new(response.contract_id, #wallet_name.clone())
};
}
})
.reduce(|mut all_code, code| {
all_code.extend(code);
all_code
})
.unwrap_or_default()
}View on GitHub (pinned to d9a250a518)
Solutions
- Build the contract: forc build --release so the binary exists at the path passed to the macro
- Verify the path string and that the .bin file is non-empty
- Wire CI (or a justfile/build.rs) to run forc build before cargo test
Example fix
# before cargo test # panics: "Failed to load the contract" — binary missing # after forc build --release cargo test
Defensive patterns
Strategy: validation
Validate before calling
// Fail fast when the contract binary is missing before deployment-heavy tests
let bin = "tests/contracts/my_contract/out/release/my_contract.bin";
assert!(
std::path::Path::new(bin).exists(),
"contract binary missing — run `forc build --release` first"
); Try / catch
// In manual setup code, propagate instead of expecting:
let loaded = Contract::load_from(bin, LoadConfiguration::default())
.with_context(|| format!("failed to load contract binary at {bin}"))?; Prevention
- Run forc build --release before cargo test (wire it into CI or a justfile)
- Assert binary existence in a lightweight pre-test
- Keep contract paths in one place to avoid typos
When it happens
Trigger: The bin path given in the macro (e.g. "tests/contracts/my_contract/out/release/my_contract.bin") does not exist because forc build was never run, or the .bin file is corrupt/empty.
Common situations: Running cargo test before building contracts; typo in the contract path; CI pipelines that skip the forc build step.
Related errors
- Error while trying to fetch wallets from the custom provider
- Failed to deploy the contract
- failed to read binary: {binary_filepath:?}: {e}
- is there
- abigen generation failed
AI-assisted analysis of FuelLabs/fuels-rs@d9a250a518 (2026-08-16).
Data as JSON: /api/errors/80c2c3d3996f4983.
Report an issue: GitHub.