foundry icon indicating copy to clipboard operation
foundry copied to clipboard

Extend error traces to include source code line number

Open ckoopmann opened this issue 3 years ago • 3 comments

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:

image

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: image

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)

ckoopmann avatar Jul 15 '22 07:07 ckoopmann

I'd love to take a shot at implementing this, if it is considered a desirable feature.

ckoopmann avatar Jul 15 '22 07:07 ckoopmann

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 :)

onbjerg avatar Jul 16 '22 21:07 onbjerg

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. :)

ckoopmann avatar Jul 25 '22 02:07 ckoopmann