Repository: foundry-rs/foundry (language: rust)

Issue:
bug(`forge test`): failures in `constructor` wrongly point to the `setUp` function
### 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.0.0-stable Commit SHA: e144b82070619b6e10485c38734b4d4d45aebe04 Build Timestamp: 2025-02-13T20:03:31.026474817Z (1739477011) Build Profile: maxperf

### What version of Foundryup are you on?

foundryup: 1.0.0

### What command(s) is the bug in?

forge test

### Operating System

Linux

### Describe the bug

Adding the following constructor to a just initiated Counter forge repository:
```solidity
    constructor() {
        require(false);
    }
```
and doing `forge test`
will ouput:
![Image](https://github.com/user-attachments/assets/71dcc0b9-b018-40c5-acfc-c27e6458748e)

This can be misleading as there is a `setUp` function, but the revert does not happen in it


Discussion / hints:

Fix patch:
diff --git a/crates/forge/src/result.rs b/crates/forge/src/result.rs
index 0b1ebff19d74..02ca64fde18a 100644
--- a/crates/forge/src/result.rs
+++ b/crates/forge/src/result.rs
@@ -765,6 +765,8 @@ pub struct TestSetup {
     pub reason: Option<String>,
     /// Whether setup and entire test suite is skipped.
     pub skipped: bool,
+    /// Whether the test failed to deploy.
+    pub deployment_failure: bool,
 }
 
 impl TestSetup {
diff --git a/crates/forge/src/runner.rs b/crates/forge/src/runner.rs
index 1b30dcebe6ce..893c74baf449 100644
--- a/crates/forge/src/runner.rs
+++ b/crates/forge/src/runner.rs
@@ -158,6 +158,9 @@ impl<'a> ContractRunner<'a> {
             U256::ZERO,
             Some(&self.mcr.revert_decoder),
         );
+
+        result.deployment_failure = deploy_result.is_err();
+
         if let Ok(dr) = &deploy_result {
             debug_assert_eq!(dr.address, address);
         }
@@ -340,9 +343,14 @@ impl<'a> ContractRunner<'a> {
 
         if setup.reason.is_some() {
             // The setup failed, so we return a single test result for `setUp`
+            let fail_msg = if !setup.deployment_failure {
+                "setUp()".to_string()
+            } else {
+                "constructor()".to_string()
+            };
             return SuiteResult::new(
                 start.elapsed(),
-                [("setUp()".to_string(), TestResult::setup_result(setup))].into(),
+                [(fail_msg, TestResult::setup_result(setup))].into(),
                 warnings,
             )
         }


Test patch:
diff --git a/crates/forge/tests/cli/test_cmd.rs b/crates/forge/tests/cli/test_cmd.rs
index d3a7eb9179be..c0e975016350 100644
--- a/crates/forge/tests/cli/test_cmd.rs
+++ b/crates/forge/tests/cli/test_cmd.rs
@@ -3137,3 +3137,34 @@ Encountered a total of 1 failing tests, 1 tests succeeded
 
 "#]]);
 });
+
+forgetest_init!(catch_test_deployment_failure, |prj, cmd| {
+    prj.add_test(
+        "TestDeploymentFailure.t.sol",
+        r#"
+import "forge-std/Test.sol";
+contract TestDeploymentFailure is Test {
+
+    constructor() {
+        require(false);
+    }
+
+    function setUp() public {
+        require(true);
+    }
+
+    function test_something() public {
+        require(1 == 1);
+    }
+}
+    "#,
+    )
+    .unwrap();
+
+    cmd.args(["t", "--mt", "test_something"]).assert_failure().stdout_eq(str![[r#"
+...
+Failing tests:
+Encountered 1 failing test in test/TestDeploymentFailure.t.sol:TestDeploymentFailure
+[FAIL: EvmError: Revert] constructor() ([GAS])
+..."#]]);
+});