Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(nix fmt): remove the default "." argument #11438

Merged
merged 1 commit into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions doc/manual/rl-next/nix-fmt-default-argument.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
synopsis: Removing the default argument passed to the `nix fmt` formatter
issues: []
prs: [11438]
---

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.

Author: [**@zimbatm**](https://github.com/zimbatm)
10 changes: 2 additions & 8 deletions src/nix/fmt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,8 @@ struct CmdFmt : SourceExprCommand {
Strings programArgs{app.program};

// 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) {
programArgs.push_back(i);
}
for (auto &i : args) {
programArgs.push_back(i);
}

// Release our references to eval caches to ensure they are persisted to disk, because
Expand Down
11 changes: 7 additions & 4 deletions tests/functional/fmt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ source common.sh
TODO_NixOS # Provide a `shell` variable. Try not to `export` it, perhaps.

clearStoreIfPossible
rm -rf $TEST_HOME/.cache $TEST_HOME/.config $TEST_HOME/.local
rm -rf "$TEST_HOME"/.cache "$TEST_HOME"/.config "$TEST_HOME"/.local

cp ./simple.nix ./simple.builder.sh ./fmt.simple.sh ./config.nix $TEST_HOME
cp ./simple.nix ./simple.builder.sh ./fmt.simple.sh ./config.nix "$TEST_HOME"

cd $TEST_HOME
cd "$TEST_HOME"

nix fmt --help | grep "Format"

Expand All @@ -30,6 +30,9 @@ cat << EOF > flake.nix
};
}
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 show | grep -P "package 'formatter'"
3 changes: 2 additions & 1 deletion tests/functional/fmt.simple.sh
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
echo Formatting: "${@}"
#!/usr/bin/env bash
echo "Formatting(${#}):" "${@}"
Loading