swc-project/swc · error

failed to create parent directory for temp directory

Error message

failed to create parent directory for temp directory

What it means

`exec_with_node_test_runner` (used by execution tests) creates its scratch directory at `$CARGO_TARGET_DIR/swc-es-exec-testing` with `create_dir_all`. The `expect("failed to create parent directory for temp directory")` panics when that `create_dir_all` fails: the target dir (or a parent) is not writable, the disk is full or read-only, or `CARGO_TARGET_DIR` points to an invalid/unavailable location.

Source

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

            src_without_helpers
        );

        exec_with_node_test_runner(&src).map(|_| {})
    })
}

fn calc_hash(s: &str) -> String {
    let mut hasher = Sha256::new();
    hasher.update(s.as_bytes());
    let sum = hasher.finalize();

    hex::encode(sum)
}

fn exec_with_node_test_runner(src: &str) -> Result<(), ()> {
    let root = CARGO_TARGET_DIR.join("swc-es-exec-testing");

    create_dir_all(&root).expect("failed to create parent directory for temp directory");

    let hash = calc_hash(src);
    let success_cache = root.join(format!("{hash}.success"));

    if env::var("SWC_CACHE_TEST").unwrap_or_default() == "1" {
        println!("Trying cache as `SWC_CACHE_TEST` is `1`");

        if success_cache.exists() {
            println!("Cache: success");
            return Ok(());
        }
    }

    let tmp_dir = tempdir_in(&root).expect("failed to create a temp directory");
    create_dir_all(&tmp_dir).unwrap();

    let path = tmp_dir.path().join(format!("{hash}.test.js"));

View on GitHub (pinned to 5176682b65)

Solutions

  1. Check writability: `touch $CARGO_TARGET_DIR/swc-es-exec-testing/.probe` (create as needed) and fix ownership/permissions (`chown -R $(id -u) target`).
  2. Point CARGO_TARGET_DIR at a writable location when invoking cargo if the default is unusable.
  3. Free disk space / raise the quota if the write fails with ENOSPC.
  4. Skip execution tests (`EXEC=0`) if you only need transform-output tests in a constrained environment.
Defensive patterns

Strategy: validation

Validate before calling

// Ensure the execution-test scratch root is writable before running tests.
fn exec_scratch_writable() -> bool {
    let root = std::env::var("CARGO_TARGET_DIR")
        .map(std::path::PathBuf::from)
        .unwrap_or_else(|_| std::path::PathBuf::from("target"));
    let root = root.join("swc-es-exec-testing");
    std::fs::create_dir_all(&root).is_ok()
        && {
            let probe = root.join(".probe");
            std::fs::write(&probe, b"").is_ok()
                && { let _ = std::fs::remove_file(&probe); true }
        }
}

Prevention

When it happens

Trigger: Running execution tests (`EXEC != 0`) with CARGO_TARGET_DIR unset/pointing somewhere unwritable, a read-only filesystem (some CI sandboxes, Nix builds without writable paths), exhausted disk quota, or permission changes on target/.

Common situations: CI containers running tests as a user without write access to the target dir; root-owned `target/` after running cargo once under sudo; disk-full build agents.

Related errors


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