From 4b705d1795b9c6c5b8878a38c70c086818a12a77 Mon Sep 17 00:00:00 2001 From: Gautier DI FOLCO Date: Sat, 4 Oct 2025 18:40:39 +0200 Subject: [PATCH 1/2] Rely on gitignore to exclude listed files in ghcide (#4665) --- ghcide/ghcide.cabal | 1 + ghcide/src/Development/IDE/Main.hs | 29 ++++++++++++++++++++++------- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/ghcide/ghcide.cabal b/ghcide/ghcide.cabal index b217012bec..b3a1fc495f 100644 --- a/ghcide/ghcide.cabal +++ b/ghcide/ghcide.cabal @@ -89,6 +89,7 @@ library , optparse-applicative , os-string , parallel + , process , prettyprinter >=1.7 , prettyprinter-ansi-terminal , random diff --git a/ghcide/src/Development/IDE/Main.hs b/ghcide/src/Development/IDE/Main.hs index ad4a36327a..2ed1ad42b2 100644 --- a/ghcide/src/Development/IDE/Main.hs +++ b/ghcide/src/Development/IDE/Main.hs @@ -114,16 +114,17 @@ import qualified Language.LSP.Server as LSP 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) @@ -446,15 +447,29 @@ defaultMain recorder Arguments{..} = withHeapStats (cmapWithPrio LogHeapStats re c ide 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 + (testGitExitCode, _, _) <- readProcessWithExitCode "git" ["status"] "" + let findFiles = + case testGitExitCode of + ExitSuccess -> \path -> do + let lookups = [path, path "*.hs", path "*.lhs"] + (trackedExitCode, trackedStdout, _) <- readProcessWithExitCode "git" ("ls-files":lookups) "" + (untrackedExitCode, untrackedStdout, _) <- readProcessWithExitCode "git" ("ls-files":"-o":lookups) "" + if trackedExitCode == ExitSuccess && untrackedExitCode == ExitSuccess + then pure $ lines trackedStdout <> lines untrackedStdout + else haskellFind path + _ -> haskellFind + flip concatMapM paths $ \x -> do 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 From 7957387ee3eca5a0b0eb64b4a925aa2c18231b5e Mon Sep 17 00:00:00 2001 From: Gautier DI FOLCO Date: Tue, 28 Oct 2025 21:56:52 +0100 Subject: [PATCH 2/2] doc: document expandFiles --- ghcide/src/Development/IDE/Main.hs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ghcide/src/Development/IDE/Main.hs b/ghcide/src/Development/IDE/Main.hs index 2ed1ad42b2..6e93e4db60 100644 --- a/ghcide/src/Development/IDE/Main.hs +++ b/ghcide/src/Development/IDE/Main.hs @@ -446,6 +446,9 @@ defaultMain recorder Arguments{..} = withHeapStats (cmapWithPrio LogHeapStats re 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 paths = do let haskellFind x =