treefmt

repository·main·Indexed 21 days ago

https://github.com/numtide/treefmt

A formatter multiplexer that allows running multiple language-specific formatters across a multi-language repository using a single command. It optimizes performance by running formatters in parallel, processing only changed files via a bbolt cache, and supporting batch processing. Configuration is managed through a treefmt.toml file, and the tool includes a VS Code extension and shell autocompletion for bash, zsh, and fish.

Tokens
4.7K
Snippets
26
Records
35
Agent score
75%

What's inside treefmt

  1. How multiple formatters interact on a single file

    main

    When multiple formatters match a single file based on includes and excludes rules, treefmt follows these steps:

    1. Sorting: The list of applicable formatters is sorted first by priority (lower numbers have higher precedence) and secondly by formatter name (lexicographically).
    2. Execution: treefmt guarantees that only one formatter will be operating on a given file at any point in time.
    3. Batching: Files are grouped into batches based on their unique sequence of formatters. Once a batch is full, the files are passed to the formatters in the sorted sequence.

    This ensures that formatting is deterministic and that priority settings correctly control the order of operations for files that match multiple formatters.

  2. Configure treefmt precedence and mechanisms

    main

    treefmt behavior is controlled via three mechanisms, which follow a specific order of precedence (highest to lowest):

    1. Process flags and arguments
    2. Environment variables
    3. TOML-based configuration file

    Note that not all options support all three mechanisms; some may only be available as flags, while others support all three.

  3. Formatter Specification requirements for treefmt

    main

    To be compatible with treefmt, a formatter must adhere to a specific standard. treefmt handles the tree traversal and invokes the formatter only on selected files. If a formatter does not comply with these rules, you may need to create a wrapper script to transform its usage to match this specification.

    Required Rules (MUST)

    • Files passed as arguments: The formatter's CLI must accept files as trailing arguments in the format <command> [options] [...<files>]. It MUST process all specified files and MUST NOT ignore files based on VCS (Version Control System) status.
    • Write to changed files: The formatter MUST write changes back to the original file location. If no changes are needed, it MUST NOT write to the file.
    • Exit nonzero on error: If formatting fails (e.g., due to invalid syntax), the formatter MUST exit with a non-zero status code.
    • Process only specified files: It SHOULD only format the files passed to it.
    • Provide error details: It SHOULD print useful error information to stderr.
    • Idempotency: The formatter SHOULD be idempotent, producing stable outputs across multiple runs.
    • Reliability: The formatter is expected to be reliable and must not break the semantics of the files it formats.
    # Example of a compliant CLI usage:
    $ rustfmt --edition 2018 src/main.rs src/lib.rs
  4. Reduce log verbosity for unmatched files

    main

    By default, treefmt emits WARN messages for every file that does not match a configured formatter. To reduce noise in your output, you can set the on-unmatched configuration key to debug in your treefmt.toml file.

    If you need to temporarily see which files are unmatched during a run, you can override this setting using the --on-unmatched CLI flag.

    # treefmt.toml
    on-unmatched = "debug"
    $ treefmt --on-unmatched warn
  5. Initialize a treefmt configuration

    main

    To start using treefmt in a new project, generate a default treefmt.toml configuration file by running the --init flag. This file should be located in your project's root folder.

    $ treefmt --init
  6. Format specific files or directories

    main

    You can pass specific file paths or directory paths as arguments to treefmt. When passing directories, treefmt will traverse them using the configured walk strategy.

    # Format specific files
    > treefmt default.nix walk/walk.go nix/devshells/renovate.nix
    
    # Format directories
    > treefmt nix walk/cache