Skip to content

Commit b084a14

Browse files
authored
Fix forge bind with --resolc (#132)
* fix forge bind
1 parent a95c9a7 commit b084a14

File tree

4 files changed

+53
-35
lines changed

4 files changed

+53
-35
lines changed

crates/config/src/revive.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@ use crate::{Config, SolcReq};
99
/// Filename for resolc cache
1010
pub const RESOLC_SOLIDITY_FILES_CACHE_FILENAME: &str = "resolc-solidity-files-cache.json";
1111

12-
/// Directory for resolc artifacts
13-
pub const RESOLC_ARTIFACTS_DIR: &str = "resolc-out";
14-
1512
pub const CONTRACT_SIZE_LIMIT: usize = 250_000;
1613

1714
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Default, Deserialize)]
@@ -41,13 +38,13 @@ impl ResolcConfig {
4138
.sources(&config.src)
4239
.tests(&config.test)
4340
.scripts(&config.script)
44-
.artifacts(config.root.join(RESOLC_ARTIFACTS_DIR))
4541
.libs(config.libs.iter())
4642
.remappings(config.get_all_remappings())
4743
.allowed_path(&config.root)
4844
.allowed_paths(&config.libs)
4945
.allowed_paths(&config.allow_paths)
50-
.include_paths(&config.include_paths);
46+
.include_paths(&config.include_paths)
47+
.artifacts(&config.out);
5148

5249
builder.build_with_root(&config.root)
5350
}

crates/forge/src/args.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,10 @@ pub fn run_command(args: Forge) -> Result<()> {
105105
Ok(())
106106
}
107107
ForgeSubcommand::Clean { root } => {
108-
let mut config = utils::load_config_with_root(root.as_deref())?;
108+
let config = utils::load_config_with_root(root.as_deref())?;
109109
let project = config.project()?;
110110
config.cleanup(&project)?;
111111

112-
config.resolc.resolc_compile = true;
113-
let resolc_project = config.project()?;
114-
config.cleanup(&resolc_project)?;
115-
116112
Ok(())
117113
}
118114
ForgeSubcommand::Snapshot(cmd) => {

crates/forge/src/cmd/bind.rs

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use foundry_common::{compile::ProjectCompiler, fs::json_files};
77
use foundry_config::impl_figment_convert;
88
use regex::Regex;
99
use std::{
10+
collections::BTreeSet,
1011
fs,
1112
path::{Path, PathBuf},
1213
};
@@ -112,31 +113,58 @@ pub struct BindArgs {
112113
}
113114

114115
impl BindArgs {
115-
pub fn run(self) -> Result<()> {
116+
pub fn run(mut self) -> Result<()> {
116117
if self.ethers {
117118
eyre::bail!("`--ethers` bindings have been removed. Use `--alloy` (default) instead.");
118119
}
119-
120120
if !self.skip_build {
121121
let project = self.build.project()?;
122-
let _ = ProjectCompiler::new().compile(&project)?;
122+
let old_builds: BTreeSet<String> = {
123+
if let Ok(entries) = std::fs::read_dir(&project.paths.build_infos) {
124+
entries
125+
.filter_map(|x| x.ok())
126+
.filter_map(|f| {
127+
let path = f.path();
128+
if path.is_file() {
129+
path.file_stem().map(|x| x.to_string_lossy().into_owned())
130+
} else {
131+
None
132+
}
133+
})
134+
.collect()
135+
} else {
136+
BTreeSet::new()
137+
}
138+
};
139+
let output = ProjectCompiler::new().compile(&project)?;
140+
141+
let new_builds: BTreeSet<String> =
142+
output.builds().map(|(key, _)| key).cloned().collect();
143+
144+
// if build_infos got overwritten => different compiler was used.
145+
// in case of other possible errors(e.g. modified file we go as usual)
146+
if old_builds.is_disjoint(&new_builds) {
147+
self.overwrite = true;
148+
}
123149
}
124150

125151
let config = self.load_config()?;
126-
let artifacts = config.out;
152+
let project = config.project()?;
153+
154+
let artifacts = project.artifacts_path();
127155
let bindings_root = self.bindings.clone().unwrap_or_else(|| artifacts.join("bindings"));
128156

129157
if bindings_root.exists() {
130158
if !self.overwrite {
131159
sh_println!("Bindings found. Checking for consistency.")?;
132-
return self.check_existing_bindings(&artifacts, &bindings_root);
160+
return self.check_existing_bindings(artifacts, &bindings_root);
133161
}
134162

135163
trace!(?artifacts, "Removing existing bindings");
136164
fs::remove_dir_all(&bindings_root)?;
137165
}
138166

139-
self.generate_bindings(&artifacts, &bindings_root)?;
167+
self.generate_bindings(artifacts, &bindings_root)?;
140168

141169
sh_println!("Bindings have been generated to {}", bindings_root.display())?;
142170
Ok(())

crates/forge/tests/cli/revive_cmd.rs

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ forgetest_init!(can_clean_config, |prj, cmd| {
2525
// prj.update_config(|config| config.out = "custom-out".into());
2626
cmd.args(["build", "--resolc"]).assert_success();
2727

28-
let artifact = prj.root().join(format!("resolc-out/{TEMPLATE_TEST_CONTRACT_ARTIFACT_JSON}"));
28+
let artifact = prj.artifacts().join(TEMPLATE_TEST_CONTRACT_ARTIFACT_JSON);
2929
assert!(artifact.exists());
3030

3131
cmd.forge_fuse().arg("clean").assert_empty_stdout();
@@ -43,14 +43,14 @@ contract Foo {}
4343
.unwrap();
4444

4545
// compile with solc
46-
cmd.args(["build", "--out=resolc-out"]).assert_success().stdout_eq(str![[r#"
46+
cmd.args(["build"]).assert_success().stdout_eq(str![[r#"
4747
[COMPILING_FILES] with [SOLC_VERSION]
4848
[SOLC_VERSION] [ELAPSED]
4949
Compiler run successful!
5050
5151
"#]]);
5252

53-
let artifact = prj.root().join("resolc-out/");
53+
let artifact = prj.artifacts();
5454
assert!(artifact.exists());
5555

5656
// compile with resolc to the same output dir (resolc has hardcoded output dir)
@@ -62,7 +62,7 @@ Compiler run successful!
6262
"#]]);
6363

6464
// compile again with solc to the same output dir
65-
cmd.forge_fuse().args(["build", "--out=resolc-out"]).assert_success().stdout_eq(str![[r#"
65+
cmd.forge_fuse().args(["build"]).assert_success().stdout_eq(str![[r#"
6666
[COMPILING_FILES] with [SOLC_VERSION]
6767
[SOLC_VERSION] [ELAPSED]
6868
Compiler run successful!
@@ -84,7 +84,7 @@ Compiler run successful!
8484
"#]
8585
]);
8686

87-
let artifact_path = prj.root().join(format!("resolc-out/{CONTRACT_ARTIFACT_JSON}"));
87+
let artifact_path = prj.artifacts().join(CONTRACT_ARTIFACT_JSON);
8888
let artifact: ConfigurableContractArtifact =
8989
foundry_compilers::utils::read_json_file(&artifact_path).unwrap();
9090
assert!(artifact.metadata.is_some());
@@ -100,8 +100,7 @@ Compiler run successful!
100100
101101
"#]]);
102102

103-
let metadata_path =
104-
prj.root().join(format!("resolc-out/{CONTRACT_ARTIFACT_BASE}.metadata.json"));
103+
let metadata_path = prj.artifacts().join(format!("{CONTRACT_ARTIFACT_BASE}.metadata.json"));
105104
let _artifact: Metadata = foundry_compilers::utils::read_json_file(&metadata_path).unwrap();
106105
});
107106

@@ -125,7 +124,7 @@ Compiler run successful!
125124
126125
"#]]);
127126

128-
let artifact_path = prj.root().join(format!("resolc-out/{CONTRACT_ARTIFACT_JSON}"));
127+
let artifact_path = prj.artifacts().join(CONTRACT_ARTIFACT_JSON);
129128
let artifact: ConfigurableContractArtifact =
130129
foundry_compilers::utils::read_json_file(&artifact_path).unwrap();
131130
assert!(artifact.metadata.is_some());
@@ -151,14 +150,13 @@ Compiler run successful!
151150
152151
"#]]);
153152

154-
let metadata_path =
155-
prj.root().join(format!("resolc-out/{CONTRACT_ARTIFACT_BASE}.metadata.json"));
153+
let metadata_path = prj.artifacts().join(format!("{CONTRACT_ARTIFACT_BASE}.metadata.json"));
156154
let _artifact: Metadata = foundry_compilers::utils::read_json_file(&metadata_path).unwrap();
157155

158-
let iropt = prj.root().join(format!("resolc-out/{CONTRACT_ARTIFACT_BASE}.iropt"));
156+
let iropt = prj.artifacts().join(format!("{CONTRACT_ARTIFACT_BASE}.iropt"));
159157
std::fs::read_to_string(iropt).unwrap();
160158

161-
let ir = prj.root().join(format!("resolc-out/{CONTRACT_ARTIFACT_BASE}.ir"));
159+
let ir = prj.artifacts().join(format!("{CONTRACT_ARTIFACT_BASE}.ir"));
162160
std::fs::read_to_string(ir).unwrap();
163161
});
164162

@@ -389,7 +387,7 @@ Compiler run successful!
389387
390388
"#]]);
391389

392-
let artifact = prj.root().join(format!("resolc-out/{CONTRACT_ARTIFACT_JSON}"));
390+
let artifact = prj.artifacts().join(CONTRACT_ARTIFACT_JSON);
393391
assert!(artifact.exists());
394392
let cache = prj.root().join("cache/resolc-solidity-files-cache.json");
395393
assert!(cache.exists());
@@ -892,8 +890,7 @@ forgetest!(inspect_custom_counter_method_identifiers_for_resolc, |prj, cmd| {
892890
forgetest!(can_bind_for_resolc, |prj, cmd| {
893891
init_prj(&prj);
894892

895-
//TODO: bind command is looking for artifacts in wrong directory
896-
cmd.args(["bind", "--resolc", "--out", "resolc-out"]).assert_success().stdout_eq(str![[r#"
893+
cmd.args(["bind", "--resolc"]).assert_success().stdout_eq(str![[r#"
897894
[COMPILING_FILES] with [RESOLC_VERSION]
898895
[RESOLC_VERSION] [ELAPSED]
899896
Compiler run successful!
@@ -927,13 +924,13 @@ forgetest!(can_bind_enum_modules_for_resolc, |prj, cmd| {
927924
.unwrap();
928925

929926
//TODO: bind command is looking for artifacts in wrong directory
930-
cmd.args(["bind", "--resolc", "--out", "resolc-out", "--select", "^Enum$"])
931-
.assert_success()
932-
.stdout_eq(str![[r#"[COMPILING_FILES] with [RESOLC_VERSION]
927+
cmd.args(["bind", "--resolc", "--select", "^Enum$"]).assert_success().stdout_eq(str![[
928+
r#"[COMPILING_FILES] with [RESOLC_VERSION]
933929
[RESOLC_VERSION] [ELAPSED]
934930
Compiler run successful!
935931
Generating bindings for 1 contracts
936-
Bindings have been generated to [..]"#]]);
932+
Bindings have been generated to [..]"#
933+
]]);
937934
});
938935

939936
// Tests that direct import paths are handled correctly

0 commit comments

Comments
 (0)