Typed rules - #5025
Conversation
wz1000
left a comment
There was a problem hiding this comment.
Broadly looks good, some inputs need to be tightened up
fendor
left a comment
There was a problem hiding this comment.
Thanks, this looks very nice! This is a nitpick review, so it is the intention to be done after the review is addressed. I have reviewed the ghcide changes, will review plugins soon.
Further, we agreed the following documentation is still missing and will be added:
* What are the existing rules?
* What is RuleInput?
* Why do we have RuleInput
* What is the design of RuleInput
Note [Hierarchical Inputs]
Note [RuleInput]
Reference these notes from various type classes and RuleInput type
* Helper functions need documentation
| toPriority :: PluginError -> Priority | ||
| toPriority (PluginInternalError _) = Error | ||
| toPriority (PluginInvalidParams _) = Warning | ||
| toPriority (PluginUnsupportedUriType _) = Warning |
There was a problem hiding this comment.
We might want to demote this to Debug. IIRC, a warning is displayed in the editor, but these errors will be perfectly sensible in the future, right?
@wz1000 opinions?
144b524 to
ec6e677
Compare
bc09427 to
0afd603
Compare
| case file of | ||
| SomeProjectHaskellInput projectFile -> | ||
| setFileModified (cmapWithPrio LogFileStore recorder) (VFSModified vfs) ide False projectFile action | ||
| _ -> setSomethingModified (VFSModified vfs) ide (fromNormalizedFilePath (inputFilePath file) ++ " (modified)") action |
There was a problem hiding this comment.
We shouldn't do anything here, rather log this case.
There was a problem hiding this comment.
@wz1000 What should we do in this case? Call setSomethingModified for completeness? We need to add it to the set of files of interest, but it might not need to record that it isn't the first time this non project haskell file was opened, right? 🤔
There was a problem hiding this comment.
setSomethingModified is not correct here, that will trash the entire session. I think setFileModified should accept SomeHaskellInput instead of ProjectHaskellInput
It should always restart with the new VFS and dirty the file’s GetModificationTime, then run typecheckParents only in the SomeProjectHaskellInput branch
| case file of | ||
| SomeProjectHaskellInput projectFile -> | ||
| setFileModified (cmapWithPrio LogFileStore recorder) (VFSModified vfs) ide False projectFile action | ||
| _ -> setSomethingModified (VFSModified vfs) ide (fromNormalizedFilePath (inputFilePath file) ++ " (modified)") action |
There was a problem hiding this comment.
Should we set something modified here? What does this do?
| case file of | ||
| SomeProjectHaskellInput projectFile -> | ||
| setFileModified (cmapWithPrio LogFileStore recorder) (VFSModified vfs) ide True projectFile action | ||
| _ -> setSomethingModified (VFSModified vfs) ide (fromNormalizedFilePath (inputFilePath file) ++ " (modified)") action |
Store SomeInput in Shake keys and route rule lookup helpers through typed RuleInput values instead of raw normalized file paths. This also updates tracing and no-file handling to work with the typed key representation.
Thread SomeFileInput, SomeHaskellInput, and ProjectHaskellInput through file store, HIE indexing, and module rules. Add a generic input reclassification helper so call sites can move between compatible typed rule inputs without bespoke wrappers.
Thread typed rule inputs through core Shake helpers, file-store state, dependency graph lookups, and LSP request handlers. Convert raw normalized paths at API boundaries so project, Haskell, and general file rules receive the appropriate input type.
Builds Ghcide, removes helpers that are not used or should not be used
Carry ProjectHaskellInput through dependency information, import lookup, reverse dependency traversal, HIE reads, and eval/reference paths instead of round-tripping through NormalizedFilePath. This keeps non-project Haskell inputs out of project-only rules while preserving the typed inputs needed by downstream Shake rules.
Track project Haskell inputs in session state, known targets, VFS-backed services, and caches. Migrate hover, notes, eval, and semantic token rules to use their typed inputs directly.
wz1000
left a comment
There was a problem hiding this comment.
plugins and input types broadly look correct. I need to do another more careful pass though.
| isProjectHaskellInput fp = isHaskellFilePath fp && not (isDependencyHaskellPath fp) | ||
|
|
||
| isDependencyHaskellPath :: NormalizedFilePath -> Bool | ||
| isDependencyHaskellPath = (".hls/dependencies" `isInfixOf`) . normalise . fromNormalizedFilePath |
There was a problem hiding this comment.
Let's use something like
isDependencyHaskellPath =
isInfixOf [".hls", "dependencies"]
. splitDirectories
. fromNormalizedFilePath
also normalise is redundant after fromNormalizedFilePath
This let's us avoid issues with path seperators on windows etc.
Perhaps we could even use System.FilePath.Glob, we already have that dependency.
| case file of | ||
| SomeProjectHaskellInput projectFile -> | ||
| setFileModified (cmapWithPrio LogFileStore recorder) (VFSModified vfs) ide False projectFile action | ||
| _ -> setSomethingModified (VFSModified vfs) ide (fromNormalizedFilePath (inputFilePath file) ++ " (modified)") action |
There was a problem hiding this comment.
setSomethingModified is not correct here, that will trash the entire session. I think setFileModified should accept SomeHaskellInput instead of ProjectHaskellInput
It should always restart with the new VFS and dirty the file’s GetModificationTime, then run typecheckParents only in the SomeProjectHaskellInput branch
| deriving anyclass (Hashable, NFData) | ||
| -- GetNotes collects all note definition across all files in the | ||
| -- project. It returns a map from note name to pair of (filepath, position). | ||
| type instance RuleInput GetNotes = SomeFileInput |
There was a problem hiding this comment.
The comment claims it collects note definitions across all files, then why does it have a SomeFileInput instead of NoInput?
| deriving anyclass (Hashable, NFData) | ||
| -- GetNoteReferences collects all note references across all files in the | ||
| -- project. It returns a map from note name to list of (filepath, position). | ||
| type instance RuleInput GetNoteReferences = SomeFileInput |
This PR introduces typed inputs for HLS/ghcide rules.
HLS already associates every rule key with its result through
RuleResult, but all rules previously accepted the same input type:NormalizedFilePath. Theis PR adds an open type family that associates each rule with the kind of input it supports:Rule inputs are now part of the rule's contract, just like rule results.
Why this is needed
A raw
NormalizedFilePathdoes not tell us what a path actually represent. These distinctions matter. For example,TypeCheckandGhcSessionrequire a project component and session, while file-content rules can work on many filetypes. Typed inputs make these assumptions explicit, reject incorrect rule/input pairings earlier, and provide the foundation needed to support dependency sources without treating them as project modules.
How it works
The new
Development.IDE.Core.RuleInputmodule defines this input hierarchy:Internally, Shake still needs one heterogeneous key representation.
SomeInputtherefore existentially stores typed inputs inside
Q, whileInputFingerprintprovides consistent equality and hashing.