swc-project/swc · error

Node path provider does not support using `{:?}` as a target

Error message

Node path provider does not support using `{:?}` as a target file name

What it means

NodePathProvider::try_resolve_import (swc_ecma_transforms_module) rewrites import specifiers into relative paths after the module resolver runs. The resolved target filename must be FileName::Real (a real path to relativize) or FileName::Custom (a virtual specifier returned verbatim); every other FileName variant (Url, Internal, Anon, ...) hits this unreachable!, i.e. the provider cannot compute a relative path for such a target.

Source

Thrown at crates/swc_ecma_transforms_module/src/path.rs:308

                    target.filename = FileName::Real(orig);
                }
            }
        }

        let Resolution {
            filename: target,
            slug,
        } = target;
        let slug = slug.as_deref().or(orig_slug);

        #[cfg(debug_assertions)]
        info!("Resolved as {target:?} with slug = {slug:?}");

        let mut target = match target {
            FileName::Real(v) => v,
            FileName::Custom(s) => return Ok(self.to_specifier(s.into(), slug)),
            _ => {
                unreachable!(
                    "Node path provider does not support using `{:?}` as a target file name",
                    target
                )
            }
        };
        let mut base = match base {
            FileName::Real(v) => Cow::Borrowed(
                v.parent()
                    .ok_or_else(|| anyhow!("failed to get parent of {v:?}"))?,
            ),
            FileName::Anon => match &self.config.base_dir {
                Some(v) => Cow::Borrowed(&**v),
                None => {
                    if cfg!(target_arch = "wasm32") {
                        panic!("Please specify `filename`")
                    } else {
                        Cow::Owned(current_dir().expect("failed to get current directory"))
                    }

View on GitHub (pinned to 5176682b65)

Solutions

  1. Make your resolver return FileName::Real for on-disk modules and FileName::Custom(specifier) for virtual ones — never Url/Internal as the resolved target filename.
  2. If you control the input, pass a real path (or configure base_dir) so the provider has something to relativize against.
  3. Update swc_ecma_transforms_module; new FileName handling lands over time.
  4. As an embedder, wrap the transform in catch_unwind and degrade to leaving the specifier untouched.

Example fix

// before: resolver returns a URL target -> panic in try_resolve_import
Ok(Resolution { filename: FileName::Url(url), slug: None })

// after: virtual targets use Custom, real files use Real
Ok(Resolution {
    filename: if is_virtual {
        FileName::Custom(specifier.clone())
    } else {
        FileName::Real(path_buf)
    },
    slug: None,
})
Defensive patterns

Strategy: validation

Validate before calling

// Validate resolved targets before import rewriting
use swc_common::FileName;
fn usable_target(f: &FileName) -> bool {
    matches!(f, FileName::Real(_) | FileName::Custom(_))
}
// in your resolver hook:
// assert!(usable_target(&res.filename), "resolver must return Real or Custom targets");

Type guard

fn is_supported_target(f: &FileName) -> bool { matches!(f, FileName::Real(_) | FileName::Custom(_)) }

Try / catch

let out = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
    module_transform.apply(cm, &mut program)
}));
out.unwrap_or_else(|_| { /* keep original specifiers, log resolver filename variant */ });

Prevention

When it happens

Trigger: A custom or exotic ImportResolver whose Resolution.filename is FileName::Url (data:/http: style module references) or FileName::Internal/Anon, while jsc.transform.modules (commonjs/amd/systemjs) import rewriting is enabled. Embedders passing an anonymous input file whose resolver echoes Anon back as the target also trip it.

Common situations: Bundler/spack-style integrations with resolvers returning URL filenames; virtual-module plugins that mark resolved files with Url/Internal instead of Custom; upgrading swc versions where FileName gained new variants the path provider has not learned.

Related errors


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