2 -- For some reason ghc warns about not using
3 -- Control.Monad.IO.Class but it's needed for
5 {-# OPTIONS_GHC -Wunused-imports #-}
6 module Language.Haskell.LSP.Test.Compat where
11 #if MIN_VERSION_process(1,6,3)
12 -- We have to hide cleanupProcess for process-1.6.3.0
13 -- cause it is in the public api for 1.6.3.0 versions
14 -- shipped with ghc >= 8.6 and < 8.6.4
15 import System.Process hiding (getPid, cleanupProcess, withCreateProcess)
16 # if MIN_VERSION_process(1,6,4)
17 import qualified System.Process (getPid, cleanupProcess, withCreateProcess)
19 import Foreign.C.Error
20 import GHC.IO.Exception ( IOErrorType(..), IOException(..) )
22 import qualified System.Process (getPid)
23 import qualified Control.Exception as C
26 import Control.Concurrent.MVar
27 import Foreign.C.Error
28 import GHC.IO.Exception ( IOErrorType(..), IOException(..) )
29 import System.Process hiding (withCreateProcess)
30 import System.Process.Internals
32 import qualified Control.Exception as C
35 #ifdef mingw32_HOST_OS
36 import qualified System.Win32.Process
38 import qualified System.Posix.Process
42 getCurrentProcessID :: IO Int
43 #ifdef mingw32_HOST_OS
44 getCurrentProcessID = fromIntegral <$> System.Win32.Process.getCurrentProcessId
46 getCurrentProcessID = fromIntegral <$> System.Posix.Process.getProcessID
49 getProcessID :: ProcessHandle -> IO Int
50 getProcessID p = fromIntegral . fromJust <$> getProcessID' p
52 #if MIN_VERSION_process(1,6,3)
53 getProcessID' = System.Process.getPid
55 #if MIN_VERSION_process(1,6,0)
56 getProcessID' (ProcessHandle mh _ _) = do
58 getProcessID' (ProcessHandle mh _) = do
62 #ifdef mingw32_HOST_OS
64 pid <- System.Win32.Process.getProcessId h
67 OpenHandle pid -> return $ Just pid
73 :: (Maybe Handle, Maybe Handle, Maybe Handle, ProcessHandle) -> IO ()
77 -> (Maybe Handle -> Maybe Handle -> Maybe Handle -> ProcessHandle -> IO a)
80 #if MIN_VERSION_process(1,6,4)
82 cleanupProcess = System.Process.cleanupProcess
84 withCreateProcess = System.Process.withCreateProcess
88 cleanupProcess (mb_stdin, mb_stdout, mb_stderr, ph) = do
89 -- We ignore the spurious "permission denied" error in windows:
90 -- see https://github.com/haskell/process/issues/110
91 ignorePermDenied $ terminateProcess ph
92 -- Note, it's important that other threads that might be reading/writing
93 -- these handles also get killed off, since otherwise they might be holding
94 -- the handle lock and prevent us from closing, leading to deadlock.
95 maybe (return ()) (ignoreSigPipe . hClose) mb_stdin
96 maybe (return ()) hClose mb_stdout
97 maybe (return ()) hClose mb_stderr
100 where ignoreSigPipe = ignoreIOError ResourceVanished ePIPE
101 ignorePermDenied = ignoreIOError PermissionDenied eACCES
103 ignoreIOError :: IOErrorType -> Errno -> IO () -> IO ()
104 ignoreIOError ioErrorType errno =
105 C.handle $ \e -> case e of
106 IOError { ioe_type = iot
107 , ioe_errno = Just ioe }
108 | iot == ioErrorType && Errno ioe == errno -> return ()
111 withCreateProcess c action =
112 C.bracket (createProcess c) cleanupProcess
113 (\(m_in, m_out, m_err, ph) -> action m_in m_out m_err ph)