swc-project/swc · error

failed to read file

Error message

failed to read file

What it means

The Babel-compatibility test harness (crates/swc_ecma_transforms_testing/src/babel_like.rs) drives fixture tests that read the input JS file from disk before transforming it. `read_to_string(self.input).expect("failed to read file")` panics when the fixture file recorded for the test cannot be read: it does not exist, permissions block it, or it is not valid UTF-8.

Source

Thrown at crates/swc_ecma_transforms_testing/src/babel_like.rs:122

                let mut done = false;
                for factory in &mut factories {
                    if let Some(built) = factory(&builder, &name, options.clone()) {
                        pass = Box::new((pass, built));
                        done = true;
                        break;
                    }
                }

                if !done {
                    panic!("Unknown plugin: {name}");
                }
            }

            pass = Box::new((pass, hygiene(), fixer(Some(&comments))));

            // Run pass

            let src = read_to_string(self.input).expect("failed to read file");
            let src = if output_path.is_none() && !compare_stdout {
                format!(
                    "it('should work', async function () {{
                    {src}
                }})",
                )
            } else {
                src
            };
            let fm = cm.new_source_file(
                swc_common::FileName::Real(self.input.to_path_buf()).into(),
                src,
            );

            let mut errors = Vec::new();
            let input_program = parse_file_as_program(
                &fm,
                self.syntax,

View on GitHub (pinned to 5176682b65)

Solutions

  1. Verify the input path exists: `ls` the fixture directory the test points to and correct the path used when the test was declared.
  2. If submodules or generated fixtures are involved, run `git submodule update --init --recursive` and regenerate fixtures before `cargo test`.
  3. Ensure the file is UTF-8 encoded and readable by the test user.
  4. For new fixtures, follow the crate convention (input/output files next to each other) so the harness finds them.

Example fix

// before
test_fixture(
    Syntax::default(),
    tr,
    &tests_dir.join("does-not-exist/input.js"), // wrong path
    ...,
)

// after
test_fixture(
    Syntax::default(),
    tr,
    &tests_dir.join("issue1234/input.js"), // matches file on disk
    ...,
)
Defensive patterns

Strategy: validation

Validate before calling

// Before registering a fixture test, assert both files exist and are UTF-8.
fn fixture_ok(input: &std::path::Path, output: Option<&std::path::Path>) -> bool {
    std::fs::read(input).is_ok()
        && output.map_or(true, |o| o.exists())
        && std::fs::read_to_string(input)
            .map(|s| s.is_ascii() || true) // UTF-8 checked by read_to_string
            .is_ok()
}

Prevention

When it happens

Trigger: Registering a Babel-like fixture whose input path has a typo or missing file; running tests from a tree where fixtures were not checked out (sparse checkout, .gitignore rules, submodule not initialized); a non-UTF-8 fixture; or file permissions/lock issues on the fixture.

Common situations: Adding fixtures modeled after Babel test cases (input.js + output.js pairs) and mistyping the path; CI environments where fixture files are missing; moving fixture directories without updating the test registration.

Related errors


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