Skip to content
Open
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
1 change: 1 addition & 0 deletions ghcide/ghcide.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ library
, optparse-applicative
, os-string
, parallel
, process
, prettyprinter >=1.7
, prettyprinter-ansi-terminal
, random
Expand Down
43 changes: 36 additions & 7 deletions ghcide/src/Development/IDE/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import Control.Concurrent.MVar (MVar, newEmptyMVar,
putMVar, tryReadMVar)
import Control.Concurrent.STM.Stats (dumpSTMStats)
import Control.Exception.Safe as Safe
import Control.Monad.Extra (concatMapM, unless,
when)
import Control.Monad.IO.Class (liftIO)
Expand Down Expand Up @@ -114,16 +115,17 @@
import Numeric.Natural (Natural)
import Options.Applicative hiding (action)
import qualified System.Directory.Extra as IO
import System.Exit (ExitCode (ExitFailure),
import System.Exit (ExitCode (ExitFailure, ExitSuccess),
exitWith)
import System.FilePath (takeExtension,
takeFileName)
takeFileName, (</>))
import System.IO (BufferMode (LineBuffering, NoBuffering),
Handle, hFlush,
hPutStrLn,
hSetBuffering,
hSetEncoding, stderr,
stdin, stdout, utf8)
import System.Process (readProcessWithExitCode)
import System.Random (newStdGen)
import System.Time.Extra (Seconds, offsetTime,
showDuration)
Expand Down Expand Up @@ -445,16 +447,43 @@
registerIdeConfiguration (shakeExtras ide) $ IdeConfiguration mempty (hashed Nothing)
c ide

-- | List the haskell files given some paths
--
-- It will rely on git if possible to filter-out ignored files.
expandFiles :: [FilePath] -> IO [FilePath]
expandFiles = concatMapM $ \x -> do
expandFiles paths = do
let haskellFind x =
let recurse "." = True
recurse y | "." `isPrefixOf` takeFileName y = False -- skip .git etc
recurse y = takeFileName y `notElem` ["dist", "dist-newstyle"] -- cabal directories
in filter (\y -> takeExtension y `elem` [".hs", ".lhs"]) <$> IO.listFilesInside (return . recurse) x
git args = do
mResult <- (Just <$> readProcessWithExitCode "git" args "") `Safe.catchAny`const (pure Nothing)
pure $
case mResult of
Just (ExitSuccess, gitStdout, _) -> Just gitStdout
_ -> Nothing
mHasGit <- git ["status"]
let findFiles =
case mHasGit of
Just _ -> \path -> do
let lookups =
if takeExtension path `elem` [".hs", ".lhs"]
then [path]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question, what should happen if a user explicitly asks for a file to load, but the file is .gitignored?

Should we perhaps still load it? I feel like, that might be more natural behaviour, but I am admittedly not sure.
If we do want to ignore ignored files, then I would still suggest that we should warn the user in this case, that we ingored a file they requested.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could add an option in the CLI to by-pass gitignore, but I guess it'll be more complex.

Any idea which form the warning would take?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The warning should probably just be printed via the Recorder, so nothing fancy.

else [path </> "*.hs", path </> "*.lhs"]
gitLines args = fmap lines <$> git args
mTracked <- gitLines ("ls-files":lookups)
mUntracked <- gitLines ("ls-files":"-o":lookups)
case mTracked <> mUntracked of
Nothing -> haskellFind path
Just files -> pure files
_ -> haskellFind
flip concatMapM paths $ \x -> do

Check warning on line 481 in ghcide/src/Development/IDE/Main.hs

View workflow job for this annotation

GitHub Actions / Hlint check run

Warning in expandFiles in module Development.IDE.Main: Use concatForM ▫︎ Found: "flip concatMapM" ▫︎ Perhaps: "concatForM"
b <- IO.doesFileExist x
if b
then return [x]
else do
let recurse "." = True
recurse y | "." `isPrefixOf` takeFileName y = False -- skip .git etc
recurse y = takeFileName y `notElem` ["dist", "dist-newstyle"] -- cabal directories
files <- filter (\y -> takeExtension y `elem` [".hs", ".lhs"]) <$> IO.listFilesInside (return . recurse) x
files <- findFiles x
when (null files) $
fail $ "Couldn't find any .hs/.lhs files inside directory: " ++ x
return files
Loading