Skip to content

--exclude CLI flag overwrites .mdformat.toml exclude instead of merging #575

Description

@smol-ninja

Describe the bug

context
When --exclude is passed on the CLI, it completely replaces the exclude list from .mdformat.toml rather than merging with it. This makes it impossible to use both CLI and config exclusions together.

expectation
CLI --exclude patterns should be additive with .mdformat.toml exclude patterns. Passing --exclude "node_modules/**" on the CLI should add that pattern to whatever is already defined in the config file, not replace it.

use case
I maintain a shared just module that wraps mdformat for multiple repositories. Each repository has its own .mdformat.toml with repo-specific exclusions. I wanted to add a common exclusion (node_modules/**) at the CLI level without having to duplicate it across every repo's config file, but the current behavior makes this impossible.

Reproduce the bug

mkdir -p /tmp/mdformat-test/node_modules /tmp/mdformat-test/vendor

cat > /tmp/mdformat-test/.mdformat.toml << 'EOF'
exclude = [
  "vendor/**",
]
EOF

echo -e "# bad\n*  extra spaces" > /tmp/mdformat-test/node_modules/test.md
echo -e "# bad\n*  extra spaces" > /tmp/mdformat-test/vendor/test.md

cd /tmp/mdformat-test

Without --exclude (TOML works as expected):

$ mdformat --check .
Error: File "node_modules/test.md" is not formatted.
# vendor/test.md is correctly excluded by TOML

With --exclude (TOML exclusions are lost):

$ mdformat --check --exclude "node_modules/**" .
Error: File "vendor/test.md" is not formatted.
# vendor/test.md is no longer excluded — the TOML exclude was overwritten

List your environment

$ mdformat --version
mdformat 1.0.0 (mdformat-gfm 1.0.0, mdformat_frontmatter 2.0.10)

$ python3 --version
Python 3.14.13

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions