Skip to content

Commit b28c4e5

Browse files
committed
feat(forge build): add --selectors_cache option
This option triggers a call to `cache_local_signatures` function like the "forge selectors cache" command.
1 parent 65650a6 commit b28c4e5

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

crates/forge/src/cmd/build.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use super::{install, watch::WatchArgs};
22
use clap::Parser;
33
use eyre::Result;
4-
use foundry_cli::{opts::BuildOpts, utils::LoadConfig};
4+
use foundry_cli::{opts::BuildOpts, utils::{cache_local_signatures, LoadConfig}};
55
use foundry_common::{compile::ProjectCompiler, shell};
66
use foundry_compilers::{
77
compilers::{multi::MultiCompilerLanguage, Language},
@@ -56,6 +56,11 @@ pub struct BuildArgs {
5656
#[serde(skip)]
5757
pub ignore_eip_3860: bool,
5858

59+
/// Cache project selectors (enables trace with local contracts functions and events).
60+
#[arg(long)]
61+
#[serde(skip)]
62+
pub selectors_cache: bool,
63+
5964
#[command(flatten)]
6065
#[serde(flatten)]
6166
pub build: BuildOpts,
@@ -100,6 +105,10 @@ impl BuildArgs {
100105

101106
let output = compiler.compile(&project)?;
102107

108+
if self.selectors_cache {
109+
cache_local_signatures(&output, &Config::foundry_cache_dir().unwrap())?
110+
}
111+
103112
if format_json && !self.names && !self.sizes {
104113
sh_println!("{}", serde_json::to_string_pretty(&output.output())?)?;
105114
}

crates/forge/tests/cli/test_cmd.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3828,3 +3828,41 @@ Encountered a total of 1 failing tests, 0 tests succeeded
38283828
38293829
"#]]);
38303830
});
3831+
3832+
// This test is a copy of `error_event_decode_with_cache` in cast/tests/cli/selectors.rs
3833+
// but it uses `forge build --selectors-cache` to build the project and cache the project selectors.
3834+
forgetest_init!(build_with_selectors_cache, |prj, cmd| {
3835+
prj.add_source(
3836+
"LocalProjectContract",
3837+
r#"
3838+
contract ContractWithCustomError {
3839+
error AnotherValueTooHigh(uint256, address);
3840+
event MyUniqueEventWithinLocalProject(uint256 a, address b);
3841+
}
3842+
"#,
3843+
)
3844+
.unwrap();
3845+
// Build and cache project selectors.
3846+
cmd.forge_fuse().args(["build", "--selectors-cache"]).assert_success();
3847+
3848+
// Assert cast can decode custom error with local cache.
3849+
cmd.cast_fuse()
3850+
.args(["decode-error", "0x7191bc6200000000000000000000000000000000000000000000000000000000000000650000000000000000000000000000000000000000000000000000000000D0004F"])
3851+
.assert_success()
3852+
.stdout_eq(str![[r#"
3853+
AnotherValueTooHigh(uint256,address)
3854+
101
3855+
0x0000000000000000000000000000000000D0004F
3856+
3857+
"#]]);
3858+
// Assert cast can decode event with local cache.
3859+
cmd.cast_fuse()
3860+
.args(["decode-event", "0xbd3699995dcc867b64dbb607be2c33be38df9134bef1178df13bfb9446e73104000000000000000000000000000000000000000000000000000000000000004e00000000000000000000000000000000000000000000000000000dd00000004e"])
3861+
.assert_success()
3862+
.stdout_eq(str![[r#"
3863+
MyUniqueEventWithinLocalProject(uint256,address)
3864+
78
3865+
0x00000000000000000000000000000DD00000004e
3866+
3867+
"#]]);
3868+
});

0 commit comments

Comments
 (0)