- validates that all path arguments exist and are contained within the tree root
- fixes a bug with git index checking, where we were ignoring directories
Signed-off-by: Brian McGee <brian@bmcgee.ie>
The contract seems to be that the `walkFilesystem` goroutine is
responsible for closing `f.filesCh`, but before this change, there were
codepaths that could result in the gorouting exiting without closing
`f.filesCh`. That shouldn't be possible anymore, so long as we keep this
statement at the top of the function =)
This fixes https://github.com/numtide/treefmt/issues/406
If a formatter errors out, continue with subsequent formatters regardless.
Do not cache the result to ensure later invocations re-try the same files.
Signed-off-by: Brian McGee <brian@bmcgee.ie>
When configuring treefmt with relative paths to commands, for example if
you want to create your own repo-specific wrappers, treefmt would fail
if executed in a sub-directory.
This change makes sure that the relative path is resolved based on the
treefmt root instead of the current directory.
By default, we log the changed file at debug level. If the `--fail-on-change` flag has been provided, we log the changed file at error level.
This is to surface issues better in CI pipelines for example.
Signed-off-by: Brian McGee <brian@bmcgee.ie>
This can happen if a user removes a file from the filesystem and that change hasn't been staged yet.
We log a warning when this happens and continue.
Signed-off-by: Brian McGee <brian@bmcgee.ie>
Adds a ci flag which does the following:
- ensures INFO level logging at a minimum
- --no-cache
- --fail-on-change
This also adds a delay on startup which is intended to ensure we do not start doing anything until we have moved into the second after the one in which the process started. This helps to ensure accurate comparisons of `modtime` using truncated second-level precision for change detection.
This is only an issue in scenarios in which treefmt is being executed quickly in succession or directly after an automatic checkout, such as in CI.
Signed-off-by: Brian McGee <brian@bmcgee.ie>
We were using whatever precision the underlying file system was giving us, and in some cases that can be very detailed. Some formatters mess with the mod time, but not to the same precision (e.g. dos2unix).
POSIX also specifies that mod time should be EPOCH (second) precision.
This change brings us back in line with how 1.x worked, and should resolve issues with false fail on change errors.
Separates out the logic for detecting changes from the logic for updating the cache, fixing the case where both `--fail-on-change` and `--no-cache` are enabled.
Signed-off-by: Brian McGee <brian@bmcgee.ie>
Co-authored-by: Jonas Chevalier <zimbatm@zimbatm.com>
Signed-off-by: Brian McGee <brian@bmcgee.ie>
Separates out the logic
for detecting changes from the logic for updating the cache, fixing the case where both `--fail-on-change` and `--no-cache`
are enabled.
Closes#343
Signed-off-by: Brian McGee <brian@bmcgee.ie>
Separates global excludes processing from `Formatter.Wants`. This removes redundant processing of global excludes in each `Formatter.Wants` call.
If a file has been globally excluded, we do not emit an `on-unmatched` log message. This should help reduce as reported in #317.
Signed-off-by: Brian McGee <brian@bmcgee.ie>
This was incorrectly ported from Rust to Go.
When `--stdin` is provided, `treefmt` copy the `stdin` into a temporary file, using the first path argument as the filename. This allows the user to control which formatters will match this temp file based on their `treefmt` config.
After the formatters have been applied, the contents of this temporary file are then printed to stdout and the temp file is removed.
Signed-off-by: Brian McGee <brian@bmcgee.ie>
This was incorrectly ported from Rust to Go.
When provided, `treefmt` will take the contents of stdin and place them into the file provided with the `--stdin` flag, then format it according to the configured formatters.
If the file doesn't exist it is created. If it exists, it is first truncated and then populated with stdin.
Signed-off-by: Brian McGee <brian@bmcgee.ie>
Restore the treefmt 1.x behaviour where it would search for the tree
root by recursively searching for the treefmt.toml file up the
filesystem, starting from the current directory.
The `--tree-root-file` option will be useful to remove this bash wrapper: 2fba33a182/module-options.nix (L116-L135)Fixes#308
By default, if a path does not match any formatter a log message at WARN level will be emitted. A user can change this by providing the `--on-unmatched` or `-u` flag and specifying a log level `debug,info,warn,error,fatal`.
If fatal, the process will exit with an error on the first unmatched path encountered.
Closes#302
Signed-off-by: Brian McGee <brian@bmcgee.ie>
For each path we determine the list of formatters that are interested in formatting it. From there, we sort
the list of formatters first by priority (lower value, higher priority) and then by name (lexicographically).
With this information we create a batch key which is based on the unique sequence of formatters. When enough paths with the same sequence is ready we apply them in order to each formatter.
By default, with no special configuration, this model guarantees that a given path will only be processed by one formatter at a time.
If a user wishes to influence the order in which formatters are applied they can use the priority field.
Signed-off-by: Brian McGee <brian@bmcgee.ie>