Ghcide: Strip path/span output from the actual error message

Created on 7 Oct 2019  路  13Comments  路  Source: haskell/ghcide

ghcide already reports file and line/column offsets in the LSP metadata, so having them also be in the message is clutter. At the moment I'm using a patched version that just strips everything up to : warning: and : error:, but that's a bit of a hacky solution.

An easy (and reliable) first step to improve this situation might be to inject the -fhide-source-paths flag into the GHC configuration, which will at least stop reporting the full path and only report the file name.

I'll play around with flags and stripping some more to see if I can figure out a less hacky solution.

All 13 comments

Actually manually parsing the file prefix when -fhide-source-paths is enabled should be fairly easy, since it only reports the module's filename, because we can safely assume that name doesn't include a : (since GHC doesn't allow those in module names), which we can't assume for arbitrary full paths.

@merijn Would you mind sharing which file you patched to get that working? I've not looked at the code yet but I'd love to add that in the interim as a quick fix.

diff -r 61331d1d506d src/Development/IDE/GHC/Error.hs
--- a/src/Development/IDE/GHC/Error.hs  Mon Sep 30 07:54:57 2019 +0100
+++ b/src/Development/IDE/GHC/Error.hs  Thu Nov 07 16:52:53 2019 +0100
@@ -39,10 +39,14 @@
     { _range    = srcSpanToRange loc
     , _severity = Just sev
     , _source   = Just diagSource -- not shown in the IDE, but useful for ghcide developers
-    , _message  = msg
+    , _message  = T.strip . keepAfter ": warning:" . keepAfter ": error:" $ msg
     , _code     = Nothing
     , _relatedInformation = Nothing
     }
+  where
+    keepAfter s t
+        | T.isInfixOf s t = snd $ T.breakOnEnd s t
+        | otherwise = t

 -- | Produce a GHC-style error from a source span and a message.
 diagFromErrMsg :: T.Text -> DynFlags -> ErrMsg -> [FileDiagnostic]

is this going to be integrated?

Is there no way to remove this at the GHC level? I'd rather not monkey around in text messages unless we have to.

I already looked into that, there isn't (currently) a flag in GHC that does this.

CC @cocreature - what are your thoughts on reducing the error message down a bit in the above ways?

:wave: @cocreature

@ndmitchell

Is there no way to remove this at the GHC level? I'd rather not monkey around in text messages unless we have to.

This can be done as follows:

 src/Development/IDE/GHC/Error.hs | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/src/Development/IDE/GHC/Error.hs b/src/Development/IDE/GHC/Error.hs
index 8764546..d0ab540 100644
--- a/src/Development/IDE/GHC/Error.hs
+++ b/src/Development/IDE/GHC/Error.hs
@@ -54,7 +54,8 @@ diagFromText diagSource sev loc msg = (toNormalizedFilePath' $ srcSpanToFilename
 -- | Produce a GHC-style error from a source span and a message.
 diagFromErrMsg :: T.Text -> DynFlags -> ErrMsg -> [FileDiagnostic]
 diagFromErrMsg diagSource dflags e =
-    [ diagFromText diagSource sev (errMsgSpan e) $ T.pack $ Out.showSDoc dflags $ ErrUtils.pprLocErrMsg e
+    [ diagFromText diagSource sev (errMsgSpan e) $ T.pack $ Out.showSDoc dflags $
+      ErrUtils.formatErrDoc dflags $ ErrUtils.errMsgDoc e
     | Just sev <- [toDSeverity $ errMsgSeverity e]]

After the change, the error message looks as follows (the [typecheck] [E] is added by the editor's lsp plugin):

image

This looks pretty nice. Someone want to make a PR?

@ndmitchell don't mind if I do!

@merijn you can close this!

\o/

Was this page helpful?
0 / 5 - 0 ratings