Skip to content

Commit bfb1cb6

Browse files
authored
fix: add workaround for unlinked artifacts (#10291)
* fix: add workaround for unlinked artifacts * clippy
1 parent b53e6a9 commit bfb1cb6

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/sol-macro-gen/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,6 @@ proc-macro2.workspace = true
2323
quote.workspace = true
2424
syn.workspace = true
2525
prettyplease.workspace = true
26+
serde_json.workspace = true
2627

2728
eyre.workspace = true

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

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use eyre::{Context, OptionExt, Result};
1515
use foundry_common::fs;
1616
use proc_macro2::{Span, TokenStream};
1717
use std::{
18+
env::temp_dir,
1819
fmt::Write,
1920
path::{Path, PathBuf},
2021
str::FromStr,
@@ -82,7 +83,37 @@ impl MultiSolMacroGen {
8283
}
8384

8485
fn generate_binding(instance: &mut SolMacroGen, all_derives: bool) -> Result<()> {
85-
let input = instance.get_sol_input()?.normalize_json()?;
86+
// TODO: in `get_sol_input` we currently can't handle unlinked bytecode: <https://github.com/alloy-rs/core/issues/926>
87+
let input = match instance.get_sol_input() {
88+
Ok(input) => input.normalize_json()?,
89+
Err(error) => {
90+
// TODO(mattsse): remove after <https://github.com/alloy-rs/core/issues/926>
91+
if error.to_string().contains("expected bytecode, found unlinked bytecode") {
92+
// we attempt to do a little hack here until we have this properly supported by
93+
// removing the bytecode objects from the json file and using a tmpfile (very
94+
// hacky)
95+
let content = std::fs::read_to_string(&instance.path)?;
96+
let mut value = serde_json::from_str::<serde_json::Value>(&content)?;
97+
let obj = value.as_object_mut().expect("valid abi");
98+
99+
// clear unlinked bytecode
100+
obj.remove("bytecode");
101+
obj.remove("deployedBytecode");
102+
103+
let tmpdir = temp_dir();
104+
let mut tmp_file = tmpdir.join(instance.path.file_name().unwrap());
105+
std::fs::write(&tmp_file, serde_json::to_string(&value)?)?;
106+
107+
// try again
108+
std::mem::swap(&mut tmp_file, &mut instance.path);
109+
let input = instance.get_sol_input()?.normalize_json()?;
110+
std::mem::swap(&mut tmp_file, &mut instance.path);
111+
input.normalize_json()?
112+
} else {
113+
return Err(error)
114+
}
115+
}
116+
};
86117

87118
let SolInput { attrs: _, path: _, kind } = input;
88119

0 commit comments

Comments
 (0)