swc-project/swc · error

failed to run mocha

Error message

failed to run mocha

What it means

After locating mocha, `exec_with_node_test_runner` spawns it (`Command::new(&test_runner_path).output()`; `cmd /C` wrapper on Windows). The `expect("failed to run mocha")` panics when the spawn itself fails — the executable file was found but cannot be executed: missing execute permission, noexec mounts, a bad interpreter line, or OS-level spawn errors. This is distinct from mocha running and exiting non-zero, which takes the separate "Execution failed" path.

Source

Thrown at crates/swc_ecma_transforms_testing/src/lib.rs:682

    write!(tmp, "{src}").expect("failed to write to temp file");
    tmp.flush().unwrap();

    let test_runner_path = find_executable("mocha").expect("failed to find `mocha` from path");

    let mut base_cmd = if cfg!(target_os = "windows") {
        let mut c = Command::new("cmd");
        c.arg("/C").arg(&test_runner_path);
        c
    } else {
        Command::new(&test_runner_path)
    };

    let output = base_cmd
        .arg(format!("{}", path.display()))
        .arg("--color")
        .current_dir(root)
        .output()
        .expect("failed to run mocha");

    println!(">>>>> {} <<<<<", Color::Red.paint("Stdout"));
    println!("{}", String::from_utf8_lossy(&output.stdout));
    println!(">>>>> {} <<<<<", Color::Red.paint("Stderr"));
    println!("{}", String::from_utf8_lossy(&output.stderr));

    if output.status.success() {
        fs::write(&success_cache, "").unwrap();
        return Ok(());
    }
    let dir_name = path.display().to_string();
    ::std::mem::forget(tmp_dir);
    panic!("Execution failed: {dir_name}")
}

fn stdout_of(code: &str) -> Result<String, Error> {
    exec_node_js(
        code,

View on GitHub (pinned to 5176682b65)

Solutions

  1. Check the located path is executable: `ls -l $(which mocha)` and `chmod +x` the shim or reinstall mocha properly (`npm i -D mocha`).
  2. If node_modules lives on a noexec mount, move the target/workspace or remount with exec.
  3. On Windows, prefer a real mocha install so `mocha.cmd` has a valid handler; avoid bare .js shims in PATH.
  4. Raise process/fd limits in the CI job if spawn fails with resource errors.

Example fix

# before
$ ls -l node_modules/.bin/mocha   # no execute bit -> spawn fails

# after
$ chmod +x node_modules/.bin/mocha   # or: npm reinstall mocha
$ cargo test -p swc_ecma_transforms_testing
Defensive patterns

Strategy: validation

Validate before calling

// Verify the resolved mocha is actually executable before spawning tests.
use std::os::unix::fs::PermissionsExt;

fn mocha_is_executable(path: &std::path::Path) -> bool {
    #[cfg(unix)]
    {
        std::fs::metadata(path)
            .map(|m| m.permissions().mode() & 0o111 != 0)
            .unwrap_or(false)
    }
    #[cfg(not(unix))]
    { path.is_file() }
}

Prevention

When it happens

Trigger: A mocha shim/script in PATH without the executable bit (`chmod -x`), node_modules on a noexec-mounted filesystem, Windows where the found file is a `.js`/shell shim without a valid handler, or system resource limits preventing process creation (EAGAIN).

Common situations: Mocha found via `pnpm bin` on a mounted volume with noexec; manually copied mocha shims; fork-bomb-guarded CI limiting process creation; Windows permissions on .cmd shims.

Related errors


AI-assisted analysis of swc-project/swc@5176682b65 (2026-08-17). Data as JSON: /api/errors/566d205192b661f7. Report an issue: GitHub.