Skip to content

Commit

Permalink
track link error for all libs
Browse files Browse the repository at this point in the history
Signed-off-by: 9547 <[email protected]>
  • Loading branch information
9547 committed Jan 9, 2025
1 parent 6fc79fe commit 9183133
Showing 1 changed file with 14 additions and 16 deletions.
30 changes: 14 additions & 16 deletions crates/linking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ pub enum LinkerError {
InvalidAddress(<Address as std::str::FromStr>::Err),
#[error("cyclic dependency found, can't link libraries via CREATE2")]
CyclicDependency,
#[error("linking failed for library {name} at {file}")]
LinkingFailed { file: String, name: String },
#[error("linking failed for library at {file}")]
LinkingFailed { file: String },
}

pub struct Linker<'a> {
Expand Down Expand Up @@ -252,27 +252,26 @@ impl<'a> Linker<'a> {
let mut contract =
self.contracts.get(target).ok_or(LinkerError::MissingTargetArtifact)?.clone();
for (file, libs) in &libraries.libs {
// Track if any linking succeeded
let mut linked = false;
for (name, address) in libs {
let address = Address::from_str(address).map_err(LinkerError::InvalidAddress)?;

if let Some(bytecode) = contract.bytecode.as_mut() {
if !bytecode.to_mut().link(&file.to_string_lossy(), name, address) {
return Err(LinkerError::LinkingFailed {
file: file.to_string_lossy().into(),
name: name.clone(),
});
}
linked |= bytecode.to_mut().link(&file.to_string_lossy(), name, address);
}
if let Some(deployed_bytecode) =
contract.deployed_bytecode.as_mut().and_then(|b| b.to_mut().bytecode.as_mut())
{
if !deployed_bytecode.link(&file.to_string_lossy(), name, address) {
return Err(LinkerError::LinkingFailed {
file: file.to_string_lossy().into(),
name: name.clone(),
});
}
linked |= deployed_bytecode.link(&file.to_string_lossy(), name, address);
}
}

if !linked {
return Err(LinkerError::LinkingFailed {
file: file.to_string_lossy().into_owned(),
});
}
}
Ok(contract)
}
Expand Down Expand Up @@ -723,9 +722,8 @@ mod tests {

// Verify we get a LinkingFailed error
match result {
Err(LinkerError::LinkingFailed { file, name }) => {
Err(LinkerError::LinkingFailed { file }) => {
assert_eq!(file, "default/linking/simple/Simple.t.sol");
assert_eq!(name, "NonExistentLib");
}
_ => panic!("Expected LinkingFailed error, got: {result:?}"),
}
Expand Down

0 comments on commit 9183133

Please sign in to comment.