Skip to content

Typed rules - #5025

Open
vidit-od wants to merge 22 commits into
haskell:masterfrom
vidit-od:Typed-Rules
Open

Typed rules#5025
vidit-od wants to merge 22 commits into
haskell:masterfrom
vidit-od:Typed-Rules

Conversation

@vidit-od

@vidit-od vidit-od commented Jul 23, 2026

Copy link
Copy Markdown
Collaborator

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:

type family RuleInput k

type instance RuleInput TypeCheck = ProjectHaskellInput
type instance RuleResult TypeCheck = TcModuleResult

Rule inputs are now part of the rule's contract, just like rule results.

Why this is needed

A raw NormalizedFilePath does not tell us what a path actually represent. These distinctions matter. For example, TypeCheck and GhcSession require a project component and session, while file-content rules can work on many file
types. 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.RuleInput module defines this input hierarchy:

SomeInput
|-- NoInput
`-- SomeFileInput
    |-- SomeHaskellInput
    |   |-- ProjectHaskellInput
    |   `-- NonProjectHaskellInput
    |-- CabalInput
    `-- arbitrary NormalizedFilePath

Internally, Shake still needs one heterogeneous key representation. SomeInput
therefore existentially stores typed inputs inside Q, while InputFingerprint
provides consistent equality and hashing.

Comment thread ghcide/src/Development/IDE/Core/Actions.hs Outdated
Comment thread ghcide/src/Development/IDE/Core/RuleInput.hs Outdated
Comment thread ghcide/src/Development/IDE/Core/Rules.hs Outdated
Comment thread ghcide/src/Development/IDE/Core/Rules.hs Outdated

@wz1000 wz1000 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Broadly looks good, some inputs need to be tightened up

Comment thread ghcide/src/Development/IDE/Core/RuleInput.hs Outdated
Comment thread ghcide/src/Development/IDE/Core/Actions.hs Outdated
Comment thread ghcide/src/Development/IDE/Core/Actions.hs Outdated
Comment thread ghcide/src/Development/IDE/Core/Rules.hs Outdated
Comment thread ghcide/src/Development/IDE/Core/RuleTypes.hs Outdated

@fendor fendor left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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

Comment thread ghcide/session-loader/Development/IDE/Session/Diagnostics.hs Outdated
Comment thread ghcide/session-loader/Development/IDE/Session/Ghc.hs Outdated
Comment thread ghcide/session-loader/Development/IDE/Session/Ghc.hs Outdated
Comment thread ghcide/src/Development/IDE/Import/DependencyInformation.hs Outdated
Comment thread ghcide/src/Development/IDE/Import/FindImports.hs Outdated
Comment thread ghcide/src/Development/IDE/Spans/Pragmas.hs Outdated
Comment thread hls-plugin-api/src/Ide/Plugin/Error.hs Outdated
Comment thread hls-plugin-api/src/Ide/Plugin/Error.hs
Comment thread hls-plugin-api/src/Ide/Plugin/Error.hs
toPriority :: PluginError -> Priority
toPriority (PluginInternalError _) = Error
toPriority (PluginInvalidParams _) = Warning
toPriority (PluginUnsupportedUriType _) = Warning

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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?

@vidit-od
vidit-od force-pushed the Typed-Rules branch 7 times, most recently from 144b524 to ec6e677 Compare August 4, 2026 20:50
@vidit-od
vidit-od force-pushed the Typed-Rules branch 2 times, most recently from bc09427 to 0afd603 Compare August 8, 2026 13:06

@fendor fendor left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Some more comments :)

Comment thread ghcide/src/Development/IDE/Core/RuleInput.hs Outdated
Comment thread ghcide/src/Development/IDE/Core/RuleInput.hs Outdated
Comment thread ghcide/src/Development/IDE/Core/RuleInput.hs Outdated
Comment thread ghcide/src/Development/IDE/Core/RuleInput.hs Outdated
Comment thread ghcide/src/Development/IDE/Core/RuleInput.hs Outdated
Comment thread ghcide/src/Development/IDE/Core/RuleInput.hs Outdated
Comment thread ghcide/src/Development/IDE/Core/RuleInput.hs
Comment thread ghcide/src/Development/IDE/Core/RuleInput.hs Outdated
Comment thread ghcide/src/Development/IDE/Core/RuleInput.hs Outdated
Comment thread ghcide/src/Development/IDE/Core/RuleInput.hs
case file of
SomeProjectHaskellInput projectFile ->
setFileModified (cmapWithPrio LogFileStore recorder) (VFSModified vfs) ide False projectFile action
_ -> setSomethingModified (VFSModified vfs) ide (fromNormalizedFilePath (inputFilePath file) ++ " (modified)") action

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

We shouldn't do anything here, rather log this case.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Same here

Comment thread ghcide/src/Development/IDE/Core/Rules.hs Outdated
Comment thread ghcide/src/Development/IDE/Core/Shake.hs Outdated
Comment thread ghcide/src/Development/IDE/Core/Shake.hs Outdated
Comment thread ghcide/src/Development/IDE/Core/Shake.hs Outdated
Comment thread ghcide/src/Development/IDE/Core/Shake.hs Outdated
Comment thread ghcide/src/Development/IDE/Import/FindImports.hs Outdated
Comment thread ghcide/src/Development/IDE/Plugin/Completions.hs Outdated
Comment thread ghcide/src/Development/IDE/Plugin/Test.hs Outdated
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 wz1000 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

same as above.

@vidit-od vidit-od mentioned this pull request Aug 23, 2026
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.

3 participants