numtide / treefmt

one CLI to format your repo [maintainers=@zimbatm,@brianmcgee]
https://treefmt.com
MIT License
602 stars 37 forks source link

Formatting through stdin no longer does anything #376

Closed veselyn closed 2 months ago

veselyn commented 2 months ago

To Reproduce

Example test file:

$ cat test.nix
{ description = "A very basic flake"; inputs = { nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable"; flake-utils.url = "github:numtide/flake-utils/main"; treefmt.url = "github:numtide/treefmt-nix/main"; devenv.url = "github:cachix/devenv"; devenv.inputs.nixpkgs.follows = "nixpkgs"; }; outputs = { self, nixpkgs, flake-utils, treefmt, devenv, } @ inputs: flake-utils.lib.eachDefaultSystem (system: let pkgs = import nixpkgs {inherit system;}; treefmtModule = treefmt.lib.evalModule pkgs { projectRootFile = "flake.nix"; programs = {alejandra.enable = true;}; }; in { formatter = treefmtModule.config.build.wrapper; devShells.default = devenv.lib.mkShell { inherit inputs pkgs ; modules  = [ { packages = [treefmtModule.config.build.wrapper]; } ]; }; }); }

After formatting it with treefmt:

$ cat $(which treefmt)
#!/nix/store/fyjay93q3dq2hx3dhx7zhr8kyjnkh9m8-bash-5.2p26/bin/bash
set -euo pipefail
exec /nix/store/5ndd9xi854cg9ikxnqv93lcggc17cqc4-treefmt-2.0.3/bin/treefmt \
  --config-file=/nix/store/w9b2zbjnxvwhyldwaps47hzczd49qllc-treefmt.toml \
  --tree-root-file=flake.nix \
  "$@"

$ cat /nix/store/w9b2zbjnxvwhyldwaps47hzczd49qllc-treefmt.toml
[formatter]
[formatter.alejandra]
command = "/nix/store/xzx59278kikxd9kxvgh98plj690kmjjg-alejandra-3.0.0/bin/alejandra"
excludes = []
includes = ["*.nix"]
options = []

[formatter.prettier]
command = "/nix/store/4wa1gl9gslnc9r6id5c5v0yv6jxn0b7b-prettier-3.3.3/bin/prettier"
excludes = []
includes = ["*.cjs", "*.css", "*.html", "*.js", "*.json", "*.json5", "*.jsx", "*.md", "*.mdx", "*.mjs", "*.scss", "*.ts", "*.tsx", "*.vue", "*.yaml", "*.yml"]
options = ["--write"]

[formatter.rustfmt]
command = "/nix/store/21nby83mwb48gz21ng551iygmnhdl87l-rustfmt-1.79.0/bin/rustfmt"
excludes = []
includes = ["*.rs"]
options = ["--edition", "2021"]

[global]
excludes = ["*.lock", "*.patch", "package-lock.json", ".gitignore", ".gitmodules", ".hgignore", ".svnignore"]

$ treefmt --stdin test.nix < test.nix
{ description = "A very basic flake"; inputs = { nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable"; flake-utils.url = "github:numtide/flake-utils/main"; treefmt.url = "github:numtide/treefmt-nix/main"; devenv.url = "github:cachix/devenv"; devenv.inputs.nixpkgs.follows = "nixpkgs"; }; outputs = { self, nixpkgs, flake-utils, treefmt, devenv, } @ inputs: flake-utils.lib.eachDefaultSystem (system: let pkgs = import nixpkgs {inherit system;}; treefmtModule = treefmt.lib.evalModule pkgs { projectRootFile = "flake.nix"; programs = {alejandra.enable = true;}; }; in { formatter = treefmtModule.config.build.wrapper; devShells.default = devenv.lib.mkShell { inherit inputs pkgs; modules = [{packages = [treefmtModule.config.build.wrapper];}]; }; }); }

$ cat test.nix
{ description = "A very basic flake"; inputs = { nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable"; flake-utils.url = "github:numtide/flake-utils/main"; treefmt.url = "github:numtide/treefmt-nix/main"; devenv.url = "github:cachix/devenv"; devenv.inputs.nixpkgs.follows = "nixpkgs"; }; outputs = { self, nixpkgs, flake-utils, treefmt, devenv, } @ inputs: flake-utils.lib.eachDefaultSystem (system: let pkgs = import nixpkgs {inherit system;}; treefmtModule = treefmt.lib.evalModule pkgs { projectRootFile = "flake.nix"; programs = {alejandra.enable = true;}; }; in { formatter = treefmtModule.config.build.wrapper; devShells.default = devenv.lib.mkShell { inherit inputs pkgs; modules = [{packages = [treefmtModule.config.build.wrapper];}]; }; }); }

Expected behavior

After formatting it directly with Alejandra:

$ alejandra test.nix
Checking style in 1 file using 11 threads.

Formatted: test.nix

Success! 1 file was formatted.

👏 Special thanks to Daniel Salazar for being a sponsor of Alejandra!

$ cat test.nix
{
  description = "A very basic flake";
  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
    flake-utils.url = "github:numtide/flake-utils/main";
    treefmt.url = "github:numtide/treefmt-nix/main";
    devenv.url = "github:cachix/devenv";
    devenv.inputs.nixpkgs.follows = "nixpkgs";
  };
  outputs = {
    self,
    nixpkgs,
    flake-utils,
    treefmt,
    devenv,
  } @ inputs:
    flake-utils.lib.eachDefaultSystem (system: let
      pkgs = import nixpkgs {inherit system;};
      treefmtModule = treefmt.lib.evalModule pkgs {
        projectRootFile = "flake.nix";
        programs = {alejandra.enable = true;};
      };
    in {
      formatter = treefmtModule.config.build.wrapper;
      devShells.default = devenv.lib.mkShell {
        inherit inputs pkgs;
        modules = [{packages = [treefmtModule.config.build.wrapper];}];
      };
    });
}

System information

Darwin Treefmt 2.0.3 Alejandra 3.0.0

Additional context

This used to work until recently when I updated my dependencies. After a rigorous debugging session, I'm almost certain something has changed with treefmt, and it looks like a bug.

This is from treefmt 0.6.1:

$ cat /nix/store/4q18x0g0f0n2kcrnk8h33823sdx9yqyw-treefmt.toml
[formatter]
[formatter.alejandra]
command = "/nix/store/ri1n3a74i8a4kmhqm7lq8gsqmhwf044i-alejandra-3.0.0/bin/alejandra"
excludes = []
includes = ["*.nix"]
options = []

[formatter.prettier]
command = "/nix/store/3w1hjyp00jj056wqv7f351f21zr5w7yd-prettier-3.2.5/bin/prettier"
excludes = []
includes = ["*.cjs", "*.css", "*.html", "*.js", "*.json", "*.json5", "*.jsx", "*.md", "*.mdx", "*.mjs", "*.scss", "*.ts", "*.tsx", "*.vue", "*.yaml", "*.yml"]
options = ["--write"]

[formatter.rustfmt]
command = "/nix/store/p1nxaljxhb5rjzphz6v8gjjh366qsmra-rustfmt-1.77.2/bin/rustfmt"
excludes = []
includes = ["*.rs"]
options = ["--edition", "2021"]

[global]
excludes = ["*.lock", "*.patch", "package-lock.json", ".gitignore", ".gitmodules", ".hgignore", ".svnignore"]
$ cat test.nix
{ description = "A very basic flake"; inputs = { nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable"; flake-utils.url = "github:numtide/flake-utils/main"; treefmt.url = "github:numtide/treefmt-nix/main"; devenv.url = "github:cachix/devenv"; devenv.inputs.nixpkgs.follows = "nixpkgs"; }; outputs = { self, nixpkgs, flake-utils, treefmt, devenv, } @ inputs: flake-utils.lib.eachDefaultSystem (system: let pkgs = import nixpkgs {inherit system;}; treefmtModule = treefmt.lib.evalModule pkgs { projectRootFile = "flake.nix"; programs = {alejandra.enable = true;}; }; in { formatter = treefmtModule.config.build.wrapper; devShells.default = devenv.lib.mkShell { inherit inputs pkgs; modules = [{packages = [treefmtModule.config.build.wrapper];}]; }; }); }
$ treefmt --stdin test.nix < test.nix
[INFO ] running #alejandra
{
  description = "A very basic flake";
  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
    flake-utils.url = "github:numtide/flake-utils/main";
    treefmt.url = "github:numtide/treefmt-nix/main";
    devenv.url = "github:cachix/devenv";
    devenv.inputs.nixpkgs.follows = "nixpkgs";
  };
  outputs = {
    self,
    nixpkgs,
    flake-utils,
    treefmt,
    devenv,
  } @ inputs:
    flake-utils.lib.eachDefaultSystem (system: let
      pkgs = import nixpkgs {inherit system;};
      treefmtModule = treefmt.lib.evalModule pkgs {
        projectRootFile = "flake.nix";
        programs = {alejandra.enable = true;};
      };
    in {
      formatter = treefmtModule.config.build.wrapper;
      devShells.default = devenv.lib.mkShell {
        inherit inputs pkgs;
        modules = [{packages = [treefmtModule.config.build.wrapper];}];
      };
    });
}
$ treefmt --version
treefmt 0.6.1
brianmcgee commented 2 months ago

@veselyn thanks for the report, I'll try to find some time to look into this today

brianmcgee commented 2 months ago

I tested this with latest main and it works as expected.

❯ nix run github:numtide/treefmt -- -vv --stdin test.nix < test.nix
DEBU format: config-file=/tmp/tmp.QbRYUfUNKN/treefmt.toml tree-root=/tmp/tmp.QbRYUfUNKN
DEBU format | alejandra: match: /run/user/1000/3418258967.nix
DEBU format | alejandra: executing: /nix/store/qnx064899v5wn40gps5gkwczkhq35j61-alejandra-3.0.0/bin/alejandra ../../run/user/1000/3418258967.nix
INFO format | alejandra: 1 file(s) processed in 3.880804ms
DEBU format: file has changed path=/run/user/1000/3418258967.nix prev_size=758 current_size=881 prev_mod_time="2024-08-08 09:25:42 +0100 IST" current_mod_time="2024-08-08 09:25:42 +0100 IST"
{
  description = "A very basic flake";
  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
    flake-utils.url = "github:numtide/flake-utils/main";
    treefmt.url = "github:numtide/treefmt-nix/main";
    devenv.url = "github:cachix/devenv";
    devenv.inputs.nixpkgs.follows = "nixpkgs";
  };
  outputs = {
    self,
    nixpkgs,
    flake-utils,
    treefmt,
    devenv,
  } @ inputs:
    flake-utils.lib.eachDefaultSystem (system: let
      pkgs = import nixpkgs {inherit system;};
      treefmtModule = treefmt.lib.evalModule pkgs {
        projectRootFile = "flake.nix";
        programs = {alejandra.enable = true;};
      };
    in {
      formatter = treefmtModule.config.build.wrapper;
      devShells.default = devenv.lib.mkShell {
        inherit inputs pkgs;
        modules = [{packages = [treefmtModule.config.build.wrapper];}];
      };
    });
}

The issue is related to these fixes:

I will cut a new release in the next day or so, as there are several bug fixes to go out.