{"record":{"id":"09f1e7f242204778","repo":"swc-project/swc","slug":"failed-to-get-parent-of-v","errorCode":null,"errorMessage":"failed to get parent of {v:?}","messagePattern":"failed to get parent of (.+?)","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"crates/swc_ecma_transforms_module/src/path.rs","lineNumber":317,"sourceCode":"        let slug = slug.as_deref().or(orig_slug);\n\n        #[cfg(debug_assertions)]\n        info!(\"Resolved as {target:?} with slug = {slug:?}\");\n\n        let mut target = match target {\n            FileName::Real(v) => v,\n            FileName::Custom(s) => return Ok(self.to_specifier(s.into(), slug)),\n            _ => {\n                unreachable!(\n                    \"Node path provider does not support using `{:?}` as a target file name\",\n                    target\n                )\n            }\n        };\n        let mut base = match base {\n            FileName::Real(v) => Cow::Borrowed(\n                v.parent()\n                    .ok_or_else(|| anyhow!(\"failed to get parent of {v:?}\"))?,\n            ),\n            FileName::Anon => match &self.config.base_dir {\n                Some(v) => Cow::Borrowed(&**v),\n                None => {\n                    if cfg!(target_arch = \"wasm32\") {\n                        panic!(\"Please specify `filename`\")\n                    } else {\n                        Cow::Owned(current_dir().expect(\"failed to get current directory\"))\n                    }\n                }\n            },\n            _ => {\n                unreachable!(\n                    \"Node path provider does not support using `{:?}` as a base file name\",\n                    base\n                )\n            }\n        };","sourceCodeStart":299,"sourceCodeEnd":335,"githubUrl":"https://github.com/swc-project/swc/blob/d7d74346665e36e692aa07e13d4ee3ee692ee35c/crates/swc_ecma_transforms_module/src/path.rs#L299-L335","documentation":"Thrown by the Node-style import path provider in swc_ecma_transforms_module when the base for relative-specifier conversion is a FileName::Real whose PathBuf has no parent. std::path::Path::parent() returns None only for root-like paths ('/', 'C:\\'), so this means the configured base/filename resolves to a filesystem root and no relative path can be computed from it.","triggerScenarios":"Running module transforms (CommonJS/UMD/AMD paths handling, import rewrites) where the base FileName::Real is '/' or a drive root - e.g. base_url / filename configured as '/' or a path that canonicalizes to root.","commonSituations":"Passing an absolute root-ish base_dir from build tooling, an empty filename string that joins to '/', or wasm32 builds where cwd fallbacks interact badly with base_dir config (the sibling branch panics on wasm32 without a filename).","solutions":["Set base_dir/filename to a real directory under a parent (e.g. the project root or the source file's directory), never '/' or a bare drive root","When constructing the provider programmatically, wrap FileName::Real(base) with a parent() check and fall back to a sensible default directory before calling the transform","On wasm32 targets, always pass an explicit filename/base_dir - the Anon branch panics without one"],"exampleFix":"// before\nlet provider = NodeImportPathProvider::new(Some(\"/\".into()), ...);\n\n// after\nlet provider = NodeImportPathProvider::new(Some(\"/project/src\".into()), ...);","handlingStrategy":"validation","validationCode":"// Rust: reject root-like bases before constructing the provider\nfn ensure_base_has_parent(base: &Path) -> anyhow::Result<PathBuf> {\n    let parent = base.parent().ok_or_else(|| {\n        anyhow::anyhow!(\"base path {} has no parent directory\", base.display())\n    })?;\n    if parent.as_os_str().is_empty() {\n        anyhow::bail!(\"base path {} resolves to a filesystem root\", base.display());\n    }\n    Ok(base.to_path_buf())\n}","typeGuard":null,"tryCatchPattern":"match provider.resolve_target(...) {\n    Err(e) if e.to_string().contains(\"failed to get parent\") => {\n        // fall back to the source file's own directory as the base\n        retry_with_base(source_file.parent().unwrap().to_path_buf())?\n    }\n    other => other?,\n}","preventionTips":["Default base_dir to the project root or file directory, never user-supplied raw strings","Canonicalize and assert a parent exists at config load time, not mid-transform","On wasm32 always pass an explicit filename to avoid the sibling panic"],"tags":["swc","module-resolution","paths","config","filesystem"],"backgroundTag":null,"analyzedSha":"d7d74346665e36e692aa07e13d4ee3ee692ee35c","analyzedAt":"2026-08-16T12:41:13.160Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}