{"record":{"id":"38611686a5cf04e7","repo":"swc-project/swc","slug":"cargo-manifest-dir","errorCode":null,"errorMessage":"CARGO_MANIFEST_DIR","messagePattern":"CARGO_MANIFEST_DIR","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/swc_ecma_transforms_testing/src/lib.rs","lineNumber":407,"sourceCode":"    );\n}\n\n/// NOT A PUBLIC API. DO NOT USE.\n#[doc(hidden)]\n#[track_caller]\npub fn test_inlined_transform<F, P>(\n    test_name: &str,\n    syntax: Syntax,\n    module: Option<bool>,\n    tr: F,\n    input: &str,\n) where\n    F: FnOnce(&mut Tester) -> P,\n    P: Pass,\n{\n    let loc = panic::Location::caller();\n\n    let manifest_dir = PathBuf::from(env::var(\"CARGO_MANIFEST_DIR\").expect(\"CARGO_MANIFEST_DIR\"));\n\n    let test_file_path = CARGO_WORKSPACE_ROOT.join(loc.file());\n\n    let snapshot_dir = manifest_dir.join(\"tests\").join(\"__swc_snapshots__\").join(\n        test_file_path\n            .strip_prefix(&manifest_dir)\n            .expect(\"test_inlined_transform does not support paths outside of the crate root\"),\n    );\n\n    test_fixture_inner(\n        syntax,\n        Box::new(move |tester| Box::new(tr(tester))),\n        input,\n        &snapshot_dir.join(format!(\"{test_name}.js\")),\n        FixtureTestConfig {\n            module,\n            ..Default::default()\n        },","sourceCodeStart":389,"sourceCodeEnd":425,"githubUrl":"https://github.com/swc-project/swc/blob/5176682b65416c6b5de6b47379ae1588ea3ecb3f/crates/swc_ecma_transforms_testing/src/lib.rs#L389-L425","documentation":"`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.","triggerScenarios":"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).","commonSituations":"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.","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)."],"exampleFix":"// before\n$ ./target/debug/deps/mycrate-abc123 inline_test  # env missing -> panic\n\n// after\n$ cargo test -p mycrate inline_test","handlingStrategy":"validation","validationCode":"// Guard inlined-transform tests against non-cargo execution.\nfn cargo_env_ok() -> bool {\n    std::env::var(\"CARGO_MANIFEST_DIR\").is_ok()\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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."],"tags":["test-harness","environment-variable","cargo","runtime-panic","swc"],"backgroundTag":"missing-env-var","analyzedSha":"5176682b65416c6b5de6b47379ae1588ea3ecb3f","analyzedAt":"2026-08-17T16:16:52.067Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}