swc-project/swc · error
CARGO_MANIFEST_DIR
Error message
CARGO_MANIFEST_DIR
What it means
`test_inlined_transform` (crates/swc_ecma_transforms_testing/src/lib.rs) resolves the snapshot directory from `std::env::var("CARGO_MANIFEST_DIR")`. The `expect("CARGO_MANIFEST_DIR")` panics when the environment variable is absent — i.e. the test code is running outside a normal `cargo test` invocation, since cargo always sets this variable for build/test runs.
Source
Thrown at crates/swc_ecma_transforms_testing/src/lib.rs:407
);
}
/// NOT A PUBLIC API. DO NOT USE.
#[doc(hidden)]
#[track_caller]
pub fn test_inlined_transform<F, P>(
test_name: &str,
syntax: Syntax,
module: Option<bool>,
tr: F,
input: &str,
) where
F: FnOnce(&mut Tester) -> P,
P: Pass,
{
let loc = panic::Location::caller();
let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR"));
let test_file_path = CARGO_WORKSPACE_ROOT.join(loc.file());
let snapshot_dir = manifest_dir.join("tests").join("__swc_snapshots__").join(
test_file_path
.strip_prefix(&manifest_dir)
.expect("test_inlined_transform does not support paths outside of the crate root"),
);
test_fixture_inner(
syntax,
Box::new(move |tester| Box::new(tr(tester))),
input,
&snapshot_dir.join(format!("{test_name}.js")),
FixtureTestConfig {
module,
..Default::default()
},View on GitHub (pinned to 5176682b65)
Solutions
- Run the tests through cargo: `cargo test -p <crate> <test_name>` so cargo exports CARGO_MANIFEST_DIR.
- If you must run the binary directly, export the variable first: `CARGO_MANIFEST_DIR=$(pwd) ./target/debug/deps/...`.
- For IDE/runner integrations, configure them to preserve cargo's environment (rust-analyzer and `cargo nextest` do this by default).
Example fix
// before $ ./target/debug/deps/mycrate-abc123 inline_test # env missing -> panic // after $ cargo test -p mycrate inline_test
Defensive patterns
Strategy: validation
Validate before calling
// Guard inlined-transform tests against non-cargo execution.
fn cargo_env_ok() -> bool {
std::env::var("CARGO_MANIFEST_DIR").is_ok()
} Prevention
- Always invoke these tests via `cargo test` so cargo exports CARGO_MANIFEST_DIR.
- If running the test binary directly, export CARGO_MANIFEST_DIR first.
- Configure IDE/runner integrations to inherit cargo's environment.
When it happens
Trigger: Executing the compiled test binary directly (e.g. `./target/debug/deps/foo-... `), running tests under a runner or IDE that scrubs the environment, or invoking the test harness from a non-cargo context (custom scripts, some remote-test setups).
Common situations: Debugging a single test by running the binary from a shell; CI wrappers that sanitize env; `cargo test` replaced by direct binary execution in benchmarks or probes.
Understand the failure class
Background: "environment variable is not set" and "Missing keys in environment" errors: what missing required env var messages mean and how to fix them — this error's family across 28 libraries.
Related errors
- failed to execute transfomred code
- test_inlined_transform does not support paths outside of the
- failed to get current directory
- failed to read file
- failed to create parent directory for temp directory
AI-assisted analysis of swc-project/swc@5176682b65 (2026-08-17).
Data as JSON: /api/errors/38611686a5cf04e7.
Report an issue: GitHub.