swc-project/swc · error

Cannot specify multiple files when using a directory as inpu

Error message

Cannot specify multiple files when using a directory as input

What it means

swc CLI compile input collection (crates/swc_cli_impl/src/commands/compile.rs): if any positional input is a directory, it must be the only input. Mixing a directory with extra files is rejected up front because directory mode recursively collects all compilable files itself.

Source

Thrown at crates/swc_cli_impl/src/commands/compile.rs:290

        .iter()
        .filter_map(|path| absolutize_path(path).ok())
        .collect()
}

/// Infer list of files from cli arguments.
#[cfg_attr(debug_assertions, tracing::instrument(level = "info", skip_all))]
fn collect_input_files(
    raw_files_input: &[PathBuf],
    ignore_pattern: Option<&Pattern>,
    excluded_dir: Option<&Path>,
) -> anyhow::Result<Vec<InputFile>> {
    let input_dir = raw_files_input.iter().find(|path| path.is_dir());
    let cwd = std::env::current_dir()?;
    let excluded_dir = excluded_dir.map(absolutize_path).transpose()?;

    let files = if let Some(input_dir) = input_dir {
        if raw_files_input.len() > 1 {
            bail!("Cannot specify multiple files when using a directory as input");
        }

        WalkDir::new(input_dir)
            .into_iter()
            .filter_entry(|entry| {
                // Avoid descending into generated output when --out-dir is
                // nested inside the watched or compiled input tree.
                !excluded_dir
                    .as_deref()
                    .map(|excluded_dir| is_same_or_nested_path(entry.path(), excluded_dir))
                    .unwrap_or(false)
            })
            .filter_map(|entry| entry.ok())
            .map(|entry| entry.into_path())
            .filter(|path| path.is_file())
            .filter(|path| !is_ignored_path(path, ignore_pattern, &cwd))
            .map(|path| InputFile {
                path,

View on GitHub (pinned to 5176682b65)

Solutions

  1. Pass the directory alone; select extensions inside it with --extensions if needed
  2. Or list the individual files explicitly without the directory

Example fix

# before
swc ./src ./setup.js -d dist
# after
swc ./src -d dist
Defensive patterns

Strategy: validation

Validate before calling

# Shell / JS - reject dir + file mixes before invoking swc
const hasDir = inputs.some((p) => fs.statSync(p).isDirectory());
if (hasDir && inputs.length > 1) {
  throw new Error('pass the directory alone, or only files');
}

Prevention

When it happens

Trigger: Running `swc ./src extra.js` or `npx swc lib/ setup.js` - any invocation where one input path is a directory and raw_files_input.len() > 1.

Common situations: Shell scripts appending explicit files to a directory argument; glob expansion that includes a directory; copy-pasted commands mixing modes.

Understand the failure class

Background: "Unknown argument", "Invalid value", and "must be one of": invalid CLI argument errors explained — this error's family across 35 libraries.

Related errors


AI-assisted analysis of swc-project/swc@5176682b65 (2026-08-17). Data as JSON: /api/errors/ccdd36e4369a26e8. Report an issue: GitHub.