883bfc9ef32e5db25a0eb22a22e204fa9cf3d512
[lsp-test.git] / src / Language / Haskell / LSP / Test / Compat.hs
1 {-# LANGUAGE CPP #-}
2 -- For some reason ghc warns about not using
3 -- Control.Monad.IO.Class but it's needed for
4 -- MonadIO
5 {-# OPTIONS_GHC -Wunused-imports #-}
6 module Language.Haskell.LSP.Test.Compat where
7
8 import Data.Maybe
9 import System.IO
10
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)
18 # else
19 import Foreign.C.Error
20 import GHC.IO.Exception ( IOErrorType(..), IOException(..) )
21
22 import qualified System.Process (getPid)
23 import qualified Control.Exception as C
24 # endif
25 #else
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
31
32 import qualified Control.Exception as C
33 #endif
34
35 #ifdef mingw32_HOST_OS
36 import qualified System.Win32.Process
37 #else
38 import qualified System.Posix.Process
39 #endif
40
41
42 getCurrentProcessID :: IO Int
43 #ifdef mingw32_HOST_OS
44 getCurrentProcessID = fromIntegral <$> System.Win32.Process.getCurrentProcessId
45 #else
46 getCurrentProcessID = fromIntegral <$> System.Posix.Process.getProcessID
47 #endif
48
49 getProcessID :: ProcessHandle -> IO Int
50 getProcessID p = fromIntegral . fromJust <$> getProcessID' p
51   where
52 #if MIN_VERSION_process(1,6,3)
53   getProcessID' = System.Process.getPid
54 #else
55 #if MIN_VERSION_process(1,6,0)
56   getProcessID' (ProcessHandle mh _ _) = do
57 #else
58   getProcessID' (ProcessHandle mh _) = do
59 #endif
60     p_ <- readMVar mh
61     case p_ of
62 #ifdef mingw32_HOST_OS
63       OpenHandle h -> do
64         pid <- System.Win32.Process.getProcessId h
65         return $ Just pid
66 #else
67       OpenHandle pid -> return $ Just pid
68 #endif
69       _ -> return Nothing
70 #endif
71
72 cleanupProcess
73   :: (Maybe Handle, Maybe Handle, Maybe Handle, ProcessHandle) -> IO ()
74
75 withCreateProcess
76   :: CreateProcess
77   -> (Maybe Handle -> Maybe Handle -> Maybe Handle -> ProcessHandle -> IO a)
78   -> IO a
79
80 #if MIN_VERSION_process(1,6,4)
81
82 cleanupProcess = System.Process.cleanupProcess
83
84 withCreateProcess = System.Process.withCreateProcess
85
86 #else
87
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
98
99     return ()
100   where ignoreSigPipe = ignoreIOError ResourceVanished ePIPE
101         ignorePermDenied = ignoreIOError PermissionDenied eACCES
102     
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 ()
109                      _ -> C.throwIO e
110
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)
114
115 #endif