{"record":{"id":"88b9f6ce86b5c062","repo":"swc-project/swc","slug":"failed-to-get-current-directory","errorCode":null,"errorMessage":"failed to get current directory","messagePattern":"failed to get current directory","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/swc_ecma_transforms_module/src/path.rs","lineNumber":325,"sourceCode":"            _ => {\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        };\n\n        if base.is_absolute() != target.is_absolute() {\n            if base.is_absolute() {\n                base = Cow::Owned(base.to_path_buf().clean());\n            } else {\n                base = Cow::Owned(absolute_base_path(self.config.base_dir.as_deref(), &base)?);\n            }\n","sourceCodeStart":307,"sourceCodeEnd":343,"githubUrl":"https://github.com/swc-project/swc/blob/5176682b65416c6b5de6b47379ae1588ea3ecb3f/crates/swc_ecma_transforms_module/src/path.rs#L307-L343","documentation":"In the Node-style import path resolver of swc_ecma_transforms_module (crates/swc_ecma_transforms_module/src/path.rs), when the base file is `FileName::Anon` and no `base_dir` is configured in `config`, the resolver falls back to `std::env::current_dir()`. On native targets that call is unwrapped with `expect(\"failed to get current directory\")`, so resolution of an import specifier panics when the OS reports no current directory. On wasm32 the same branch panics with a different message (\"Please specify `filename`\").","triggerScenarios":"Calling a module transform with import rewriting (e.g. `ImportRewriter` / node-style path resolution) on source parsed without a `filename` (FileName::Anon), with `config.base_dir` unset, while the process cwd has been deleted, unlinked, or made unreadable (ENOENT/EACCES from getcwd).","commonSituations":"Programmatic uses of @swc/core-style APIs that pass a source string with no filename and no `base_dir`, running under a daemon whose cwd was removed (common with some watchers and container setups), or test harnesses that chdir into temp dirs and delete them.","solutions":["Pass a real file name when creating the SourceMap entry (parse with `FileName::Real(path)` instead of Anon/Custom).","Set `base_dir` in the module config (e.g. `Config { base_dir: Some(\"/project\".into()), .. }`) so Anon sources never need the cwd.","If running a long-lived process, ensure its working directory still exists; restart daemons after deleting their cwd.","On wasm32 builds the same misconfiguration panics earlier with \"Please specify `filename`\" — supply the filename there."],"exampleFix":"// before\nlet fm = cm.new_source_file(FileName::Anon.into(), src.clone());\n// base_dir unset -> resolver calls current_dir()\n\n// after\nlet fm = cm.new_source_file(\n    FileName::Real(PathBuf::from(\"/project/src/input.js\")).into(),\n    src.clone(),\n);","handlingStrategy":"validation","validationCode":"// Before running a module transform with node-style path resolution, ensure\n// a base is derivable: real filename OR explicit base_dir OR a valid cwd.\nuse std::env;\n\nfn module_resolution_preconditions(base_dir: Option<&str>) -> Result<(), String> {\n    if base_dir.is_none() && env::current_dir().is_err() {\n        return Err(\"no filename, no base_dir, and cwd unavailable\".into());\n    }\n    Ok(())\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always parse with a real FileName::Real(path) instead of FileName::Anon when import rewriting will run.","Set module config base_dir for string-based (filename-less) inputs.","In long-lived processes, avoid deleting or invalidating the process cwd."],"tags":["module-resolution","filesystem","current-directory","runtime-panic","swc"],"backgroundTag":"current-directory-unavailable","analyzedSha":"5176682b65416c6b5de6b47379ae1588ea3ecb3f","analyzedAt":"2026-08-17T16:16:52.067Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}