Skip to content

Commit 3edac6f

Browse files
authored
Merge branch 'master' into 1875-tests-randomly-fail-with-exception-fd111-hputbuf-resource-vanished-broken-pipe---test-option-j1-workaround
2 parents 9b7b400 + 7d02159 commit 3edac6f

2 files changed

Lines changed: 41 additions & 14 deletions

File tree

ghcide/session-loader/Development/IDE/Session/Diagnostics.hs

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,22 @@ import System.FilePath
1818

1919
data CradleErrorDetails =
2020
CradleErrorDetails
21-
{ cabalProjectFiles :: [FilePath]
21+
{ cradleDependencies :: [FilePath]
2222
-- ^ files related to the cradle error
2323
-- i.e. .cabal, cabal.project, etc.
24+
, structuredCradleError :: Maybe StructuredCradleError
25+
-- ^ structured information about the cradle error
26+
-- i.e. unknownModules error
27+
} deriving (Show, Eq, Ord, Read, Generic, Aeson.ToJSON, Aeson.FromJSON)
28+
29+
data StructuredCradleError
30+
= UnknownModuleError UnknownModuleDetails
31+
deriving (Show, Eq, Ord, Read, Generic, Aeson.ToJSON, Aeson.FromJSON)
32+
33+
data UnknownModuleDetails =
34+
UnknownModuleDetails
35+
{ moduleFilePath :: FilePath
36+
, suggestedModuleName :: String
2437
} deriving (Show, Eq, Ord, Read, Generic, Aeson.ToJSON, Aeson.FromJSON)
2538

2639
{- | Takes a cradle error, the corresponding cradle and the file path where
@@ -33,21 +46,31 @@ renderCradleError cradleError cradle nfp =
3346
ideErrorWithSource (Just "cradle") (Just DiagnosticSeverity_Error) nfp (T.unlines $ map T.pack userFriendlyMessage) Nothing
3447
in
3548
if HieBios.isCabalCradle cradle
36-
then noDetails & fdLspDiagnosticL %~ \diag -> diag{_data_ = Just $ Aeson.toJSON CradleErrorDetails{cabalProjectFiles=absDeps}}
49+
then noDetails & fdLspDiagnosticL %~ \diag -> diag
50+
{ _data_ = Just $ Aeson.toJSON CradleErrorDetails
51+
{ cradleDependencies = absDeps
52+
, structuredCradleError = fmap UnknownModuleError mkUnknownModuleDetails
53+
}
54+
}
3755
else noDetails
3856
where
3957
ms = cradleErrorStderr cradleError
4058

4159
absDeps = fmap (cradleRootDir cradle </>) (cradleErrorDependencies cradleError)
4260
userFriendlyMessage :: [String]
4361
userFriendlyMessage
44-
| HieBios.isCabalCradle cradle = fromMaybe ms $ fileMissingMessage <|> mkUnknownModuleMessage
62+
| HieBios.isCabalCradle cradle = fromMaybe ms $ fileMissingMessage <|> (unknownModuleMessage (fromNormalizedFilePath nfp) <$ mkUnknownModuleDetails)
4563
| otherwise = ms
4664

47-
mkUnknownModuleMessage :: Maybe [String]
48-
mkUnknownModuleMessage
65+
-- Produce structured details when unknown module error is detected
66+
mkUnknownModuleDetails :: Maybe UnknownModuleDetails
67+
mkUnknownModuleDetails
4968
| any (isInfixOf "Failed extracting script block:") ms =
50-
Just $ unknownModuleMessage (fromNormalizedFilePath nfp)
69+
let fp = fromNormalizedFilePath nfp
70+
in Just UnknownModuleDetails
71+
{ moduleFilePath = fp
72+
, suggestedModuleName = dropExtension (takeFileName fp)
73+
}
5174
| otherwise = Nothing
5275

5376
fileMissingMessage :: Maybe [String]

plugins/hls-cabal-plugin/src/Ide/Plugin/Cabal/CabalAdd/CodeAction.hs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,15 @@ import Ide.Types (CommandId (Comma
3636
PluginId)
3737

3838
import Control.Lens ((^.))
39+
import qualified Data.Aeson as Aeson
40+
import Development.IDE.Session.Diagnostics
3941
import qualified Language.LSP.Protocol.Lens as JL
4042
import Language.LSP.Protocol.Types (CodeActionKind (..),
4143
VersionedTextDocumentIdentifier)
4244
import qualified Language.LSP.Protocol.Types as J
4345
import System.FilePath
4446
import Text.PrettyPrint (render)
4547
import Text.Regex.TDFA
46-
4748
--------------------------------------------
4849
-- Add module to cabal file
4950
--------------------------------------------
@@ -148,13 +149,16 @@ mkRelativeModulePathM hsSourceDirs cabalSrcPath' haskellFilePath =
148149
cabalSrcPath = takeDirectory cabalSrcPath'
149150

150151
isUnknownModuleDiagnostic :: J.Diagnostic -> Bool
151-
isUnknownModuleDiagnostic diag = (msg =~ regex)
152-
where
153-
msg :: T.Text
154-
msg = diag ^. JL.message
155-
regex :: T.Text
156-
regex = "Loading the module [\8216'][^\8217']*[\8217'] failed."
157-
152+
isUnknownModuleDiagnostic diag =
153+
case diag ^. JL.data_ of
154+
Nothing -> False
155+
Just v ->
156+
case Aeson.fromJSON v of
157+
Aeson.Success details ->
158+
case structuredCradleError details of
159+
Just (UnknownModuleError _) -> True
160+
_ -> False
161+
_ -> False
158162
--------------------------
159163
-- Below are several utility functions which create a StanzaItem for each of the possible Stanzas,
160164
-- these all have specific constructors we need to match, so we can't generalise this process well.

0 commit comments

Comments
 (0)