foundry
foundry copied to clipboard
Extend error traces to include source code line number
Component
Forge
Describe the feature you would like
Current behaviour
Error traces in failing tests show only the contract and method where the respective error was thrown:
Desired behaviour
Extend the error traces from tests (or other tasks) to include the line number in the contract's solidity source code which corresponds to the compiled bytecode where the error / revertion was thrown. Ideally this should work both in the case of locally compiled contracts as well as contracts verified on etherscan.
Hardhat does this for locally compiled contracts:

Additional context
I tried to produce the desired behaviour in forge with different verbosity levels and also couldn't find any corresponding option in forge test --help.
Please lmk if this feature already exists (in which case we might want to extend the documentation)
I'd love to take a shot at implementing this, if it is considered a desirable feature.
It would be cool but it's really really hard to do. You would need to keep track of the instruction counter (not the program counter), identify the contract, look up the source range in the source map, translate that to a line and display that. Additionally, it's somewhat unclear how this would work in the following situations:
- If you use DSTest-style assertions, then there is no revert. What line should be displayed in this case? If we just display the line number of the function then I'd argue what we have now is already good enough
- If you use assertion libraries or inherited assertion helpers, what line should be displayed? If you just track the point of the revert, the line would show up as being inside the assertion helper or assertion library, not inside the test. In this case you would need to construct a backtrace somehow
But it is definitely something we've talked about so feel free to take a stab at it :)
It would be cool but it's really really hard to do. You would need to keep track of the instruction counter (not the program counter), identify the contract, look up the source range in the source map, translate that to a line and display that. Additionally, it's somewhat unclear how this would work in the following situations:
- If you use DSTest-style assertions, then there is no revert. What line should be displayed in this case? If we just display the line number of the function then I'd argue what we have now is already good enough
- If you use assertion libraries or inherited assertion helpers, what line should be displayed? If you just track the point of the revert, the line would show up as being inside the assertion helper or assertion library, not inside the test. In this case you would need to construct a backtrace somehow
But it is definitely something we've talked about so feel free to take a stab at it :)
Thanks for the info 👍 . I had suspected already that there was a good reason that this isn't implemented already. I think I'll be looking for an easier issue to start contributing. :)