Merge "fix(nix fmt): remove the default "." argument" into main

This commit is contained in:
Jonas Chevalier 2024-09-30 16:10:32 +00:00 committed by Gerrit Code Review
commit a16ceb9411
5 changed files with 48 additions and 10 deletions

View file

@ -147,3 +147,6 @@ winter:
yshui: yshui:
github: yshui github: yshui
zimbatm:
github: zimbatm

View file

@ -0,0 +1,38 @@
---
synopsis: Removing the `.` default argument passed to the `nix fmt` formatter
issues: []
prs: [11438]
cls: [1902]
category: Breaking Changes
credits: zimbatm
---
The underlying formatter no longer receives the ". " default argument when `nix fmt` is called with no arguments.
This change was necessary as the formatter wasn't able to distinguish between
a user wanting to format the current folder with `nix fmt .` or the generic
`nix fmt`.
The default behaviour is now the responsibility of the formatter itself, and
allows tools such as treefmt to format the whole tree instead of only the
current directory and below.
This may cause issues with some formatters: nixfmt, nixpkgs-fmt and alejandra currently format stdin when no arguments are passed.
Here is a small wrapper example that will restore the previous behaviour for such a formatter:
```nix
{
outputs = { self, nixpkgs, systems }:
let
eachSystem = nixpkgs.lib.genAttrs (import systems) (system: nixpkgs.legacyPackages.${system});
in
{
formatter = eachSystem (pkgs:
pkgs.writeShellScriptBin "formatter" ''
if [[ $# = 0 ]]; set -- .; fi
exec "${pkgs.nixfmt-rfc-style}/bin/nixfmt "$@"
'');
};
}
```

View file

@ -39,15 +39,9 @@ struct CmdFmt : SourceExprCommand {
Strings programArgs{app.program}; Strings programArgs{app.program};
// Propagate arguments from the CLI // Propagate arguments from the CLI
if (args.empty()) {
// Format the current flake out of the box
programArgs.push_back(".");
} else {
// User wants more power, let them decide which paths to include/exclude
for (auto &i : args) { for (auto &i : args) {
programArgs.push_back(i); programArgs.push_back(i);
} }
}
runProgramInStore(store, UseSearchPath::DontUse, app.program, programArgs); runProgramInStore(store, UseSearchPath::DontUse, app.program, programArgs);
}; };

View file

@ -26,7 +26,10 @@ cat << EOF > flake.nix
}; };
} }
EOF EOF
nix fmt ./file ./folder | grep 'Formatting: ./file ./folder' # No arguments check
[[ "$(nix fmt)" = "Formatting(0):" ]]
# Argument forwarding check
nix fmt ./file ./folder | grep 'Formatting(2): ./file ./folder'
nix flake check nix flake check
nix flake show | grep -P "package 'formatter'" nix flake show | grep -P "package 'formatter'"

View file

@ -1,3 +1,3 @@
#!/usr/bin/env bash #!/usr/bin/env bash
echo Formatting: "${@}" echo "Formatting(${#}):" "${@}"