swc-project/swc · error

failed to read `CARGO_MANIFEST_DIR`: {err}

Error message

failed to read `CARGO_MANIFEST_DIR`: {err}

What it means

Raised in `manifest_dir` (crates/testing/src/paths.rs:12). The function reads the CARGO_MANIFEST_DIR environment variable to locate the crate root; the panic fires when the variable is unset (env::var returns Err), i.e. the code is running outside a cargo-driven build/test context where cargo normally injects this variable. The missing environment variable is the input at fault.

Source

Thrown at crates/testing/src/paths.rs:12

use std::{env, path::PathBuf, sync::Arc};

use once_cell::sync::Lazy;

pub fn manifest_dir() -> PathBuf {
    env::var("CARGO_MANIFEST_DIR")
        .map(PathBuf::from)
        .map(|p| {
            p.canonicalize()
                .expect("failed to canonicalize `CARGO_MANIFEST_DIR`")
        })
        .unwrap_or_else(|err| panic!("failed to read `CARGO_MANIFEST_DIR`: {err}"))
}

/// This directory is per-crate.
pub fn test_results_dir() -> Arc<PathBuf> {
    fn detect() -> PathBuf {
        manifest_dir().join("target").join("swc-test-results")
    }

    static DIR: Lazy<Arc<PathBuf>> = Lazy::new(|| Arc::new(detect()));

    DIR.clone()
}

View on GitHub (pinned to 5176682b65)

Solutions

  1. Run the code through cargo (cargo test / cargo run) so CARGO_MANIFEST_DIR is set
  2. Set CARGO_MANIFEST_DIR manually when invoking the test binary directly
  3. Derive the manifest path from a build-script-generated env! constant instead of runtime env::var
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at crates/testing/src/paths.rs:12 when the library encounters an invalid state.

Common situations: See trigger scenarios.


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