swc-project/swc · error

.swcrc exists but not matched

Error message

.swcrc exists but not matched

What it means

Raised by `Rc::into_config` when a .swcrc was found and loaded, a filename was supplied, but no config object in the file matches that filename. Each config entry matches via its `test` and `exclude` fields (regex/pattern `FileMatcher`); the loop falls through and swc bails. It means 'configuration exists on disk, but none of its entries claim this file'.

Source

Thrown at crates/swc/src/config/mod.rs:1190

                None => return Ok(Some(c)),
            },
            Rc::Multi(cs) => cs,
        };

        match filename {
            Some(filename) => {
                for mut c in cs {
                    if c.matches(filename)? {
                        c.adjust(filename);

                        return Ok(Some(c));
                    }
                }
            }
            None => return Ok(Some(Config::default())),
        }

        bail!(".swcrc exists but not matched")
    }
}

/// A single object in the `.swcrc` file
#[derive(Debug, Default, Clone, Deserialize, Merge)]
#[serde(deny_unknown_fields, rename_all = "camelCase")]
pub struct Config {
    #[serde(default)]
    pub env: Option<swc_ecma_preset_env::Config>,

    #[serde(default)]
    pub test: Option<FileMatcher>,

    #[serde(default)]
    pub exclude: Option<FileMatcher>,

    #[serde(default)]
    pub jsc: JscConfig,

View on GitHub (pinned to 5176682b65)

Solutions

  1. Add or fix a config entry whose `test` covers the failing extension, e.g. "test": "\\.m?js$"
  2. Check `exclude` on every entry - an entry may match `test` but be knocked out by `exclude`, causing fall-through
  3. Test the regex against the exact path form swc sees (absolute path, separators, case)
  4. Pass --config-file explicitly if a different/unexpected .swcrc is being loaded

Example fix

// before (.swcrc)
[
  { "test": "\\.tsx?$", "jsc": { "parser": { "syntax": "typescript" } } }
]
// after (.swcrc)
[
  { "test": "\\.tsx?$", "jsc": { "parser": { "syntax": "typescript" } } },
  { "test": "\\.m?js$", "jsc": { "parser": { "syntax": "ecmascript" } } }
]
Defensive patterns

Strategy: validation

Validate before calling

// JavaScript - verify a .swcrc entry claims the file before compiling
import fs from 'node:fs';
function configMatchesFile(swcrcPath, filePath) {
  const configs = JSON.parse(fs.readFileSync(swcrcPath, 'utf8'));
  const list = Array.isArray(configs) ? configs : [configs];
  return list.some((c) => {
    const test = c.test ? new RegExp(c.test) : null;
    const excl = c.exclude ? new RegExp(c.exclude) : null;
    return (!test || test.test(filePath)) && !(excl && excl.test(filePath));
  });
}

Prevention

When it happens

Trigger: A .swcrc whose entries all use `test` regexes like "\.tsx?$" while the compiled file is `.js`; an `exclude` pattern matching the path so the entry is skipped; multiple entries where the catch-all entry was removed. Hit via swc CLI compile or Compiler::process_js_file with a real file path.

Common situations: Adding plain .js files to a TypeScript-only .swcrc; exclude patterns broader than intended; regexes written for forward slashes failing on Windows paths; renaming file extensions (mjs/cjs/jsx) not covered by `test`.

Related errors


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