treefmt icon indicating copy to clipboard operation
treefmt copied to clipboard

`excludes` in `treefmt.toml` are ignored with `--stdin`

Open Stzx opened this issue 8 months ago • 10 comments

Describe the bug

Does treefmt.toml have no effect when using treefmt with --stdin?

To Reproduce

Steps to reproduce the behavior:

  1. configure zed & treefmt as above
  2. edit foo/bar.nix in zed
  3. save

Expected behavior

bar.nix file is not formatted

System information

system: nixos-unstable treefmt: v2.3.1 zed: v0.188.3

Additional context

treefmt.toml

...
[formatter.nixfmt-rfc-style]
command = "nixfmt"
includes = ["*.nix"]
excludes = ["*/bar.nix"]
...

Zed

...
languages = {
  Nix = {
...
    formatter.external = {
      command = "treefmt";
      arguments = ["--stdin" "{buffer_path}"];
    };
  };
};
...

Stzx avatar Jun 05 '25 10:06 Stzx

Does treefmt.toml have no effect when using treefmt with --stdin?

It does have an effect. If it did not, then foo/bar.nix wouldn't get formatted in your example (because treefmt wouldn't know how to format a .nix file.

You are correct that there's a bug here, though. I bet the issue is that we only use the excludes for walking the filesystem, and we also need to consider them when formatting via stdin.

~I'm pretty swamped right now. If someone hasn't fixed this by July, I'll take a look.~ This is on @brianmcgee's list.

jfly avatar Jun 06 '25 00:06 jfly

Should be easy enough to fix, I've added it to my list for this week.

brianmcgee avatar Jun 09 '25 07:06 brianmcgee

Looking at this a bit more closely, I think what you're seeing is this: https://github.com/numtide/treefmt/issues/573.

I believe we will respect excludes based on the extension, but because we currently don't preserve the original file path for the temp file we create from stdin, excludes = ["*/bar.nix"] will have no effect.

brianmcgee avatar Jun 13 '25 16:06 brianmcgee

IIRC, ~#53~ #573 is about threading the filename down to the underlying formatter, which requires an extension to our formatter specification (#586). Fixing this wouldn't require support from the underlying formatters, right? It's entirely up to treefmt itself.

jfly avatar Jun 13 '25 18:06 jfly

@brianmcgee quick ping here.

jfly avatar Jul 09 '25 17:07 jfly

#53 relates to v1 and we don't have the equivalent in v2.

I think this can only be properly addressed when the discussions around #586 come to a conclusion.

brianmcgee avatar Jul 21 '25 18:07 brianmcgee

Urg, sorry. There was a typo on my message. I still don't see how #586 is relevant here. This feels to me like something entirely up to treefmt's walker implementation(s).

jfly avatar Jul 21 '25 18:07 jfly

Let's see if we can have a quick chat sometime this week. Feels like my smooth brain is just not grasping the nuance on this one.

brianmcgee avatar Jul 21 '25 19:07 brianmcgee

Does treefmt.toml have no effect when using treefmt with --stdin?

It does have an effect. If it did not, then foo/bar.nix wouldn't get formatted in your example (because treefmt wouldn't know how to format a .nix file.

You are correct that there's a bug here, though. I bet the issue is that we only use the excludes for walking the filesystem, and we also need to consider them when formatting via stdin.

~I'm pretty swamped right now. If someone hasn't fixed this by July, I'll take a look.~ This is on @brianmcgee's list.

Yeah, what seems to be getting considered for exclusions is the /some/tmp/path/12345.nix rather than actual_name.nix.

lf- avatar Nov 03 '25 21:11 lf-

I completely forgot about this one. I need to test if it's still an issue in 2.4.0 as I did some fixes wrt stdin.

brianmcgee avatar Nov 04 '25 13:11 brianmcgee