Skip to content

Commit 8e3bf07

Browse files
committed
Don't show error context
1 parent 2ba147e commit 8e3bf07

5 files changed

Lines changed: 38 additions & 14 deletions

File tree

src/DeepSeek.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import Network.HTTP.Simple
2525
import Builder (Builder)
2626
import qualified Builder
2727
import Sensei.API (Instructions(..))
28-
import GHC.Diagnostic (Diagnostic, Span, Location)
28+
import GHC.Diagnostic (Diagnostic, Span, Location, ShowErrorContext(..))
2929
import qualified GHC.Diagnostic.Type as Diagnostic
3030
import qualified Config.DeepSeek as Config
3131
import DeepSeek.Types
@@ -146,7 +146,7 @@ createPrompt dir span instructions = do
146146
, "The GHC diagnostics message:"
147147
, ""
148148
, "```console"
149-
, Builder.fromText . Text.stripEnd . pack $ Diagnostic.format diagnostic
149+
, Builder.fromText . Text.stripEnd . pack $ Diagnostic.format ShowErrorContext diagnostic
150150
, "```"
151151
]
152152

src/GHC/Diagnostic.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ import GHC.Diagnostic.Type as Diagnostic
3939
import GHC.Diagnostic.Annotated
4040
import GHC.Diagnostic.Util
4141

42-
formatAnnotated :: Int -> Annotated -> (Int, Text)
43-
formatAnnotated start annotated = case formatSolutions start annotated.solutions of
44-
(next, solutions) -> (next, Builder.toText $ fromString (format annotated.diagnostic) <> solutions)
42+
formatAnnotated :: ShowErrorContext -> Int -> Annotated -> (Int, Text)
43+
formatAnnotated showErrorContext start annotated = case formatSolutions start annotated.solutions of
44+
(next, solutions) -> (next, Builder.toText $ fromString (format showErrorContext annotated.diagnostic) <> solutions)
4545

4646
formatSolutions :: Int -> [Solution] -> (Int, Builder)
4747
formatSolutions start = zipWith formatNumbered [start..] >>> reverse >>> \ case

src/GHC/Diagnostic/Type.hs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ module GHC.Diagnostic.Type (
66
, Severity(..)
77
, Reason(..)
88
, parse
9+
10+
, ShowErrorContext(..)
911
, format
1012
) where
1113

@@ -65,8 +67,11 @@ data Category = Category {
6567
parse :: ByteString -> Maybe Diagnostic
6668
parse = fmap removeGhciSpecificHints . decodeThrow
6769

68-
format :: Diagnostic -> String
69-
format diagnostic = render $ unlines [
70+
data ShowErrorContext = ShowErrorContext | NoShowErrorContext
71+
deriving (Eq, Show)
72+
73+
format :: ShowErrorContext -> Diagnostic -> String
74+
format showErrorContext diagnostic = render $ unlines [
7075
hang header 4 messageWithHints
7176
, ""
7277
, ""
@@ -109,7 +114,9 @@ format diagnostic = render $ unlines [
109114
ReasonCategory {} -> "-Werror="
110115

111116
message :: Doc
112-
message = bulleted $ map (verbatim . T.stripStart) diagnostic.message
117+
message = bulleted $ map (verbatim . T.stripStart) case showErrorContext of
118+
ShowErrorContext -> diagnostic.message
119+
NoShowErrorContext -> dropErrorContext diagnostic.message
113120

114121
hints :: [Doc]
115122
hints = map verbatim diagnostic.hints
@@ -151,3 +158,15 @@ removeGhciSpecificHints diagnostic = diagnostic { hints = map processHint diagno
151158
hint : "You may enable these language extensions in GHCi with:" : ghciHints
152159
| all isSetLanguageExtension ghciHints -> hint
153160
_ -> input
161+
162+
dropErrorContext :: [Text] -> [Text]
163+
dropErrorContext = filter \ m -> not $ or $ map ($ m) [
164+
startsWith " defined at "
165+
, startsWith "In an equation for "
166+
, startsWith "In a stmt of a "
167+
, startsWith "In the expression: "
168+
, startsWith "In the Template Haskell quotation "
169+
]
170+
where
171+
startsWith :: Text -> Text -> Bool
172+
startsWith = T.isPrefixOf

src/Language/Haskell/GhciWrapper.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import System.Exit (exitFailure)
3131
import Util (isWritableByOthers)
3232
import ReadHandle hiding (getResult)
3333
import qualified ReadHandle
34-
import GHC.Diagnostic (Annotated)
34+
import GHC.Diagnostic (ShowErrorContext(..), Annotated)
3535
import qualified GHC.Diagnostic as Diagnostic
3636

3737
data Config = Config {
@@ -196,15 +196,15 @@ extractAnnotatedDiagnostics numberForNextSolution getAvailableImports = ReadHand
196196
Nothing -> return Nothing
197197
Just diagnostic -> do
198198
n <- readIORef numberForNextSolution
199-
let (next, formatted) = Diagnostic.formatAnnotated n diagnostic
199+
let (next, formatted) = Diagnostic.formatAnnotated NoShowErrorContext n diagnostic
200200
writeIORef numberForNextSolution next
201201
return $ Just (diagnostic, T.encodeUtf8 formatted)
202202
}
203203

204204
extractDiagnostics :: ReadHandle.Extract Diagnostic.Diagnostic
205205
extractDiagnostics = ReadHandle.Extract {
206206
isPartialMessage = ByteString.isPrefixOf "{"
207-
, parseMessage = \ input -> return $ (id &&& encodeUtf8 . Diagnostic.format) <$> Diagnostic.parse input
207+
, parseMessage = \ input -> return $ (id &&& encodeUtf8 . Diagnostic.format NoShowErrorContext) <$> Diagnostic.parse input
208208
}
209209

210210
getResult :: Extract a -> Interpreter -> IO (String, [a])

test/GHC/DiagnosticSpec.hs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,10 @@ testWith :: HasCallStack => FilePath -> GHC -> [String] -> String -> Maybe Annot
7777
testWith name requiredVersion extraArgs (unindent -> code) annotation solutions = it name do
7878
unless (T.null code) do
7979
ensureFile src $ T.encodeUtf8 code
80+
8081
err <- translate <$> ghc ["-fno-diagnostics-show-caret"]
82+
errNoContext <- translate <$> ghc ["-fno-diagnostics-show-caret", "-fno-show-error-context"]
83+
8184
json <- pretty <$> ghc ["-fdiagnostics-as-json", "--interactive", "-ignore-dot-ghci"]
8285
ensureFile (dir </> "err.out") (encodeUtf8 err)
8386
ensureFile (dir </> "err.yaml") (encodeUtf8 $ normalizeGhcVersion json)
@@ -86,7 +89,9 @@ testWith name requiredVersion extraArgs (unindent -> code) annotation solutions
8689
expectationFailure $ "Parsing JSON failed:\n\n" <> json
8790
Just annotated -> addAnnotation (separator <> err <> separator) do
8891
whenGhc requiredVersion do
89-
format annotated.diagnostic `shouldBe` err
92+
format ShowErrorContext annotated.diagnostic `shouldBe` err
93+
format NoShowErrorContext annotated.diagnostic `shouldBe` errNoContext
94+
9095
annotated.annotation `shouldBe` annotation
9196
annotated.solutions `shouldBe` solutions
9297
where
@@ -467,7 +472,7 @@ spec = do
467472

468473
it "formats an annotated diagnostic message" do
469474
Just annotated <- B.readFile "test/fixtures/not-in-scope/err.yaml" >>= parseAnnotated getAvailableImports
470-
stripAnsi . unpack <$> formatAnnotated 1 annotated `shouldBe` (2, unlines [
475+
stripAnsi . unpack <$> formatAnnotated NoShowErrorContext 1 annotated `shouldBe` (2, unlines [
471476
"test/fixtures/not-in-scope/Foo.hs:2:7: error: [GHC-88464]"
472477
, " Variable not in scope: catMaybes"
473478
, ""
@@ -477,7 +482,7 @@ spec = do
477482

478483
it "formats an annotated diagnostic message" do
479484
Just annotated <- B.readFile "test/fixtures/not-in-scope-operator/err.yaml" >>= parseAnnotated getAvailableImports
480-
stripAnsi . unpack <$> formatAnnotated 1 annotated `shouldBe` (5, unlines [
485+
stripAnsi . unpack <$> formatAnnotated NoShowErrorContext 1 annotated `shouldBe` (5, unlines [
481486
"test/fixtures/not-in-scope-operator/Foo.hs:2:7: error: [GHC-88464]"
482487
, " Variable not in scope: <&>"
483488
, " Suggested fix:"

0 commit comments

Comments
 (0)