Skip to content

Conversation

VolodymyrBg
Copy link
Contributor

Before: Dependencies were built by shallow-merging link_references maps via extend. When both creation and deployed bytecode contained references under the same file key but with different library names, the latter overwrote the former, dropping dependencies.

After: We deep-merge the inner maps per file, unioning library names and offsets from both sources. This matches how we link and validate both bytecode objects and aligns with forge inspect ... libraries (union semantics). Prevents missing libs when a file has libraries used exclusively in constructor vs runtime.

@DaniPopes
Copy link
Member

Is there a test case we could add for this that would fail before?

@klkvr thoughts?

@VolodymyrBg
Copy link
Contributor Author

Is there a test case we could add for this that would fail before?

@klkvr thoughts?

Yes, added a test case

Copy link
Member

@klkvr klkvr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, left a suggestion for driveby clean up

also need to commit the test files and investigate whether the linking tests are actually testing anything


#[test]
fn link_samefile_union() {
link_test("../../testdata/default/linking/samefile_union", |linker| {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like you didn't commit the linking/samefile_union but the test is still passing somehow 🤔

Comment on lines 111 to 118
let mut references: BTreeMap<String, BTreeMap<String, Vec<Offsets>>> = BTreeMap::new();
if let Some(bytecode) = &contract.bytecode {
references.extend(bytecode.link_references.clone());
for (file, libs) in &bytecode.link_references {
let entry = references.entry(file.clone()).or_default();
for (name, offsets) in libs {
entry.entry(name.clone()).or_default().extend(offsets.clone());
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, the issue is that we were not supposed to use extend here bc it would make second cycle's values overwrite the file keys

this does make sense

it seems that we accumulate offsets in this map for no reason, do you mind also getting rid of this and changing references to BTreeMap<String, BTreeSet<String>> so that it's a bit easier to reason about?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, the issue is that we were not supposed to use extend here bc it would make second cycle's values overwrite the file keys

this does make sense

it seems that we accumulate offsets in this map for no reason, do you mind also getting rid of this and changing references to BTreeMap<String, BTreeSet<String>> so that it's a bit easier to reason about?

done, also added a test if artifacts are missing and committed samefile_union

Copy link
Member

@klkvr klkvr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs rebase and ci fixes

@VolodymyrBg VolodymyrBg force-pushed the fix/linking-deep-merge-link-references branch from 554024c to 50d62f7 Compare September 24, 2025 14:07
@VolodymyrBg
Copy link
Contributor Author

needs rebase and ci fixes

test / nextest / test unit (x86_64-pc-windows-msvc) (pull_request)
test / nextest / test unit (x86_64-pc-windows-msvc) (pull_request)Failing after 6m
test / nextest / test unit (x86_64-unknown-linux-gnu) (pull_request)
test / nextest / test unit (x86_64-unknown-linux-gnu) (pull_request)Failing after 2m

I'm sorry but i can't fix this, i can't just test it on my computer, it's not that powerful, and I think making noise here is not the best idea..

),
(
"default/linking/duplicate/Duplicate.t.sol:C",
"default/linking/duplicate/Duplicate.t.sol:C".to_string(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please undo these changes, you accepted the wrong side when merging master

@VolodymyrBg VolodymyrBg force-pushed the fix/linking-deep-merge-link-references branch from 7625f36 to 514142f Compare September 24, 2025 19:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

3 participants