swc-project/swc · error

Please specify `filename`

Error message

Please specify `filename`

What it means

The node-modules path provider resolves specifiers relative to a base directory. When the input file is FileName::Anon and no config.base_dir is set, native builds fall back to current_dir(); on wasm32 there is no current directory, so the code panics with 'Please specify `filename`'.

Source

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

            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"))
                    }
                }
            },
            _ => {
                unreachable!(
                    "Node path provider does not support using `{:?}` as a base file name",
                    base
                )
            }
        };

        if base.is_absolute() != target.is_absolute() {
            if base.is_absolute() {
                base = Cow::Owned(base.to_path_buf().clean());
            } else {
                base = Cow::Owned(absolute_base_path(self.config.base_dir.as_deref(), &base)?);

View on GitHub (pinned to 5176682b65)

Solutions

  1. Pass a real filename for the source being processed (e.g. filename: 'src/index.js' in transform options) so base resolution has an anchor
  2. Set base_dir in the resolver/path-provider config to a known virtual root
  3. Avoid node-modules resolution paths in wasm builds unless you provide both filename and base_dir

Example fix

// before
import { transformSync } from '@swc/core/wasm';
transformSync(code, {}); // FileName::Anon, no base_dir -> panic on wasm

// after
transformSync(code, { filename: 'src/index.js' });
Defensive patterns

Strategy: validation

Validate before calling

// JS (wasm build): always anchor transforms with a filename
function safeTransform(code, opts) {
  if (!opts.filename) throw new Error('filename is required for path resolution on wasm');
  return transformSync(code, opts);
}

Prevention

When it happens

Trigger: Running SWC compiled to wasm32 (browser, wasm bindings, plugins) with a transform that triggers node-style path resolution while the source has no real filename and base_dir is None.

Common situations: Wasm builds of SWC used in playgrounds or in-browser bundlers, calling process()/transform without a filename option, plugin configs that rely on cwd-relative resolution that works natively but not in wasm.

Related errors


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