Skip to content

Commit 66bd58d

Browse files
committed
Implement treefmt's stdin spec
This completes #305
1 parent 2153e60 commit 66bd58d

3 files changed

Lines changed: 19 additions & 15 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,9 +291,9 @@ to return back to the unmerged state.
291291

292292
## Usage
293293

294-
* `nixfmt < input.nix` – reads Nix code from `stdin`, formats it, and outputs to `stdout`
294+
* `echo "{a=1;}" | nixfmt --stdin-filepath input.nix` – reads Nix code from `stdin`, formats it, and outputs to `stdout` (the filepath (`input.nix`) is only used for error messages)
295295
* `nixfmt file.nix` – format the file in place
296296

297-
## Acknowledgements
297+
## Acknowledgments
298298

299299
`nixfmt` was originally developed by [Serokell](https://github.com/serokell) and later donated to become an official Nix project with the acceptance of [RFC 166](https://github.com/NixOS/rfcs/pull/166).

main/Main.hs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import Data.ByteString.Char8 (unpack)
1515
import Data.Either (lefts)
1616
import Data.FileEmbed
1717
import Data.List (intersperse, isSuffixOf)
18-
import Data.Maybe (fromMaybe)
1918
import Data.Text (Text)
2019
import qualified Data.Text.IO as TextIO (getContents, hPutStr, putStr)
2120
import Data.Version (showVersion)
@@ -57,7 +56,7 @@ data Nixfmt = Nixfmt
5756
strict :: Bool,
5857
verify :: Bool,
5958
ast :: Bool,
60-
filename :: Maybe FilePath,
59+
stdin_filepath :: Maybe FilePath,
6160
ir :: Bool
6261
}
6362
deriving (Show, Data, Typeable)
@@ -89,11 +88,11 @@ options =
8988
False
9089
&= help
9190
"Pretty print the internal AST, only for debugging",
92-
filename =
91+
stdin_filepath =
9392
Nothing
9493
&= help
95-
"The filename to display when the file input is given through stdin.\n\
96-
\Useful for tools like editors and autoformatters that wish to use Nixfmt without providing it direct file access, while still providing context to where the file is.",
94+
"Format the contents of stdin. When specified, no positional arguments are allowed.\n\
95+
\The filepath is is only used for error messages.",
9796
ir =
9897
False
9998
&= help
@@ -153,8 +152,8 @@ checkTarget format Target{tDoRead, tPath} = do
153152
| formatted == contents -> Right ()
154153
| otherwise -> Left $ tPath ++ ": not formatted"
155154

156-
stdioTarget :: Maybe FilePath -> Target
157-
stdioTarget filename = Target TextIO.getContents (fromMaybe "<stdin>" filename) (const TextIO.putStr)
155+
stdioTarget :: FilePath -> Target
156+
stdioTarget filepath = Target TextIO.getContents filepath (const TextIO.putStr)
158157

159158
fileTarget :: FilePath -> Target
160159
fileTarget path = Target (readFileUtf8 path) path atomicWriteFile
@@ -169,8 +168,8 @@ checkFileTarget :: FilePath -> Target
169168
checkFileTarget path = Target (readFileUtf8 path) path (const $ const $ pure ())
170169

171170
toTargets :: Nixfmt -> IO [Target]
172-
toTargets Nixfmt{files = [], filename} = pure [stdioTarget filename]
173-
toTargets Nixfmt{files = ["-"], filename} = pure [stdioTarget filename]
171+
toTargets Nixfmt{files = [], stdin_filepath = Just filepath} = pure [stdioTarget filepath]
172+
toTargets Nixfmt{files = _ : _, stdin_filepath = Just _} = error "Cannot specify both positional files and --stdin-filepath"
174173
toTargets Nixfmt{check = False, files = paths} = map fileTarget <$> collectAllNixFiles paths
175174
toTargets Nixfmt{check = True, files = paths} = map checkFileTarget <$> collectAllNixFiles paths
176175

test/test.sh

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
#!/usr/bin/env bash
22

3+
# We invoke `nixmft` in a way that makes shellcheck think we're clobbering the
4+
# file we're formatting as we're reading it. That's not actually happening
5+
# here.
6+
# shellcheck disable=SC2094
7+
38
set -euo pipefail
49

510
# Simple test runner for nixfmt.
@@ -35,7 +40,7 @@ verifyCorrect() {
3540
shift
3641

3742
echo "Checking $file (${*:-no options}) …"
38-
if ! out=$(nixfmt --strict --verify "$@" < "$file"); then
43+
if ! out=$(nixfmt --strict --verify --stdin-filepath "$file" "$@" < "$file"); then
3944
echo "[ERROR] failed nixfmt verification"
4045
exit 1
4146
fi
@@ -60,7 +65,7 @@ verifyCorrect test/correct-indent-4.nix --indent=4
6065

6166
# Verify "invalid"
6267
for file in test/invalid/*.nix; do
63-
if nixfmt < "$file" > /dev/null 2>&1; then
68+
if nixfmt --stdin-filepath "test-$file" < "$file" > /dev/null 2>&1; then
6469
echo "[ERROR] $file should have failed nixfmt"
6570
exit 1
6671
else
@@ -71,7 +76,7 @@ done
7176
# Verify "diff"
7277
for file in test/diff/**/in.nix; do
7378
echo "Checking $file"
74-
out="$(nixfmt --verify < "$file")"
79+
out="$(nixfmt --verify --stdin-filepath "$file" < "$file")"
7580
outfile="$(dirname "$file")/out.nix"
7681

7782
if diff --color=always --unified "$outfile" <(echo "$out"); then
@@ -85,7 +90,7 @@ for file in test/diff/**/in.nix; do
8590
fi
8691

8792
echo "Checking $file with --strict …"
88-
out="$(nixfmt --strict --verify < "$file")"
93+
out="$(nixfmt --strict --verify --stdin-filepath "$file" < "$file")"
8994
outfile="$(dirname "$file")/out-pure.nix"
9095

9196
if diff --color=always --unified "$outfile" <(echo "$out"); then

0 commit comments

Comments
 (0)