11{-# language ApplicativeDo #-}
2+ {-# language ScopedTypeVariables #-}
23{-# language BlockArguments #-}
34{-# language FlexibleContexts #-}
45{-# language NamedFieldPuns #-}
@@ -14,11 +15,11 @@ module Weeder.Main ( main, mainWithConfig, getHieFiles ) where
1415import Control.Concurrent.Async ( async , link , ExceptionInLinkedThread ( ExceptionInLinkedThread ) )
1516
1617-- base
17- import Control.Exception ( Exception , throwIO , displayException , catches , Handler ( Handler ), SomeException ( SomeException ) )
18+ import Control.Exception ( Exception , throwIO , displayException , catches , Handler ( Handler ), SomeException ( SomeException ))
1819import Control.Concurrent ( getChanContents , newChan , writeChan , setNumCapabilities )
20+ import Data.List
1921import Control.Monad ( unless , when )
2022import Data.Foldable
21- import Data.List ( isSuffixOf )
2223import Data.Maybe ( isJust , catMaybes )
2324import Data.Version ( showVersion )
2425import System.Exit ( ExitCode (.. ), exitWith )
@@ -28,10 +29,13 @@ import System.IO ( stderr, hPutStrLn )
2829import qualified TOML
2930
3031-- directory
31- import System.Directory ( canonicalizePath , doesDirectoryExist , doesFileExist , doesPathExist , listDirectory , withCurrentDirectory )
32+ import System.Directory ( doesFileExist )
3233
3334-- filepath
34- import System.FilePath ( isExtensionOf )
35+ import System.FilePath ( isExtSeparator )
36+
37+ -- glob
38+ import qualified System.FilePath.Glob as Glob
3539
3640-- ghc
3741import GHC.Iface.Ext.Binary ( HieFileResult ( HieFileResult , hie_file_result ), readHieFileWithVersion )
@@ -234,17 +238,20 @@ mainWithConfig hieExt hieDirectories requireHsFiles weederConfig = handleWeederE
234238-- Will rethrow exceptions as 'ExceptionInLinkedThread' to the calling thread.
235239getHieFiles :: String -> [FilePath ] -> Bool -> IO [HieFile ]
236240getHieFiles hieExt hieDirectories requireHsFiles = do
237- hieFilePaths <-
241+ let hiePat = " **/*." <> hieExtNoSep
242+ hieExtNoSep = if isExtSeparator (head hieExt) then tail hieExt else hieExt
243+
244+ hieFilePaths :: [FilePath ] <-
238245 concat <$>
239- traverse ( getFilesIn hieExt )
246+ traverse ( getFilesIn hiePat )
240247 ( if null hieDirectories
241248 then [" ./." ]
242249 else hieDirectories
243250 )
244251
245- hsFilePaths <-
252+ hsFilePaths :: [ FilePath ] <-
246253 if requireHsFiles
247- then getFilesIn " .hs" " ./."
254+ then getFilesIn " **/* .hs" " ./."
248255 else pure []
249256
250257 hieFileResultsChan <- newChan
@@ -274,43 +281,14 @@ getHieFiles hieExt hieDirectories requireHsFiles = do
274281-- | Recursively search for files with the given extension in given directory
275282getFilesIn
276283 :: String
277- -- ^ Only files with this extension are considered
284+ -- ^ Only files matching this pattern are considered.
278285 -> FilePath
279286 -- ^ Directory to look in
280287 -> IO [FilePath ]
281- getFilesIn ext path = do
282- exists <-
283- doesPathExist path
284-
285- if exists
286- then do
287- isFile <-
288- doesFileExist path
289-
290- if isFile && ext `isExtensionOf` path
291- then do
292- path' <-
293- canonicalizePath path
294-
295- return [ path' ]
296-
297- else do
298- isDir <-
299- doesDirectoryExist path
300-
301- if isDir
302- then do
303- cnts <-
304- listDirectory path
305-
306- withCurrentDirectory path ( foldMap ( getFilesIn ext ) cnts )
307-
308- else
309- return []
310-
311- else
312- return []
313-
288+ getFilesIn pat root = do
289+ [result] <- Glob. globDir [Glob. compile pat] root
290+ pure result
291+
314292
315293-- | Read a .hie file, exiting if it's an incompatible version.
316294readCompatibleHieFileOrExit :: NameCache -> FilePath -> IO HieFile
0 commit comments