Skip to content

Commit

Permalink
Deprecate recursive mode
Browse files Browse the repository at this point in the history
The recursive mode has caused problems because it doesn't do any
filtering, which can mess with files in `.git` directories and
elsewhere. While we could support sane implicit filters and an interface
to filter explicitly, that adds complexity and maintenance burden.

Instead, we can promote the use of `treefmt` instead, a "formatting
multiplexer", which supports file filtering by default. So `nixfmt` will
only be the "backend" formatter, while `treefmt` is the frontend.

Previously discussed in a team meeting here:
#151 (comment)
  • Loading branch information
infinisil committed Aug 27, 2024
1 parent 14be7e6 commit ba0c3fa
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
25 changes: 20 additions & 5 deletions main/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
module Main where

import Control.Concurrent (Chan, forkIO, newChan, readChan, writeChan)
import Control.Monad (unless)
import Control.Monad.Trans.Class (lift)
import Control.Monad.Trans.State.Strict (StateT, evalStateT, get, put)
import Data.ByteString.Char8 (unpack)
import Data.Either (lefts)
import Data.FileEmbed
Expand Down Expand Up @@ -87,19 +90,31 @@ data Target = Target
}

-- | Recursively collect nix files in a directory
collectNixFiles :: FilePath -> IO [FilePath]
collectNixFiles :: FilePath -> StateT Bool IO [FilePath]
collectNixFiles path = do
dir <- doesDirectoryExist path
dir <- lift $ doesDirectoryExist path
if
| dir -> do
files <- listDirectory path
warnDeprecated
files <- lift $ listDirectory path
concat <$> mapM collectNixFiles ((path </>) <$> files)
| ".nix" `isSuffixOf` path -> pure [path]
| otherwise -> pure []
| otherwise -> do
warnDeprecated
pure []
where
warnDeprecated = do
warningPrinted <- get
-- We don't want to print the warning more than once
unless warningPrinted $ do
lift $ hPutStrLn stderr $ "\ESC[33mPassing directories or non-Nix files (such as \"" <> path <> "\") is deprecated and will be unsupported soon, please use https://treefmt.com/ instead, e.g. via https://github.com/numtide/treefmt-nix\ESC[0m"
put True

-- | Recursively collect nix files in a list of directories
collectAllNixFiles :: [FilePath] -> IO [FilePath]
collectAllNixFiles paths = concat <$> mapM collectNixFiles paths
collectAllNixFiles paths =
-- The state represents whether a deprecation warning was already printed for a directory being passed
flip evalStateT False $ concat <$> mapM collectNixFiles paths

formatTarget :: Formatter -> Target -> IO Result
formatTarget format Target{tDoRead, tPath, tDoWrite} = do
Expand Down
1 change: 1 addition & 0 deletions nixfmt.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ executable nixfmt
, nixfmt
, unix >= 2.7.2 && < 2.9
, text >= 1.2.3 && < 2.2
, transformers

-- for System.IO.Atomic
, directory >= 1.3.3 && < 1.4
Expand Down

0 comments on commit ba0c3fa

Please sign in to comment.