Skip to content

Add --config option to specify explicit configuration file path #562

@meek2100

Description

@meek2100

Context

Context

Provide background to help others understand this issue.

The current configuration loading logic relies on recursive file tree traversal to find a file named .mdformat.toml or loading from Path.cwd() for stdin. This auto-discovery model is simple but inflexible for complex projects.

Describe the problem or need you'd like to address.

The reliance on a fixed filename and recursive search creates friction for users who manage configuration:

  1. Pre-commit Integration: When using mdformat in a .pre-commit-config.yaml hook alongside other tools (like markdownlint or hadolint), it is standard practice to store all configuration files in a shared directory (e.g., .config/). Most tools support explicitly loading a config via a --config flag, but mdformat does not, forcing an exception to the directory structure.
  2. Centralized Configuration: Users cannot explicitly point mdformat to a configuration file located outside the file's directory path (e.g., at the root level in a path like .config/mdformat.toml), complicating centralized maintenance.

The core problem is the lack of a mechanism to override the default config discovery path.

Proposal

Proposal

A simple and clear description of what you're proposing.

Introduce a new optional command-line flag, --config <path>, that allows a user to specify the absolute or relative path to a single configuration file.

This explicit path must override the default auto-discovery mechanism. If a path is provided via --config, mdformat should only attempt to load settings from that single file and ignore any .mdformat.toml files in the directory tree.

Ideas or constraints for how to implement this proposal

  • The flag should be defined with type=Path in argparse.
  • The implementation should include a new internal function to load configuration directly from the provided path, separate from the cached recursive lookup function.
  • If the file specified by --config does not exist, mdformat must print an error and exit with a non-zero code (e.g., 1), similar to how it handles formatting errors.

Important considerations to think about or discuss

  1. Precedence: The --config argument must take the highest precedence, overriding both recursive file discovery and any other implicit config loading (like pyproject.toml if extensions are used).
  2. Compatibility: Since the config file format remains TOML (.mdformat.toml syntax), compatibility with all existing options (wrap, number, plugin.*, etc.) is maintained.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions