Skip to content

Conversation

dylan-thinnes
Copy link
Contributor

@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?

@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
Contributor 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
Contributor Author

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

@dylan-thinnes dylan-thinnes requested a review from 0rphee as a code owner June 16, 2024 17:07
@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
Contributor 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.

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
@noughtmare
Copy link
Contributor

@dylan-thinnes are you still planning to work on this? The immediate CI issue seems very simple to solve. It seems there is just a redundant import. What else still needs to happen?

@noughtmare
Copy link
Contributor

Removing these four redundant imports fixes all issues for me locally:

diff --git a/plugins/hls-cabal-plugin/src/Ide/Plugin/Cabal/Diagnostics.hs b/plugins/hls-cabal-plugin/src/Ide/Plugin/Cabal/Diagnostics.hs index 5425c419..a0cea4dc 100644 --- a/plugins/hls-cabal-plugin/src/Ide/Plugin/Cabal/Diagnostics.hs +++ b/plugins/hls-cabal-plugin/src/Ide/Plugin/Cabal/Diagnostics.hs @@ -13,8 +13,7 @@ where import Control.Lens ((&), (.~)) import qualified Data.Text as T -import Development.IDE (FileDiagnostic, - ShowDiagnostic (ShowDiag)) +import Development.IDE (FileDiagnostic) import Development.IDE.Types.Diagnostics (fdLspDiagnosticL, ideErrorWithSource) import Distribution.Fields (showPError, showPWarning) diff --git a/plugins/hls-refactor-plugin/src/Development/IDE/Plugin/CodeAction.hs b/plugins/hls-refactor-plugin/src/Development/IDE/Plugin/CodeAction.hs index c9ce440f..35f41ea3 100644 --- a/plugins/hls-refactor-plugin/src/Development/IDE/Plugin/CodeAction.hs +++ b/plugins/hls-refactor-plugin/src/Development/IDE/Plugin/CodeAction.hs @@ -87,7 +87,6 @@ import Language.LSP.Protocol.Types (ApplyWorkspa CodeActionKind (CodeActionKind_QuickFix), CodeActionParams (CodeActionParams), Command, - Diagnostic (..), MessageType (..), Null (Null), ShowMessageParams (..), diff --git a/plugins/hls-refactor-plugin/test/Main.hs b/plugins/hls-refactor-plugin/test/Main.hs index 377a6758..434fe48b 100644 --- a/plugins/hls-refactor-plugin/test/Main.hs +++ b/plugins/hls-refactor-plugin/test/Main.hs @@ -21,7 +21,6 @@ import Data.Foldable import Data.List.Extra import Data.Maybe import qualified Data.Text as T -import Data.Tuple.Extra import Development.IDE.GHC.Util import Development.IDE.Plugin.Completions.Types (extendImportCommandId) import Development.IDE.Test diff --git a/plugins/hls-stan-plugin/src/Ide/Plugin/Stan.hs b/plugins/hls-stan-plugin/src/Ide/Plugin/Stan.hs index 1fc7fa42..51e00550 100644 --- a/plugins/hls-stan-plugin/src/Ide/Plugin/Stan.hs +++ b/plugins/hls-stan-plugin/src/Ide/Plugin/Stan.hs @@ -14,7 +14,6 @@ import qualified Data.Text as T import Development.IDE import Development.IDE.Core.Rules (getHieFile) import qualified Development.IDE.Core.Shake as Shake -import Development.IDE.Types.Diagnostics import GHC.Generics (Generic) import Ide.Plugin.Config (PluginConfig (..)) import Ide.Types (PluginDescriptor (..),
noughtmare pushed a commit to noughtmare/haskell-language-server that referenced this pull request Oct 17, 2024
noughtmare pushed a commit to noughtmare/haskell-language-server that referenced this pull request Oct 23, 2024
@fendor
Copy link
Collaborator

fendor commented Dec 23, 2024

Closing in favour of #4433

@fendor fendor closed this Dec 23, 2024
fendor added a commit that referenced this pull request Jan 4, 2025
* Change FileDiagnostic type synonym to a datatype * Make `ideErrorWithSource` produce FileDiagnostic by adding filepath arg * Supply structured error wherever we easily can - TODOs for hard parts We're leaving the TODOs for either later in this PR or in another PR * Fix UnitTests for new FileDiagnostic struct * Remove explicit uses of FileDiagnostic, add codes to LSP diagnostics * Add field for expected error codes in ghcide tests * Expect GHC-83865 for "type error" test - basic test * Return structured warnings in TcModuleResult by copying from Driver * Store FileDiagnostic instead of LSP Diagnostic in Shake store * Add expected error codes for diagnostics that have them * Dispatch TODOs, amend remaining TODOs as future work * Add scary comments all over copied code in Compat.Driver * Update all remaining diagnostics that could use an expected error code * Add _code to pretty printing for FileDiagnostic * Use case instead of `maybe` for StructuredMessage match * Use CPP to prevent setting _code before structured errors * Swap modifier for lenses, document StructuredMessage type * Add link to Issue & MR to Compat.Driver * Drop attachReason logic from withWarnings, technically incorrect * Revert "Drop attachReason logic", needed by pragmas-plugin This reverts commit 4fed987. * Fix plugins where necessary for new diagnostic structure * Fix build issues with other tests from `expectDiagnostics` * Improve comment on metadata fdStructuredMessage in FileDiagnostic * Add note to withWarnings explaining the current state of things * Attach reasons into data field of LSP Diagnostic instead of code field Had to move `attachReason` between modules to achieve this, which is fine because it was never exported from its own module. * Fix up mistakes from merge, TODO fix merge issues for 9.3.0 * Set CodeDescription from HaskellErrorIndex when available * Remove debugging print, fix expectation for preprocessor tests * Fix CPP for using Show instance on DiagnosticCode * Remove diagFromErrMsgs for GHC version < 9.6.1 using CPP * CPP fix * More stylish-haskell, more CPP fix * Fix all stylish-haskell errors triggering * Fix more CPP * Only override the LSP diagnostic code when not already set * Fixes for stylish-haskell stylish-haskell does not handle CPP pragmas very well, is this a regression? * Qualify s, t for FuzzySearch * Ignore use of unsafePerformIO in FuzzySearch * Properly split GHC.Types.Error import in Diagnostics for stylish-haskell * Force type signature of annotation on FuzzySearch.dictionary * DRY up definition of closure_errs From review #4311 (comment) * Remove unused imports * Post-rebase fixes * stylish-haskell formatting * Fix issue with GHC 9.4 * Please stylish-haskell * Ignore error codes when testing GHC 9.4 * Workaround darwin GHC bug in hls-hlint-plugin * Put the workaround in the right place * Revert "Set CodeDescription from HaskellErrorIndex when available" This reverts commit 14d6697. * Resolve fendor's feedback * Apply stylish-haskell formatting * Apply more stylish-haskell formatting * Resolve some of soulomoon's feedback * Fix small issues * Remove unused imports * Remove StructuredDiagnostic * Revert "Remove StructuredDiagnostic" This reverts commit 0776c65. * Remove the unused parameter from 'ideErrorText' * Add documentation to diagnostic helpers * Add action to query active diagnostics for a given Range Implement 'rangesOverlap' function which checks whether two 'Range's overlap in any way. Implement two new plugin utility functions which allow to conveniently get all currently displayed diagnostics for a given 'Range'. * Use lens for updating Diagnostic * Add GHC Structured Error compatibility module Add compatibility module for GHC's structured error messages. Introduce 'Prism's and 'Lens's to easily access nested structures. Expand documentation for 'StructuredMessage' * Remove unused imports * Don't suggest -Wno-deferred-out-of-scope-variables (#4441) Fixes #4440 Fixes test for disabling deferred-type-errors. * Build HLS with GHC 9.8.3 (#4444) * ci(mergify): upgrade configuration to current format (#4454) Co-authored-by: Mergify <37929162+mergify[bot]@users.noreply.github.com> * More tests and better docs for cabal-add (#4455) * new tests * change codeAction title * more tests and docs --------- Co-authored-by: fendor <fendor@users.noreply.github.com> * Fix compatibility with GHC 9.4 and rename function * Use GHC Note syntax and reference Note in docs Allows HLS to 'Goto Definition' for Note references. * Add doc comment for 'tmrWarnings' * Push CPP statements to compatibility module * Fix formatting in Development.IDE.GHC.Compat.Error --------- Co-authored-by: Dylan Thinnes <dylan.thinnes@protonmail.com> Co-authored-by: soulomoon <fwy996602672@gmail.com> Co-authored-by: Fendor <fendor@posteo.de> Co-authored-by: jeukshi <orlowd+gh@gmail.com> Co-authored-by: fendor <fendor@users.noreply.github.com> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> Co-authored-by: Georgii Gerasev <54953043+VenInf@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

6 participants