{"record":{"id":"5e0b568563044c49","repo":"swc-project/swc","slug":"unexpected-root-directory-is-given-as-a-input-fil","errorCode":null,"errorMessage":"unexpected: root directory is given as a input file","messagePattern":"unexpected: root directory is given as a input file","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/swc/src/lib.rs","lineNumber":474,"sourceCode":"                    let content = url.path()[idx + \"base64,\".len()..].trim();\n\n                    let res = BASE64_STANDARD\n                        .decode(content.as_bytes())\n                        .context(\"failed to decode base64-encoded source map\")?;\n\n                    Ok(Some(sourcemap::SourceMap::from_slice(&res).context(\n                        \"failed to read input source map from inlined base64 encoded string\",\n                    )?))\n                };\n\n            let read_file_sourcemap =\n                |data_url: Option<&str>| -> Result<Option<sourcemap::SourceMap>, Error> {\n                    match &**name {\n                        FileName::Real(filename) => {\n                            let dir = match filename.parent() {\n                                Some(v) => v,\n                                None => {\n                                    bail!(\"unexpected: root directory is given as a input file\")\n                                }\n                            };\n\n                            let map_path = match data_url {\n                                Some(data_url) => {\n                                    let mut map_path = dir.join(data_url);\n                                    if !map_path.exists() {\n                                        // Old behavior. This check would prevent\n                                        // regressions.\n                                        // Perhaps it shouldn't be supported. Sometimes\n                                        // developers don't want to expose their source\n                                        // code.\n                                        // Map files are for internal troubleshooting\n                                        // convenience.\n                                        let fallback_map_path =\n                                            PathBuf::from(format!(\"{}.map\", filename.display()));\n                                        if fallback_map_path.exists() {\n                                            map_path = fallback_map_path;","sourceCodeStart":456,"sourceCodeEnd":492,"githubUrl":"https://github.com/swc-project/swc/blob/5176682b65416c6b5de6b47379ae1588ea3ecb3f/crates/swc/src/lib.rs#L456-L492","documentation":"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.","triggerScenarios":"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.","commonSituations":"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).","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"],"exampleFix":"// before\nconst out = await swc.transform(src, { filename: inputPath /* \"\" */ });\n// after\nif (!inputPath || path.isAbsolute(inputPath) && path.parse(inputPath).root === inputPath) {\n  throw new Error('valid filename required');\n}\nconst out = await swc.transform(src, { filename: inputPath });","handlingStrategy":"validation","validationCode":"// JavaScript - reject degenerate filenames before calling swc\nimport path from 'node:path';\nfunction assertUsableFilename(filename) {\n  if (!filename || path.parse(filename).root === filename) {\n    throw new Error(`invalid input filename: ${JSON.stringify(filename)}`);\n  }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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"],"tags":["swc","file-path","source-map","path-handling"],"backgroundTag":"invalid-file-path","analyzedSha":"5176682b65416c6b5de6b47379ae1588ea3ecb3f","analyzedAt":"2026-08-17T16:16:52.067Z","contentChangedAt":"2026-08-17T16:16:52.067Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}