Skip to content

Commit 4d1b824

Browse files
DaniPopesrplusq
authored andcommitted
chore: add some more debugging to forge bind (foundry-rs#9345)
1 parent 43de8e3 commit 4d1b824

File tree

1 file changed

+41
-29
lines changed

1 file changed

+41
-29
lines changed

crates/sol-macro-gen/src/sol_macro_gen.rs

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
1212
use alloy_sol_macro_expander::expand::expand;
1313
use alloy_sol_macro_input::{SolInput, SolInputKind};
14-
use eyre::{Context, Ok, OptionExt, Result};
14+
use eyre::{Context, OptionExt, Result};
1515
use foundry_common::fs;
1616
use proc_macro2::{Span, TokenStream};
1717
use std::{
@@ -39,7 +39,7 @@ impl SolMacroGen {
3939
#path
4040
};
4141

42-
let sol_input: SolInput = syn::parse2(tokens).wrap_err("Failed to parse SolInput {e}")?;
42+
let sol_input: SolInput = syn::parse2(tokens).wrap_err("failed to parse input")?;
4343

4444
Ok(sol_input)
4545
}
@@ -69,24 +69,35 @@ impl MultiSolMacroGen {
6969

7070
pub fn generate_bindings(&mut self) -> Result<()> {
7171
for instance in &mut self.instances {
72-
let input = instance.get_sol_input()?.normalize_json()?;
72+
Self::generate_binding(instance).wrap_err_with(|| {
73+
format!(
74+
"failed to generate bindings for {}:{}",
75+
instance.path.display(),
76+
instance.name
77+
)
78+
})?;
79+
}
7380

74-
let SolInput { attrs: _attrs, path: _path, kind } = input;
81+
Ok(())
82+
}
7583

76-
let tokens = match kind {
77-
SolInputKind::Sol(mut file) => {
78-
let sol_attr: syn::Attribute = syn::parse_quote! {
79-
#[sol(rpc, alloy_sol_types = alloy::sol_types, alloy_contract = alloy::contract)]
80-
};
81-
file.attrs.push(sol_attr);
82-
expand(file).wrap_err("Failed to expand SolInput")?
83-
}
84-
_ => unreachable!(),
85-
};
84+
fn generate_binding(instance: &mut SolMacroGen) -> Result<()> {
85+
let input = instance.get_sol_input()?.normalize_json()?;
8686

87-
instance.expansion = Some(tokens);
88-
}
87+
let SolInput { attrs: _, path: _, kind } = input;
8988

89+
let tokens = match kind {
90+
SolInputKind::Sol(mut file) => {
91+
let sol_attr: syn::Attribute = syn::parse_quote! {
92+
#[sol(rpc, alloy_sol_types = alloy::sol_types, alloy_contract = alloy::contract)]
93+
};
94+
file.attrs.push(sol_attr);
95+
expand(file).wrap_err("failed to expand")?
96+
}
97+
_ => unreachable!(),
98+
};
99+
100+
instance.expansion = Some(tokens);
90101
Ok(())
91102
}
92103

@@ -139,27 +150,28 @@ edition = "2021"
139150
)?;
140151

141152
// Write src
153+
let parse_error = |name: &str| {
154+
format!("failed to parse generated tokens as an AST for {name};\nthis is likely a bug")
155+
};
142156
for instance in &self.instances {
143-
let name = instance.name.to_lowercase();
144-
let contents = instance.expansion.as_ref().unwrap().to_string();
157+
let contents = instance.expansion.as_ref().unwrap();
145158

146-
if !single_file {
147-
let path = src.join(format!("{name}.rs"));
148-
let file = syn::parse_file(&contents)?;
149-
let contents = prettyplease::unparse(&file);
150-
151-
fs::write(path.clone(), contents).wrap_err("Failed to write file")?;
152-
writeln!(&mut lib_contents, "pub mod {name};")?;
153-
} else {
159+
let name = instance.name.to_lowercase();
160+
let path = src.join(format!("{name}.rs"));
161+
let file = syn::parse2(contents.clone())
162+
.wrap_err_with(|| parse_error(&format!("{}:{}", path.display(), name)))?;
163+
let contents = prettyplease::unparse(&file);
164+
if single_file {
154165
write!(&mut lib_contents, "{contents}")?;
166+
} else {
167+
fs::write(path, contents).wrap_err("failed to write to file")?;
168+
writeln!(&mut lib_contents, "pub mod {name};")?;
155169
}
156170
}
157171

158172
let lib_path = src.join("lib.rs");
159-
let lib_file = syn::parse_file(&lib_contents)?;
160-
173+
let lib_file = syn::parse_file(&lib_contents).wrap_err_with(|| parse_error("lib.rs"))?;
161174
let lib_contents = prettyplease::unparse(&lib_file);
162-
163175
fs::write(lib_path, lib_contents).wrap_err("Failed to write lib.rs")?;
164176

165177
Ok(())

0 commit comments

Comments
 (0)