Skip to content

Commit 12473be

Browse files
committed
fix(nix fmt): remove the default "." argument
When `nix fmt` is called without an argument, Nix appends the "." argument before calling the formatter. The comment in the code is: > Format the current flake out of the box This also happens when formatting sub-folders. This means that the formatter is now unable to distinguish, as an interface, whether the "." argument is coming from the flake or the user's intent to format the current folder. This decision should be up to the formatter. Treefmt, for example, will automatically look up the project's root and format all the files. This is the desired behaviour. But because the "." argument is passed, it cannot function as expected.
1 parent 5bfe198 commit 12473be

File tree

4 files changed

+28
-13
lines changed

4 files changed

+28
-13
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
synopsis: Removing the default argument passed to the `nix fmt` formatter
3+
issues: []
4+
prs: [11438]
5+
---
6+
7+
The underlying formatter no longer receives the ". " default argument when `nix fmt` is called with no arguments.
8+
9+
This change was necessary as the formatter wasn't able to distinguish between
10+
a user wanting to format the current folder with `nix fmt .` or the generic
11+
`nix fmt`.
12+
13+
The default behaviour is now the responsibility of the formatter itself, and
14+
allows tools such as treefmt to format the whole tree instead of only the
15+
current directory and below.
16+
17+
Author: [**@zimbatm**](https://github.com/zimbatm)

src/nix/fmt.cc

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,8 @@ struct CmdFmt : SourceExprCommand {
4040
Strings programArgs{app.program};
4141

4242
// Propagate arguments from the CLI
43-
if (args.empty()) {
44-
// Format the current flake out of the box
45-
programArgs.push_back(".");
46-
} else {
47-
// User wants more power, let them decide which paths to include/exclude
48-
for (auto &i : args) {
49-
programArgs.push_back(i);
50-
}
43+
for (auto &i : args) {
44+
programArgs.push_back(i);
5145
}
5246

5347
// Release our references to eval caches to ensure they are persisted to disk, because

tests/functional/fmt.sh

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ source common.sh
55
TODO_NixOS # Provide a `shell` variable. Try not to `export` it, perhaps.
66

77
clearStoreIfPossible
8-
rm -rf $TEST_HOME/.cache $TEST_HOME/.config $TEST_HOME/.local
8+
rm -rf "$TEST_HOME"/.cache "$TEST_HOME"/.config "$TEST_HOME"/.local
99

10-
cp ./simple.nix ./simple.builder.sh ./fmt.simple.sh ./config.nix $TEST_HOME
10+
cp ./simple.nix ./simple.builder.sh ./fmt.simple.sh ./config.nix "$TEST_HOME"
1111

12-
cd $TEST_HOME
12+
cd "$TEST_HOME"
1313

1414
nix fmt --help | grep "Format"
1515

@@ -30,6 +30,9 @@ cat << EOF > flake.nix
3030
};
3131
}
3232
EOF
33-
nix fmt ./file ./folder | grep 'Formatting: ./file ./folder'
33+
# No arguments check
34+
[[ "$(nix fmt)" = "Formatting(0): " ]]
35+
# Argument forwarding check
36+
nix fmt ./file ./folder | grep 'Formatting(2): ./file ./folder'
3437
nix flake check
3538
nix flake show | grep -P "package 'formatter'"

tests/functional/fmt.simple.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
echo Formatting: "${@}"
1+
#!/usr/bin/env bash
2+
echo "Formatting(${#}):" "${@}"

0 commit comments

Comments
 (0)