foundry icon indicating copy to clipboard operation
foundry copied to clipboard

`public` functions of a library (i.e. linked library) can no longer be used in tests.

Open minaminao opened this issue 3 years ago • 2 comments

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 (8016f26 2022-08-02T00:17:15.28384Z)

What command(s) is the bug in?

forge test

Operating System

macOS (Intel)

Describe the bug

Full source code: https://github.com/minaminao/foundry-linked-library-bug Discussion: #2534

Contract.sol:

// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;

library Library {
    function f(uint256 a, uint256 b) public pure returns (uint256) {
        return a + b;
    }
}

contract Contract {
    uint256 c;

    constructor() {
        c = Library.f(1, 2);
    }
}

Contract.t.sol:

// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;

import "forge-std/Test.sol";
import "src/Contract.sol";

contract ContractTest is Test {
    function setUp() public {
        vm.createSelectFork("https://rpc.ankr.com/eth_goerli", 7329883);
    }

    function test() public {
        new Contract();
    }
}

Traces of forge test -vvvv:

Traces:
  [3040] ContractTest::setUp() 
    ├─ [0] VM::createSelectFork("https://rpc.ankr.com/eth_goerli", 7329883) 
    │   └─ ← 1
    └─ ← ()

  [35239] ContractTest::test() 
    ├─ [2893] → new <Unknown>@"0x1866…2400"
    │   ├─ [0] Library::f(1, 2) [delegatecall]
    │   │   └─ ← ()
    │   └─ ← 0 bytes of code
    └─ ← "EvmError: Revert"

Test result: FAILED. 0 passed; 1 failed; finished in 8.25s

Failed tests:
[FAIL. Reason: Contract 0xb4c79daB8f259C7Aee6E5b2Aa729821864227e84 does not exists on active fork with id `1`
        But exists on non active forks: `[0]`] test() (gas: 35239)

Encountered a total of 1 failing tests, 0 tests succeeded

minaminao avatar Aug 02 '22 05:08 minaminao

It seems to be related to #2301.

minaminao avatar Aug 02 '22 05:08 minaminao

Any idea on what could be the cause here? Had a look at the referenced pr, but can't seem to figure out what could be the cause.

Getting more and more reports as ppl update to newest nightly breaking the aave governance/protocol utility libraries. Also due to https://github.com/foundry-rs/foundry/discussions/2557 the nightly we recommended ppl to downgrade to now is no longer available (not sure if any of the available still works).

sakulstra avatar Aug 05 '22 11:08 sakulstra

Libraries are deployed before the test contract, so the likely cause is that we don't persist libraries we deploy ourselves across forks. cc @mattsse

onbjerg avatar Aug 11 '22 18:08 onbjerg