swc-project/swc · error
failed to get current directory
Error message
failed to get current directory
What it means
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`").
Source
Thrown at crates/swc_ecma_transforms_module/src/path.rs:325
_ => {
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
- 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.
Example fix
// before
let fm = cm.new_source_file(FileName::Anon.into(), src.clone());
// base_dir unset -> resolver calls current_dir()
// after
let fm = cm.new_source_file(
FileName::Real(PathBuf::from("/project/src/input.js")).into(),
src.clone(),
); Defensive patterns
Strategy: validation
Validate before calling
// Before running a module transform with node-style path resolution, ensure
// a base is derivable: real filename OR explicit base_dir OR a valid cwd.
use std::env;
fn module_resolution_preconditions(base_dir: Option<&str>) -> Result<(), String> {
if base_dir.is_none() && env::current_dir().is_err() {
return Err("no filename, no base_dir, and cwd unavailable".into());
}
Ok(())
} Prevention
- 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.
When it happens
Trigger: 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).
Common situations: 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.
Related errors
- Fallback bindings does not support filesystem access.
- Fallback bindings does not support filesystem access
- failed to read file
- failed to execute transfomred code
- CARGO_MANIFEST_DIR
AI-assisted analysis of swc-project/swc@5176682b65 (2026-08-17).
Data as JSON: /api/errors/88b9f6ce86b5c062.
Report an issue: GitHub.