Skip to content

Ethers messes with async stack traces #4181

Open
@fvictorio

Description

@fvictorio

Ethers Version

6.6.0

Search Terms

stack trace traces async frames file line number

Describe the Problem

Ethers doesn't work well with async stack traces, which means that you usually lose the line number when there's an error. Example:

const ethers = require("ethers");

const abi = ["function f() public"];
/*
contract Foo {
  function f() public {
    require(false, "boom");
  }
}
*/
const bytecode =
  "0x608060405234801561001057600080fd5b50610122806100206000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c806326121ff014602d575b600080fd5b60336035565b005b60006073576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401606a9060ce565b60405180910390fd5b565b600082825260208201905092915050565b7f626f6f6d00000000000000000000000000000000000000000000000000000000600082015250565b600060ba6004836075565b915060c3826086565b602082019050919050565b6000602082019050818103600083015260e58160af565b905091905056fea26469706673582212203249be1d11bfa0b8641f8466072df543909ceae7bb84616a0d360d33883beb2064736f6c63430008110033";

async function main() {
  const provider = new ethers.JsonRpcProvider();

  const [account] = await provider.send("eth_accounts");

  const signer = new ethers.JsonRpcSigner(provider, account);

  const factory = new ethers.ContractFactory(abi, bytecode, signer);

  const contract = await factory.deploy();

  await contract.f();
}

main();

You would expect the contract.f line to show up in the stack, but it doesn't.

This is a problem mainly in node.js. In the browser, you probably get better traces when the inspector is open because in that context there are some extra things that are done to build the trace. But these things aren't done in node for performance reasons.

To be honest, this is a super hard thing to fix. It means using async/await as much as possible and being very careful when promises and .then are used.

Code Snippet

No response

Contract ABI

No response

Errors

No response

Environment

node.js (v12 or newer)

Environment (Other)

No response

Metadata

Metadata

Assignees

Labels

help-wantedLooking for others' assistance or input.investigateUnder investigation and may be a bug.v6Issues regarding v6

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions