swc-project/swc · error
unexpected: root directory is given as a input file
Error message
unexpected: root directory is given as a input file
What it means
While locating a referenced external source map, swc takes the parent directory of the input file's real path; `Path::parent()` returned None, which happens only when the path is the filesystem root (or degenerate empty form). SWC reports this as 'root directory given as input file' because a root path has no sibling directory in which to look for a .map file.
Source
Thrown at crates/swc/src/lib.rs:474
let content = url.path()[idx + "base64,".len()..].trim();
let res = BASE64_STANDARD
.decode(content.as_bytes())
.context("failed to decode base64-encoded source map")?;
Ok(Some(sourcemap::SourceMap::from_slice(&res).context(
"failed to read input source map from inlined base64 encoded string",
)?))
};
let read_file_sourcemap =
|data_url: Option<&str>| -> Result<Option<sourcemap::SourceMap>, Error> {
match &**name {
FileName::Real(filename) => {
let dir = match filename.parent() {
Some(v) => v,
None => {
bail!("unexpected: root directory is given as a input file")
}
};
let map_path = match data_url {
Some(data_url) => {
let mut map_path = dir.join(data_url);
if !map_path.exists() {
// Old behavior. This check would prevent
// regressions.
// Perhaps it shouldn't be supported. Sometimes
// developers don't want to expose their source
// code.
// Map files are for internal troubleshooting
// convenience.
let fallback_map_path =
PathBuf::from(format!("{}.map", filename.display()));
if fallback_map_path.exists() {
map_path = fallback_map_path;View on GitHub (pinned to 5176682b65)
Solutions
- Pass a real filename in options.filename, e.g. "src/index.js" rather than "" or "/"
- Log the filename immediately before the swc call to find where it degenerates
- Guard callers: reject empty or root-only paths before invoking the compiler
Example fix
// before
const out = await swc.transform(src, { filename: inputPath /* "" */ });
// after
if (!inputPath || path.isAbsolute(inputPath) && path.parse(inputPath).root === inputPath) {
throw new Error('valid filename required');
}
const out = await swc.transform(src, { filename: inputPath }); Defensive patterns
Strategy: validation
Validate before calling
// JavaScript - reject degenerate filenames before calling swc
import path from 'node:path';
function assertUsableFilename(filename) {
if (!filename || path.parse(filename).root === filename) {
throw new Error(`invalid input filename: ${JSON.stringify(filename)}`);
}
} Prevention
- Never build swc filenames from possibly-empty variables
- Default to a placeholder like 'input.js' for in-memory sources
- Assert path invariants at the boundary of your build pipeline
When it happens
Trigger: Invoking the compiler with FileName::Real("/") or an empty path - e.g. options.filename computed as '' or '/' by glue code - on a file that references a source map.
Common situations: Programmatic Compiler/swc API use where filename is built from an unset variable; CI containers where a path env var is empty; wrappers that pass the wrong field (directory instead of file path).
Related errors
- determine_export_name({:?})
- failed to parse inline source map: not base64: {url:?}
- failed to find input source map file {:?} in {:?} file as ei
- The requested module '{specifier}' does not provide an expor
- Cannot use import.meta outside a module
AI-assisted analysis of swc-project/swc@5176682b65 (2026-08-17).
Data as JSON: /api/errors/5e0b568563044c49.
Report an issue: GitHub.