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
51 changes: 42 additions & 9 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 All @@ -141,6 +143,7 @@
| LogSession Session.Log
| LogPluginHLS PluginHLS.Log
| LogRules Rules.Log
| LogUsingGit
deriving Show

instance Pretty Log where
Expand All @@ -164,6 +167,7 @@
LogSession msg -> pretty msg
LogPluginHLS msg -> pretty msg
LogRules msg -> pretty msg
LogUsingGit -> "Using git to list file, relying on .gitignore"

data Command
= Check [FilePath] -- ^ Typecheck some paths and print diagnostics. Exit code is the number of failures
Expand Down Expand Up @@ -383,7 +387,7 @@
putStrLn "Report bugs at https://github.com/haskell/haskell-language-server/issues"

putStrLn $ "\nStep 1/4: Finding files to test in " ++ dir
files <- expandFiles (argFiles ++ ["." | null argFiles])
files <- expandFiles recorder (argFiles ++ ["." | null argFiles])
-- LSP works with absolute file paths, so try and behave similarly
absoluteFiles <- nubOrd <$> mapM IO.canonicalizePath files
putStrLn $ "Found " ++ show (length absoluteFiles) ++ " files"
Expand Down Expand Up @@ -445,16 +449,45 @@
registerIdeConfiguration (shakeExtras ide) $ IdeConfiguration mempty (hashed Nothing)
c ide

expandFiles :: [FilePath] -> IO [FilePath]
expandFiles = concatMapM $ \x -> do
-- | List the haskell files given some paths
--
-- It will rely on git if possible to filter-out ignored files.
expandFiles :: Recorder (WithPriority Log) -> [FilePath] -> IO [FilePath]
expandFiles recorder 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"]
when (isJust mHasGit) $ logWith recorder Info LogUsingGit
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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here we are!

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 485 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