foundry
foundry copied to clipboard
Nested library linking results in duplicate deployments
Component
Forge
Have you ensured that all of these are up to date?
- [X] Foundry
- [X] Foundryup
What version of Foundry are you on?
forge 0.2.0 (9b0575e 2022-08-04T00:10:43.515642Z)
What command(s) is the bug in?
forge test
Operating System
macOS (Intel)
Describe the bug
When a library calls another library, the second library gets re-deployed to a new address.
Given the code
library Library {
function foo() external pure returns (uint256) {
return 42;
}
}
library Library2 {
function foo() external pure returns (uint256) {
return Library.foo();
}
}
contract Contract {
function foo() external pure returns (uint256) {
return Library2.foo();
}
function bar() external pure returns (uint256) {
return Library.foo();
}
}
contract TestLibrary is Test {
Contract c = new Contract();
function testFoo() public {
console.log(address(Library), "lib 1");
console.log(address(Library2), "lib 2");
assertEq(c.foo(), 42);
assertEq(c.bar(), 42);
assertEq(Library.foo(), 42);
assertEq(Library2.foo(), 42);
}
}
The outcome is

As can be seen, most of the calls to the librares go to the expected addresses (7e84 & f221), except when Library2 calls Library, in that case there's a second copy of Library in 0x62d6…3691
If I link the lib to a pre-defined address in foundry.toml then there's no issue, been using that as a workaround (I then etch the lib code) but of course is not ideal.
I assume this can be also a problem with deployment scripts where the same lib may be deployed multiple times.
Good find!
Fixed with https://github.com/foundry-rs/foundry/pull/5164 💆
see output: