Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug(forge)!: strip "revert: " from vm.expectRevert reason #10144

Merged
merged 6 commits into from
Apr 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion crates/cheatcodes/src/test/revert_handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,12 @@ fn handle_revert(
} else {
(&stringify(&actual_revert), &stringify(expected_reason))
};
Err(fmt_err!("Error != expected error: {} != {}", actual, expected,))

if expected == actual {
return Ok(());
}

Err(fmt_err!("Error != expected error: {} != {}", actual, expected))
}
}

Expand Down
115 changes: 72 additions & 43 deletions crates/evm/core/src/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ use crate::abi::{console, Vm};
use alloy_dyn_abi::JsonAbiExt;
use alloy_json_abi::{Error, JsonAbi};
use alloy_primitives::{hex, map::HashMap, Log, Selector};
use alloy_sol_types::{SolEventInterface, SolInterface, SolValue};
use alloy_sol_types::{
ContractError::Revert, RevertReason, RevertReason::ContractError, SolEventInterface,
SolInterface, SolValue,
};
use foundry_common::SELECTOR_LEN;
use itertools::Itertools;
use revm::interpreter::InstructionResult;
Expand Down Expand Up @@ -138,65 +141,91 @@ impl RevertDecoder {
///
/// See [`decode`](Self::decode) for more information.
pub fn maybe_decode(&self, err: &[u8], status: Option<InstructionResult>) -> Option<String> {
let Some((selector, data)) = err.split_first_chunk::<SELECTOR_LEN>() else {
if let Some(status) = status {
if !status.is_ok() {
return Some(format!("EvmError: {status:?}"));
}
}
return if err.is_empty() {
None
} else {
Some(format!("custom error bytes {}", hex::encode_prefixed(err)))
};
};

if let Some(reason) = SkipReason::decode(err) {
return Some(reason.to_string());
}

// Solidity's `Error(string)` or `Panic(uint256)`, or `Vm`'s custom errors.
// Solidity's `Error(string)` (handled separately in order to strip revert: prefix)
if let Some(ContractError(Revert(revert))) = RevertReason::decode(err) {
return Some(revert.reason);
}

// Solidity's `Panic(uint256)` and `Vm`'s custom errors.
if let Ok(e) = alloy_sol_types::ContractError::<Vm::VmErrors>::abi_decode(err, false) {
return Some(e.to_string());
}

// Custom errors.
if let Some(errors) = self.errors.get(selector) {
for error in errors {
// If we don't decode, don't return an error, try to decode as a string later.
if let Ok(decoded) = error.abi_decode_input(data, false) {
return Some(format!(
"{}({})",
error.name,
decoded.iter().map(foundry_common::fmt::format_token).format(", ")
));
let string_decoded = decode_as_non_empty_string(err);

if let Some((selector, data)) = err.split_first_chunk::<SELECTOR_LEN>() {
// Custom errors.
if let Some(errors) = self.errors.get(selector) {
for error in errors {
// If we don't decode, don't return an error, try to decode as a string
// later.
if let Ok(decoded) = error.abi_decode_input(data, false) {
return Some(format!(
"{}({})",
error.name,
decoded.iter().map(foundry_common::fmt::format_token).format(", ")
));
}
}
}
}

// ABI-encoded `string`.
if let Ok(s) = String::abi_decode(err, true) {
return Some(s);
if string_decoded.is_some() {
return string_decoded
}

// Generic custom error.
return Some({
let mut s = format!("custom error {}", hex::encode_prefixed(selector));
if !data.is_empty() {
s.push_str(": ");
match std::str::from_utf8(data) {
Ok(data) => s.push_str(data),
Err(_) => s.push_str(&hex::encode(data)),
}
}
s
})
}

// ASCII string.
if err.is_ascii() {
return Some(std::str::from_utf8(err).unwrap().to_string());
if string_decoded.is_some() {
return string_decoded
}

// Generic custom error.
Some({
let mut s = format!("custom error {}", hex::encode_prefixed(selector));
if !data.is_empty() {
s.push_str(": ");
match std::str::from_utf8(data) {
Ok(data) => s.push_str(data),
Err(_) => s.push_str(&hex::encode(data)),
}
if let Some(status) = status {
if !status.is_ok() {
return Some(format!("EvmError: {status:?}"));
}
s
})
}
if err.is_empty() {
None
} else {
Some(format!("custom error bytes {}", hex::encode_prefixed(err)))
}
}
}

/// Helper function that decodes provided error as an ABI encoded or an ASCII string (if not empty).
fn decode_as_non_empty_string(err: &[u8]) -> Option<String> {
// ABI-encoded `string`.
if let Ok(s) = String::abi_decode(err, true) {
if !s.is_empty() {
return Some(s);
}
}

// ASCII string.
if err.is_ascii() {
let msg = std::str::from_utf8(err).unwrap().to_string();
if !msg.is_empty() {
return Some(msg);
}
}

None
}

fn trimmed_hex(s: &[u8]) -> String {
Expand Down
8 changes: 4 additions & 4 deletions crates/forge/tests/cli/failure_assertions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ forgetest!(expect_revert_tests_should_fail, |prj, cmd| {
[FAIL: next call did not revert as expected] testShouldFailExpectRevertDidNotRevert() ([GAS])
[FAIL: Error != expected error: but reverts with this message != should revert with this message] testShouldFailExpectRevertErrorDoesNotMatch() ([GAS])
[FAIL: next call did not revert as expected] testShouldFailRevertNotOnImmediateNextCall() ([GAS])
[FAIL: revert: some message] testShouldFailexpectCheatcodeRevertForCreate() ([GAS])
[FAIL: revert: revert] testShouldFailexpectCheatcodeRevertForExtCall() ([GAS])
[FAIL: some message] testShouldFailexpectCheatcodeRevertForCreate() ([GAS])
[FAIL: revert] testShouldFailexpectCheatcodeRevertForExtCall() ([GAS])
Suite result: FAILED. 0 passed; 7 failed; 0 skipped; [ELAPSED]
...
"#,
Expand Down Expand Up @@ -238,7 +238,7 @@ forgetest!(mem_safety_test_should_fail, |prj, cmd| {
r#"[COMPILING_FILES] with [SOLC_VERSION]
[SOLC_VERSION] [ELAPSED]
...
[FAIL: revert: Expected call to fail] testShouldFailExpectSafeMemoryCall() ([GAS])
[FAIL: Expected call to fail] testShouldFailExpectSafeMemoryCall() ([GAS])
[FAIL: memory write at offset 0x100 of size 0x60 not allowed; safe range: (0x00, 0x60] U (0x80, 0x100]] testShouldFailExpectSafeMemory_CALL() ([GAS])
[FAIL: memory write at offset 0x100 of size 0x60 not allowed; safe range: (0x00, 0x60] U (0x80, 0x100]] testShouldFailExpectSafeMemory_CALLCODE() ([GAS])
[FAIL: memory write at offset 0xA0 of size 0x20 not allowed; safe range: (0x00, 0x60] U (0x80, 0xA0]; counterexample: calldata=[..] args=[..]] testShouldFailExpectSafeMemory_CALLDATACOPY(uint256) (runs: 0, [AVG_GAS])
Expand Down Expand Up @@ -336,7 +336,7 @@ contract FailingSetupTest is DSTest {
r#"[COMPILING_FILES] with [SOLC_VERSION]
[SOLC_VERSION] [ELAPSED]
...
[FAIL: revert: setup failed predictably] setUp() ([GAS])
[FAIL: setup failed predictably] setUp() ([GAS])
Suite result: FAILED. 0 passed; 1 failed; 0 skipped; [ELAPSED]
...
"#
Expand Down
4 changes: 2 additions & 2 deletions crates/forge/tests/cli/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ forgetest_async!(assert_exit_code_error_on_failure_script, |prj, cmd| {

// run command and assert error exit code
cmd.assert_failure().stderr_eq(str![[r#"
Error: script failed: revert: failed
Error: script failed: failed

"#]]);
});
Expand All @@ -168,7 +168,7 @@ forgetest_async!(assert_exit_code_error_on_failure_script_with_json, |prj, cmd|

// run command and assert error exit code
cmd.assert_failure().stderr_eq(str![[r#"
Error: script failed: revert: failed
Error: script failed: failed

"#]]);
});
Expand Down
20 changes: 10 additions & 10 deletions crates/forge/tests/cli/test_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -893,17 +893,17 @@ Compiler run successful!

Ran 4 tests for test/ReplayFailures.t.sol:ReplayFailuresTest
[PASS] testA() ([GAS])
[FAIL: revert: testB failed] testB() ([GAS])
[FAIL: testB failed] testB() ([GAS])
[PASS] testC() ([GAS])
[FAIL: revert: testD failed] testD() ([GAS])
[FAIL: testD failed] testD() ([GAS])
Suite result: FAILED. 2 passed; 2 failed; 0 skipped; [ELAPSED]

Ran 1 test suite [ELAPSED]: 2 tests passed, 2 failed, 0 skipped (4 total tests)

Failing tests:
Encountered 2 failing tests in test/ReplayFailures.t.sol:ReplayFailuresTest
[FAIL: revert: testB failed] testB() ([GAS])
[FAIL: revert: testD failed] testD() ([GAS])
[FAIL: testB failed] testB() ([GAS])
[FAIL: testD failed] testD() ([GAS])

Encountered a total of 2 failing tests, 2 tests succeeded

Expand All @@ -917,16 +917,16 @@ Encountered a total of 2 failing tests, 2 tests succeeded
No files changed, compilation skipped

Ran 2 tests for test/ReplayFailures.t.sol:ReplayFailuresTest
[FAIL: revert: testB failed] testB() ([GAS])
[FAIL: revert: testD failed] testD() ([GAS])
[FAIL: testB failed] testB() ([GAS])
[FAIL: testD failed] testD() ([GAS])
Suite result: FAILED. 0 passed; 2 failed; 0 skipped; [ELAPSED]

Ran 1 test suite [ELAPSED]: 0 tests passed, 2 failed, 0 skipped (2 total tests)

Failing tests:
Encountered 2 failing tests in test/ReplayFailures.t.sol:ReplayFailuresTest
[FAIL: revert: testB failed] testB() ([GAS])
[FAIL: revert: testD failed] testD() ([GAS])
[FAIL: testB failed] testB() ([GAS])
[FAIL: testD failed] testD() ([GAS])

Encountered a total of 2 failing tests, 0 tests succeeded

Expand Down Expand Up @@ -2123,8 +2123,8 @@ forgetest_init!(should_generate_junit_xml_report, |prj, cmd| {
<system-out>[FAIL: panic: assertion failed (0x01)] test_junit_assert_fail() ([GAS])</system-out>
</testcase>
<testcase name="test_junit_revert_fail()" time="[..]">
<failure message="revert: Revert"/>
<system-out>[FAIL: revert: Revert] test_junit_revert_fail() ([GAS])</system-out>
<failure message="Revert"/>
<system-out>[FAIL: Revert] test_junit_revert_fail() ([GAS])</system-out>
</testcase>
<system-out>Suite result: FAILED. 0 passed; 2 failed; 0 skipped; [ELAPSED]</system-out>
</testsuite>
Expand Down
Loading