@@ -11,12 +11,13 @@ import Control.Concurrent.Extra (newLock, readVar,
11
11
withLock ,
12
12
withNumCapabilities )
13
13
import Control.Exception.Safe (Exception (displayException ),
14
- catchAny )
15
- import Control.Monad.Extra (concatMapM , unless ,
16
- when )
14
+ SomeException (SomeException ),
15
+ catch , catchAny )
16
+ import Control.Monad.Extra (concatMapM , join ,
17
+ unless , when )
17
18
import qualified Data.Aeson.Encode.Pretty as A
18
19
import Data.Default (Default (def ))
19
- import Data.Foldable (for_ , traverse_ )
20
+ import Data.Foldable (traverse_ )
20
21
import qualified Data.HashMap.Strict as HashMap
21
22
import Data.Hashable (hashed )
22
23
import Data.List.Extra (intercalate , isPrefixOf ,
@@ -255,8 +256,6 @@ defaultMain Arguments{..} = do
255
256
PrintDefaultConfig ->
256
257
LT. putStrLn $ decodeUtf8 $ A. encodePretty $ pluginsToDefaultConfig argsHlsPlugins
257
258
LSP -> withNumCapabilities (maybe (numProcessors `div` 2 ) fromIntegral argsThreads) $ do
258
- server <- for argsMonitoringPort $ \ p ->
259
- Monitoring. forkServer " localhost" (fromIntegral p)
260
259
t <- offsetTime
261
260
logInfo logger " Starting LSP server..."
262
261
logInfo logger " If you are seeing this in a terminal, you probably should have run WITHOUT the --lsp option!"
@@ -293,6 +292,15 @@ defaultMain Arguments{..} = do
293
292
hPutStrLn stderr $
294
293
" Currently, HLS supports GHC 9 only partially. "
295
294
<> " See [issue #297](https://github.com/haskell/haskell-language-server/issues/297) for more detail."
295
+
296
+ -- this can fail if the port is busy
297
+ server <- fmap join $ for argsMonitoringPort $ \ p ->
298
+ (Just <$> Monitoring. forkServer " localhost" (fromIntegral p))
299
+ `catch` \ e@ SomeException {} -> do
300
+ logInfo logger $ T. pack $
301
+ " Unable to bind monitoring server on port " <> show p <> " :" <> show e
302
+ return Nothing
303
+
296
304
initialise
297
305
argsDefaultHlsConfig
298
306
rules
0 commit comments