Skip to content

JSON Diagnostics Output (deprecate -ddump-json)

Ben Bellick requested to merge benbellick/ghc:json-diag-dump into master

Addresses #19278. Tracked at #24113.

This is an MR to deprecate the old -ddump-json flag and replace it with a new, more standardized -fdiagnostics-as-json flag. Discussion of the standardization process can be found here.

Error information is routed to stderr, while normal info is routed to stdout, unchanged, as demonstrated below:

ghc bad.hs -fdiagnostics-as-json
[1 of 1] Compiling Bad              ( ../tmp/bad.hs, ../tmp/bad.o )
{"version":"1.0","ghcVersion":"ghc-9.9.20230817","span":{"file":"bad.hs","start":{"line":9,"column":3},"end":{"line":9,"column":14}},"severity":"Error","code":88464,"message":["Variable not in scope: printLineas :: String -> IO a1"],"hints":[]}
{"version":"1.0","ghcVersion":"ghc-9.9.20230817","span":{"file":"bad.hs","start":{"line":10,"column":3},"end":{"line":10,"column":9}},"severity":"Error","code":88464,"message":["Variable not in scope: blaksh :: String -> IO ()"],"hints":[]}

This enables users to route stdout using the standard unix-style control flow. I made the decision to use the JSON Lines style logging. By doing this, we can log each message as it is reported by the compiler without ever outputting partial JSON.

Another component this MR addresses is the interaction with ghci (this issue was discussed here. Similar to ghc, the diagnostics are handled in a JSON Lines style.

> ghci -fdiagnostics-as-json
GHCi, version 9.9.20230815: https://www.haskell.org/ghc/  :? for help
ghci> print 2 + 2
{"version":"1.0","ghcVersion":"ghc-9.9.20230817","span":{"file":"<interactive>","start":{"line":1,"column":9},"end":{"line":1,"column":10}},"severity":"Error","code":39999,"message":["No instance for `Num (IO ())' arising from a use of `+'","In the expression: print 2 + 2\nIn an equation for `it': it = print 2 + 2"],"hints":[]}
ghci> print a + b
{"version":"1.0","ghcVersion":"ghc-9.9.20230817","span":{"file":"<interactive>","start":{"line":2,"column":7},"end":{"line":2,"column":8}},"severity":"Error","code":88464,"message":["Variable not in scope: a"],"hints":[]}
{"version":"1.0","ghcVersion":"ghc-9.9.20230817","span":{"file":"<interactive>","start":{"line":2,"column":11},"end":{"line":2,"column":12}},"severity":"Error","code":88464,"message":["Variable not in scope: b :: IO ()"],"hints":[]}
Edited by Ben Bellick

Merge request reports