Configure
treefmt
's behaviour can be influenced in one of three ways:
- Process flags and arguments
- Environment variables
- A TOML based config file
There is an order of precedence between these mechanisms as listed above, with process flags having the highest precedence and values in the configuration file having the lowest.
Note
Some options can only be configured as process flags, others may support process flags and environment variables, and others still may support all three mechanisms.
Config File
The treefmt
configuration file is a mixture of global options and formatter sections.
It should be named treefmt.toml
or .treefmt.toml
, and typically resides at the root of a repository.
When executing treefmt
within a subdirectory, treefmt
will search upwards in the directory structure, looking for
treefmt.toml
or .treefmt.toml
.
You can change this behaviour using the config-file options
Tip
When starting a new project you can generate an initial config file using treefmt --init
Global Options
allow-missing-formatter
Do not exit with error if a configured formatter is missing.
ci
Runs treefmt in a CI mode, enabling no-cache, fail-on-change and adjusting some other settings best suited to a continuous integration environment.
clear-cache
Reset the evaluation cache. Use in case the cache is not precise enough.
config-file
cpu-profile
The file into which a pprof cpu profile will be written.
excludes
An optional list of glob patterns used to exclude files from all formatters.
fail-on-change
Exit with error if any changes were made during execution.
formatters
A list of formatters to apply. Defaults to all configured formatters.
no-cache
Ignore the evaluation cache entirely. Useful for CI.
on-unmatched
Log paths that did not match any formatters at the specified log level.
Possible values are <debug|info|warn|error|fatal>
.
Warning
If you select fatal
, the process will exit immediately with a non-zero exit.
stdin
Format the context passed in via stdin.
Note
You must provide a single path argument, the value of which is used to match against the configured formatters.
tree-root
The root directory from which treefmt will start walking the filesystem. Defaults to the directory containing the config file.
tree-root-file
File to search for to find the tree root (if tree-root
is not set)
verbose
Set the verbosity level of logs:
0
=>warn
1
=>info
2
=>debug
walk
The method used to traverse the files within the tree root. Currently, we support 'auto', 'git' or 'filesystem'
working-dir
Run as if treefmt
was started in the specified working directory instead of the current working directory.
Formatter Options
Formatters are configured using a table entry in treefmt.toml
of the form
[formatter.<name>]
:
command
The command to invoke when applying the formatter.
options
An optional list of args to be passed to command
.
includes
A list of glob patterns used to determine whether the formatter should be applied against a given path.
excludes
An optional list of glob patterns used to exclude certain files from this formatter.
priority
Influences the order of execution. Greater precedence is given to lower numbers, with the default being 0
.
Same file, multiple formatters?
For each file, treefmt
determines a list of formatters based on the configured includes
/ excludes
rules. This list is
then sorted, first by priority (lower the value, higher the precedence) and secondly by formatter name (lexicographically).
The resultant sequence of formatters is used to create a batch key, and similarly matched files get added to that batch until it is full, at which point the files are passed to each formatter in turn.
This means that treefmt
guarantees only one formatter will be operating on a given file at any point in time.
Another consequence is that formatting is deterministic for a given file and a given treefmt
configuration.
By setting the priority fields appropriately, you can control the order in which those formatters are applied for any files they both happen to match on.
Glob patterns format
This is a variant of the Unix glob pattern. It supports all the usual
selectors such as *
and ?
.
Examples
*.go
- match all files in the project that end with a ".go" file extension.vendor/*
- match all files under the vendor folder, recursively.
Supported Formatters
Any formatter that follows the spec is supported out of the box.
Already 60+ formatters are supported.
To find examples, take a look at https://github.com/numtide/treefmt-nix/tree/main/examples.
If you are a Nix user, you might also like https://github.com/numtide/treefmt-nix, which uses Nix to pull in the right formatter package and seamlessly integrates both together.