Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support structured diagnostics #4311

Open
wants to merge 42 commits into
base: master
Choose a base branch
from

Conversation

dylan-thinnes
Copy link

@dylan-thinnes dylan-thinnes commented Jun 10, 2024

Addresses #2014

Should hopefully enable #3246

TODOs:

Additional work (future PRs):

  • Replacing regexes with structured diagnostic checks
  • Improve typing so that it can be known that an error has a structured diagnostic, instead of defaulting to Maybe

Copy link
Collaborator

@michaelpj michaelpj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possibly insane suggestion: could we write some orphan JSON instances for GHC's message types and stuff the GhcMessage into the data field on the Diagnostic? Then we don't have to carry anything else around, and we won't lose things going via the DiagnosticStore?

ghcide/test/exe/CradleTests.hs Outdated Show resolved Hide resolved
ghcide/src/Development/IDE/Types/Diagnostics.hs Outdated Show resolved Hide resolved
ghcide/src/Development/IDE/Types/Diagnostics.hs Outdated Show resolved Hide resolved
ghcide/src/Development/IDE/GHC/Warnings.hs Show resolved Hide resolved
ghcide/src/Development/IDE/GHC/Warnings.hs Outdated Show resolved Hide resolved
ghcide/src/Development/IDE/GHC/Error.hs Show resolved Hide resolved
ghcide/src/Development/IDE/GHC/Compat/Driver.hs Outdated Show resolved Hide resolved
ghcide/src/Development/IDE/Core/Shake.hs Outdated Show resolved Hide resolved
@dylan-thinnes dylan-thinnes force-pushed the support-structured-diagnostics branch from 12b364a to 73dc069 Compare June 16, 2024 13:41
@dylan-thinnes dylan-thinnes force-pushed the support-structured-diagnostics branch from 73dc069 to 003c15c Compare June 16, 2024 13:54
@dylan-thinnes
Copy link
Author

Re: deriving JSON instances, the messages often contain fragments of AST and other things which aren't terribly friendly to ToJSON/FromJSON. I tried deriving an instance and immediately came up against

No instance for ‘ToJSON
                         (Language.Haskell.Syntax.Binds.HsBindLR
                            GHC.Hs.Extension.GhcRn GHC.Hs.Extension.GhcRn)’

which makes me think this approach is not going to be easy.

@dylan-thinnes dylan-thinnes marked this pull request as ready for review June 16, 2024 14:44
@dylan-thinnes
Copy link
Author

Gonna take a break for today, but two big TODOs remain, and a few open questions.

@michaelpj
Copy link
Collaborator

Re: deriving JSON instances, the messages often contain fragments of AST and other things which aren't terribly friendly to ToJSON/FromJSON.

This tickled my brain and I remembered that GHC has its own JSON conversion functions! https://gitlab.haskell.org/ghc/ghc/-/blob/master/compiler/GHC/Utils/Json.hs?ref_type=heads

And they definitely have instances for diagnostics, since they're used in the JSON output for GHC, see e.g. here: https://gitlab.haskell.org/ghc/ghc/-/merge_requests/11093

Having written this I realise that that only handles one direction, so it doesn't help us much, oh well.

@michaelpj
Copy link
Collaborator

They might have another serialization format, though. There's nothing stopping us from serializing them to bytes and the smuggling a bytestring as a JSON string in data 😂

@wz1000
Copy link
Collaborator

wz1000 commented Jun 20, 2024

I don't think we should serialise the raw data over the wire, instead we should stash it in some sort of Map and send over an identifier/key which we can later use to look up the value from the Map. It requires care to ensure that the values are properly garbage collected when no longer valid though.

@michaelpj
Copy link
Collaborator

I don't think we should serialise the raw data over the wire, instead we should stash it in some sort of Map and send over an identifier/key which we can later use to look up the value from the Map. It requires care to ensure that the values are properly garbage collected when no longer valid though.

There's no viable serialization format anyway. I do think it would be nice if there was one, though.

That said, I don't think we should need this info in diagnostics that we e.g. get back from the client. So the main issue is when we e.g. ask for the diagnostics covering a range so we can then match on them to work out code actions. At that point we need the GHC diagnostics. So I don't think we need a separate store, at worst we might have to augment our server-side diagnostic store.

@dylan-thinnes
Copy link
Author

dylan-thinnes commented Jun 24, 2024

One remaining point to address from review: #4311 (comment), have made a commit for it 414c845

I'm looking into the CI failures now - I assume some CPP shenanigans are at least part of the issue.

@@ -884,13 +885,28 @@ newComponentCache recorder exts _cfp hsc_env old_cis new_cis = do
hscEnv' <- -- Set up a multi component session with the other units on GHC 9.4
Compat.initUnits dfs hsc_env

let closure_errs = checkHomeUnitsClosed' (hsc_unit_env hscEnv') (hsc_all_home_unit_ids hscEnv')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like this section can now be nicely DRYied like this (also taking advantage of the fact that we recently dropped support for GHCs < 9.4, so MIN_VERSION_ghc(9,3,0) will always be true going forward):

    let closure_errs = maybeToList $ checkHomeUnitsClosed' (hsc_unit_env hscEnv') (hsc_all_home_unit_ids hscEnv')
        closure_err_to_multi_err diag = ideErrorWithSource
            (Just "cradle")
            (Just DiagnosticSeverity_Warning)
            _cfp
            (T.pack (Compat.printWithoutUniques (singleMessage diag)))
#if MIN_VERSION_ghc(9,6,1)
            (Just (fmap GhcDriverMessage diag))
#else
            Nothing
#endif
        multi_errs = map closure_err_to_multi_err closure_errs
        bad_units = OS.fromList $ concat $ do
            x <- map errMsgDiagnostic closure_errs
            DriverHomePackagesNotClosed us <- pure x
            pure us
        isBad ci = (homeUnitId_ (componentDynFlags ci)) `OS.member` bad_units

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants