Unable to overwrite a prank after contract creation: `vm.startPrank: cannot overwrite a prank until it is applied at least once`
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 Version: 1.1.0-stable Commit SHA: d484a00089d789a19e2e43e63bbb3f1500eb2cbf Build Timestamp: 2025-04-30T13:50:49.971365000Z (1746021049) Build Profile: maxperf
What version of Foundryup are you on?
foundryup: 1.1.0
What command(s) is the bug in?
forge test
Operating System
macOS (Apple Silicon)
Describe the bug
Using vm.startPrank to override the current prank works as long as the prank has been used. However, contract creation via new does not count as a use. The following test case demonstrates the issue:
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;
import {Test, console} from "forge-std/Test.sol";
import {Counter} from "../src/Counter.sol";
contract CounterTest is Test {
Counter public counter;
function setUp() public {
address alice = makeAddr("alice");
address bob = makeAddr("bob");
vm.startPrank(alice);
counter = new Counter();
vm.startPrank(bob);
counter.setNumber(0);
}
function test_Increment() public {
counter.increment();
assertEq(counter.number(), 1);
}
function testFuzz_SetNumber(uint256 x) public {
counter.setNumber(x);
assertEq(counter.number(), x);
}
}
Ran 1 test for test/Counter.t.sol:CounterTest
[FAIL: vm.startPrank: cannot overwrite a prank until it is applied at least once] setUp() (gas: 0)
Traces:
[141686] CounterTest::setUp()
├─ [0] VM::addr(<pk>) [staticcall]
│ └─ ← [Return] alice: [0x328809Bc894f92807417D2dAD6b7C998c1aFdac6]
├─ [0] VM::label(alice: [0x328809Bc894f92807417D2dAD6b7C998c1aFdac6], "alice")
│ └─ ← [Return]
├─ [0] VM::addr(<pk>) [staticcall]
│ └─ ← [Return] bob: [0x1D96F2f6BeF1202E4Ce1Ff6Dad0c2CB002861d3e]
├─ [0] VM::label(bob: [0x1D96F2f6BeF1202E4Ce1Ff6Dad0c2CB002861d3e], "bob")
│ └─ ← [Return]
├─ [0] VM::startPrank(alice: [0x328809Bc894f92807417D2dAD6b7C998c1aFdac6])
│ └─ ← [Return]
├─ [96345] → new Counter@0xC8501479803c58592eF3Be0beABBEE22e3377C08
│ └─ ← [Return] 481 bytes of code
├─ [0] VM::startPrank(bob: [0x1D96F2f6BeF1202E4Ce1Ff6Dad0c2CB002861d3e])
│ └─ ← [Revert] vm.startPrank: cannot overwrite a prank until it is applied at least once
└─ ← [Revert] vm.startPrank: cannot overwrite a prank until it is applied at least once
@tariqkb that's expected, startPrank is global and can be stopped with stopPrank, see https://book.getfoundry.sh/cheatcodes/start-prank#description
For single call prank use prank, see https://book.getfoundry.sh/cheatcodes/prank
Going to close as works as designed, thank you
@grandizzy I'm not sure I understand. It's not consistent that the following works without using stopPrank:
vm.startPrank(alice);
counter.setNumber(0); // <- because this is a call, the alice prank is considered used
vm.startPrank(bob);
counter.setNumber(0);
I'm replacing instances of changePrank with startPrank as suggested by the deprecation notice: "changePrank is deprecated. Please use vm.startPrank instead.", but that does not work when creating a contract via new as shown in the reproduction above.
https://github.com/foundry-rs/foundry/pull/4826#issuecomment-1543962477 suggests this may be a bug. https://github.com/foundry-rs/foundry/issues/4942#issuecomment-1700701053 https://github.com/foundry-rs/foundry/pull/4826#issuecomment-1700689890 these reported the same issue I'm experiencing.
I'll keep using changePrank for now. Thank you for the quick response and for this wonderful tool :)
@tariqkb oh, sorry, I misread your report, yeah this should work, reopening