Skip to content

Commit 197586f

Browse files
mablrgrandizzy
andauthored
feat(forge build): cache project selectors by default (#10651)
* feat(forge build): cache project selectors by default Calls `cache_local_signatures` function like the "forge selectors cache" command. * refactor(forge build): update `cache_local_signatures` to accept an optional `cache_dir` No more need to unwrap the option value returned foundry's `config` crate * Move cache dir in cache sigs fn --------- Co-authored-by: grandizzy <[email protected]>
1 parent 788ba28 commit 197586f

File tree

4 files changed

+50
-4
lines changed

4 files changed

+50
-4
lines changed

crates/cli/src/utils/cmd.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,10 @@ pub async fn print_traces(
440440

441441
/// Traverse the artifacts in the project to generate local signatures and merge them into the cache
442442
/// file.
443-
pub fn cache_local_signatures(output: &ProjectCompileOutput, cache_dir: &Path) -> Result<()> {
443+
pub fn cache_local_signatures(output: &ProjectCompileOutput) -> Result<()> {
444+
let Some(cache_dir) = Config::foundry_cache_dir() else {
445+
eyre::bail!("Failed to get `cache_dir` to generate local signatures.");
446+
};
444447
let path = cache_dir.join("signatures");
445448
let mut signatures = SignaturesCache::load(&path);
446449
for (_, artifact) in output.artifacts() {

crates/forge/src/cmd/build.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
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::{
5+
opts::BuildOpts,
6+
utils::{cache_local_signatures, LoadConfig},
7+
};
58
use foundry_common::{compile::ProjectCompiler, shell};
69
use foundry_compilers::{
710
compilers::{multi::MultiCompilerLanguage, Language},
@@ -100,6 +103,9 @@ impl BuildArgs {
100103

101104
let output = compiler.compile(&project)?;
102105

106+
// Cache project selectors.
107+
cache_local_signatures(&output)?;
108+
103109
if format_json && !self.names && !self.sizes {
104110
sh_println!("{}", serde_json::to_string_pretty(&output.output())?)?;
105111
}

crates/forge/src/cmd/selectors.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use foundry_common::{
1111
selectors::{import_selectors, SelectorImportData},
1212
};
1313
use foundry_compilers::{artifacts::output_selection::ContractOutputSelection, info::ContractInfo};
14-
use foundry_config::Config;
1514
use std::fs::canonicalize;
1615

1716
/// CLI arguments for `forge selectors`.
@@ -95,7 +94,7 @@ impl SelectorsSubcommands {
9594
// compile the project to get the artifacts/abis
9695
let project = build_args.project()?;
9796
let outcome = ProjectCompiler::new().quiet(true).compile(&project)?;
98-
cache_local_signatures(&outcome, &Config::foundry_cache_dir().unwrap())?
97+
cache_local_signatures(&outcome)?;
9998
}
10099
Self::Upload { contract, all, project_paths } => {
101100
let build_args = BuildOpts {

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` to check that the project selectors are cached by default.
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"]).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)