2019-08-13 19:23:03 +03:00
|
|
|
-- Copyright (c) 2019 The DAML Authors. All rights reserved.
|
2019-05-20 18:36:08 +03:00
|
|
|
-- SPDX-License-Identifier: Apache-2.0
|
2019-09-07 17:23:14 +03:00
|
|
|
{-# OPTIONS_GHC -Wno-dodgy-imports #-} -- GHC no longer exports def in GHC 8.6 and above
|
2019-09-23 09:50:28 +03:00
|
|
|
{-# LANGUAGE CPP #-} -- To get precise GHC version
|
2019-05-20 18:36:08 +03:00
|
|
|
|
2019-06-26 19:07:08 +03:00
|
|
|
module Main(main) where
|
2019-05-20 18:36:08 +03:00
|
|
|
|
2019-06-26 19:07:08 +03:00
|
|
|
import Arguments
|
2019-06-15 11:29:40 +03:00
|
|
|
import Data.Maybe
|
2019-06-26 19:07:08 +03:00
|
|
|
import Data.List.Extra
|
|
|
|
import System.FilePath
|
2019-05-20 18:36:08 +03:00
|
|
|
import Control.Concurrent.Extra
|
2019-09-20 18:33:37 +03:00
|
|
|
import Control.Exception
|
2019-06-26 19:07:08 +03:00
|
|
|
import Control.Monad.Extra
|
2019-06-26 11:04:10 +03:00
|
|
|
import Data.Default
|
2019-05-20 18:36:08 +03:00
|
|
|
import System.Time.Extra
|
2019-06-22 11:21:59 +03:00
|
|
|
import Development.IDE.Core.FileStore
|
2019-06-25 12:09:42 +03:00
|
|
|
import Development.IDE.Core.OfInterest
|
2019-06-22 11:21:59 +03:00
|
|
|
import Development.IDE.Core.Service
|
|
|
|
import Development.IDE.Core.Rules
|
|
|
|
import Development.IDE.Core.Shake
|
|
|
|
import Development.IDE.Core.RuleTypes
|
2019-06-22 00:19:07 +03:00
|
|
|
import Development.IDE.LSP.Protocol
|
|
|
|
import Development.IDE.Types.Location
|
2019-05-20 18:36:08 +03:00
|
|
|
import Development.IDE.Types.Diagnostics
|
|
|
|
import Development.IDE.Types.Options
|
2019-06-22 11:21:59 +03:00
|
|
|
import Development.IDE.Types.Logger
|
2019-09-11 11:13:18 +03:00
|
|
|
import Development.IDE.GHC.Util
|
2019-06-28 14:47:45 +03:00
|
|
|
import qualified Data.Text as T
|
2019-05-20 18:36:08 +03:00
|
|
|
import qualified Data.Text.IO as T
|
2019-06-11 17:03:44 +03:00
|
|
|
import Language.Haskell.LSP.Messages
|
2019-10-17 12:11:52 +03:00
|
|
|
import Language.Haskell.LSP.Types (LspId(IdInt))
|
2019-08-15 16:27:26 +03:00
|
|
|
import Linker
|
2019-09-07 17:23:14 +03:00
|
|
|
import Data.Version
|
2019-06-14 12:02:04 +03:00
|
|
|
import Development.IDE.LSP.LanguageServer
|
2019-06-26 19:07:08 +03:00
|
|
|
import System.Directory.Extra as IO
|
2019-05-20 18:36:08 +03:00
|
|
|
import System.Environment
|
2019-06-15 11:29:40 +03:00
|
|
|
import System.IO
|
2019-09-23 09:50:28 +03:00
|
|
|
import System.Exit
|
|
|
|
import Paths_ghcide
|
2019-05-20 18:36:08 +03:00
|
|
|
import Development.Shake hiding (Env)
|
|
|
|
import qualified Data.Set as Set
|
2019-09-25 14:01:41 +03:00
|
|
|
import qualified Data.Map.Strict as Map
|
2019-05-20 18:36:08 +03:00
|
|
|
|
2019-09-07 17:23:14 +03:00
|
|
|
import GHC hiding (def)
|
2019-06-14 17:11:32 +03:00
|
|
|
import qualified GHC.Paths
|
2019-05-20 18:36:08 +03:00
|
|
|
|
2019-06-15 11:29:40 +03:00
|
|
|
import HIE.Bios
|
|
|
|
|
2019-06-14 17:11:32 +03:00
|
|
|
-- Set the GHC libdir to the nix libdir if it's present.
|
|
|
|
getLibdir :: IO FilePath
|
|
|
|
getLibdir = fromMaybe GHC.Paths.libdir <$> lookupEnv "NIX_GHC_LIBDIR"
|
2019-05-20 18:36:08 +03:00
|
|
|
|
2019-09-23 09:50:28 +03:00
|
|
|
ghcideVersion :: String
|
|
|
|
ghcideVersion = "ghcide version: " <> showVersion version
|
|
|
|
<> " (GHC: " <> VERSION_ghc <> ")"
|
|
|
|
|
2019-05-20 18:36:08 +03:00
|
|
|
main :: IO ()
|
|
|
|
main = do
|
2019-06-14 17:23:22 +03:00
|
|
|
-- WARNING: If you write to stdout before runLanguageServer
|
|
|
|
-- then the language server will not work
|
2019-06-26 19:07:08 +03:00
|
|
|
Arguments{..} <- getArguments
|
|
|
|
|
2019-09-23 09:50:28 +03:00
|
|
|
if argsVersion then putStrLn ghcideVersion >> exitSuccess
|
|
|
|
else hPutStrLn stderr {- see WARNING above -} ghcideVersion
|
|
|
|
|
2019-05-20 18:36:08 +03:00
|
|
|
-- lock to avoid overlapping output on stdout
|
|
|
|
lock <- newLock
|
2019-09-25 14:01:41 +03:00
|
|
|
let logger p = Logger $ \pri msg -> when (pri >= p) $ withLock lock $
|
2019-06-28 14:47:45 +03:00
|
|
|
T.putStrLn $ T.pack ("[" ++ upper (show pri) ++ "] ") <> msg
|
2019-05-20 18:36:08 +03:00
|
|
|
|
2019-06-26 19:07:08 +03:00
|
|
|
whenJust argsCwd setCurrentDirectory
|
|
|
|
|
2019-06-15 11:29:40 +03:00
|
|
|
dir <- getCurrentDirectory
|
|
|
|
|
2019-06-26 19:07:08 +03:00
|
|
|
if argLSP then do
|
|
|
|
t <- offsetTime
|
|
|
|
hPutStrLn stderr "Starting LSP server..."
|
2019-10-08 20:43:32 +03:00
|
|
|
hPutStrLn stderr "If you are seeing this in a terminal, you probably should have run ghcidie WITHOUT the --lsp option!"
|
2019-10-17 12:11:52 +03:00
|
|
|
runLanguageServer def def $ \getLspId event vfs caps -> do
|
2019-06-26 19:07:08 +03:00
|
|
|
t <- t
|
|
|
|
hPutStrLn stderr $ "Started LSP server in " ++ showDuration t
|
2019-09-25 14:01:41 +03:00
|
|
|
-- very important we only call loadSession once, and it's fast, so just do it before starting
|
|
|
|
session <- loadSession dir
|
|
|
|
let options = (defaultIdeOptions $ return session)
|
2019-08-13 21:00:21 +03:00
|
|
|
{ optReportProgress = clientSupportsProgress caps }
|
2019-10-17 12:11:52 +03:00
|
|
|
initialise (mainRule >> action kick) getLspId event (logger minBound) options vfs
|
2019-06-13 19:21:27 +03:00
|
|
|
else do
|
2019-09-25 14:01:41 +03:00
|
|
|
putStrLn $ "Ghcide setup tester in " ++ dir ++ "."
|
|
|
|
putStrLn "Report bugs at https://github.com/digital-asset/ghcide/issues"
|
2019-06-26 19:07:08 +03:00
|
|
|
|
2019-09-25 14:01:41 +03:00
|
|
|
putStrLn $ "\nStep 1/6: Finding files to test in " ++ dir
|
2019-06-26 19:07:08 +03:00
|
|
|
files <- nubOrd <$> expandFiles (argFiles ++ ["." | null argFiles])
|
|
|
|
putStrLn $ "Found " ++ show (length files) ++ " files"
|
|
|
|
|
2019-09-25 14:01:41 +03:00
|
|
|
putStrLn "\nStep 2/6: Looking for hie.yaml files that control setup"
|
|
|
|
cradles <- mapM findCradle files
|
|
|
|
let ucradles = nubOrd cradles
|
|
|
|
let n = length ucradles
|
|
|
|
putStrLn $ "Found " ++ show n ++ " cradle" ++ ['s' | n /= 1]
|
|
|
|
sessions <- forM (zipFrom (1 :: Int) ucradles) $ \(i, x) -> do
|
|
|
|
let msg = maybe ("Implicit cradle for " ++ dir) ("Loading " ++) x
|
|
|
|
putStrLn $ "\nStep 3/6, Cradle " ++ show i ++ "/" ++ show n ++ ": " ++ msg
|
|
|
|
cradle <- maybe (loadImplicitCradle $ addTrailingPathSeparator dir) loadCradle x
|
|
|
|
when (isNothing x) $ print cradle
|
|
|
|
putStrLn $ "\nStep 4/6, Cradle " ++ show i ++ "/" ++ show n ++ ": Loading GHC Session"
|
|
|
|
cradleToSession cradle
|
|
|
|
|
|
|
|
putStrLn "\nStep 5/6: Initializing the IDE"
|
|
|
|
vfs <- makeVFSHandle
|
|
|
|
let cradlesToSessions = Map.fromList $ zip ucradles sessions
|
|
|
|
let filesToCradles = Map.fromList $ zip files cradles
|
|
|
|
let grab file = fromMaybe (head sessions) $ do
|
|
|
|
cradle <- Map.lookup file filesToCradles
|
|
|
|
Map.lookup cradle cradlesToSessions
|
2019-10-17 12:11:52 +03:00
|
|
|
ide <- initialise mainRule (pure $ IdInt 0) (showEvent lock) (logger Info) (defaultIdeOptions $ return $ return . grab) vfs
|
2019-09-25 14:01:41 +03:00
|
|
|
|
|
|
|
putStrLn "\nStep 6/6: Type checking the files"
|
2019-06-26 19:07:08 +03:00
|
|
|
setFilesOfInterest ide $ Set.fromList $ map toNormalizedFilePath files
|
|
|
|
results <- runActionSync ide $ uses TypeCheck $ map toNormalizedFilePath files
|
|
|
|
let (worked, failed) = partition fst $ zip (map isJust results) files
|
2019-09-27 16:18:24 +03:00
|
|
|
when (failed /= []) $
|
|
|
|
putStr $ unlines $ "Files that failed:" : map ((++) " * " . snd) failed
|
2019-06-26 19:07:08 +03:00
|
|
|
|
2019-09-27 16:18:24 +03:00
|
|
|
let files xs = let n = length xs in if n == 1 then "1 file" else show n ++ " files"
|
|
|
|
putStrLn $ "\nCompleted (" ++ files worked ++ " worked, " ++ files failed ++ " failed)"
|
2019-06-13 19:21:27 +03:00
|
|
|
|
|
|
|
|
2019-06-26 19:07:08 +03:00
|
|
|
expandFiles :: [FilePath] -> IO [FilePath]
|
|
|
|
expandFiles = concatMapM $ \x -> do
|
|
|
|
b <- IO.doesFileExist x
|
|
|
|
if b then return [x] else do
|
|
|
|
let recurse "." = True
|
|
|
|
recurse x | "." `isPrefixOf` takeFileName x = False -- skip .git etc
|
|
|
|
recurse x = takeFileName x `notElem` ["dist","dist-newstyle"] -- cabal directories
|
|
|
|
files <- filter (\x -> takeExtension x `elem` [".hs",".lhs"]) <$> listFilesInside (return . recurse) x
|
|
|
|
when (null files) $
|
|
|
|
fail $ "Couldn't find any .hs/.lhs files inside directory: " ++ x
|
|
|
|
return files
|
|
|
|
|
|
|
|
|
2019-06-14 17:23:22 +03:00
|
|
|
kick :: Action ()
|
|
|
|
kick = do
|
2019-06-24 14:46:51 +03:00
|
|
|
files <- getFilesOfInterest
|
2019-06-14 17:23:22 +03:00
|
|
|
void $ uses TypeCheck $ Set.toList files
|
|
|
|
|
2019-05-20 18:36:08 +03:00
|
|
|
-- | Print an LSP event.
|
2019-06-11 17:03:44 +03:00
|
|
|
showEvent :: Lock -> FromServerMessage -> IO ()
|
|
|
|
showEvent _ (EventFileDiagnostics _ []) = return ()
|
2019-06-13 16:11:47 +03:00
|
|
|
showEvent lock (EventFileDiagnostics (toNormalizedFilePath -> file) diags) =
|
2019-05-20 18:36:08 +03:00
|
|
|
withLock lock $ T.putStrLn $ showDiagnosticsColored $ map (file,) diags
|
|
|
|
showEvent lock e = withLock lock $ print e
|
|
|
|
|
2019-09-25 14:01:41 +03:00
|
|
|
|
|
|
|
cradleToSession :: Cradle -> IO HscEnvEq
|
|
|
|
cradleToSession cradle = do
|
2019-09-20 18:33:37 +03:00
|
|
|
opts <- either throwIO return =<< getCompilerOptions "" cradle
|
|
|
|
libdir <- getLibdir
|
2019-08-15 16:27:26 +03:00
|
|
|
env <- runGhc (Just libdir) $ do
|
2019-09-20 18:33:37 +03:00
|
|
|
_targets <- initSession opts
|
2019-08-15 16:27:26 +03:00
|
|
|
getSession
|
|
|
|
initDynLinker env
|
2019-09-11 11:13:18 +03:00
|
|
|
newHscEnvEq env
|
|
|
|
|
2019-09-25 14:01:41 +03:00
|
|
|
|
|
|
|
loadSession :: FilePath -> IO (FilePath -> Action HscEnvEq)
|
|
|
|
loadSession dir = do
|
|
|
|
cradleLoc <- memoIO $ \v -> do
|
|
|
|
res <- findCradle v
|
|
|
|
-- Sometimes we get C: and sometimes we get c:, try and normalise that
|
|
|
|
-- e.g. see https://github.com/digital-asset/ghcide/issues/126
|
|
|
|
return $ normalise <$> res
|
|
|
|
session <- memoIO $ \file -> do
|
|
|
|
c <- maybe (loadImplicitCradle $ addTrailingPathSeparator dir) loadCradle file
|
|
|
|
cradleToSession c
|
|
|
|
return $ \file -> liftIO $ session =<< cradleLoc file
|
|
|
|
|
|
|
|
|
|
|
|
-- | Memoize an IO function, with the characteristics:
|
|
|
|
--
|
|
|
|
-- * If multiple people ask for a result simultaneously, make sure you only compute it once.
|
|
|
|
--
|
|
|
|
-- * If there are exceptions, repeatedly reraise them.
|
|
|
|
--
|
|
|
|
-- * If the caller is aborted (async exception) finish computing it anyway.
|
|
|
|
memoIO :: Ord a => (a -> IO b) -> IO (a -> IO b)
|
|
|
|
memoIO op = do
|
|
|
|
ref <- newVar Map.empty
|
|
|
|
return $ \k -> join $ mask_ $ modifyVar ref $ \mp ->
|
|
|
|
case Map.lookup k mp of
|
|
|
|
Nothing -> do
|
|
|
|
res <- onceFork $ op k
|
|
|
|
return (Map.insert k res mp, res)
|
|
|
|
Just res -> return (mp, res)
|