You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat(cli): Add --root-mode argument for .swcrc resolution (#11501)
## Summary
Adds `--root-mode` CLI argument to the `swc compile` command, matching
Babel's behavior for resolving `.swcrc` configuration files.
Closes#1801
### Modes
| Mode | Behavior |
|------|----------|
| `root` (default) | Only looks for `.swcrc` at the root directory |
| `upward` | Walks upward from the file directory, **throws error** if
no `.swcrc` is found |
| `upward-optional` | Walks upward from the file directory, **falls back
silently** if no `.swcrc` is found |
### Changes
- **compile.rs**: Add `--root-mode` argument with value parser
- **compile.rs**: Use `swcrc: default_swcrc()` in Options construction
for consistency with serde default
- **config/mod.rs**: Make `default_swcrc()` public so CLI can use it
- **lib.rs**: Canonicalize relative paths before calling `find_swcrc`
for proper parent traversal
- **lib.rs**: Add error when `upward` mode fails to find a `.swcrc`
- **issues.rs**: Add 5 CLI tests covering all root-mode behaviors
### Tests
| Test | Description |
|------|-------------|
| `root_mode_upward_finds_parent_config` | Verifies `upward` finds
`.swcrc` in parent directory |
| `root_mode_root_ignores_parent_config` | Verifies `root` mode does NOT
search parent directories |
| `root_mode_upward_fails_without_config` | Verifies `upward` fails when
no `.swcrc` exists |
| `root_mode_upward_optional_succeeds_without_config` | Verifies
`upward-optional` succeeds without `.swcrc` |
| `root_mode_upward_optional_finds_parent_config` | Verifies
`upward-optional` finds parent `.swcrc` |
### Note for maintainers
The `Options` struct derives `Default`, which sets `swcrc` to `false`
(Rust's bool default). However, the serde configuration uses
`#[serde(default = "default_swcrc")]` which returns `true`. To address
this discrepancy, the CLI now explicitly sets `swcrc: default_swcrc()`
when constructing Options, ensuring consistent behavior. Happy to tweak
the PR if you prefer some other approach here.
## Test plan
- [x] `cargo test -p swc_cli_impl` - all 9 tests pass
- [x] `cargo clippy -p swc_cli_impl --tests -- -D warnings` - no
warnings
- [x] `cargo fmt --all` - formatted
🤖 Generated with [Claude Code](https://claude.com/claude-code)
---------
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
0 commit comments