haskell-language-server icon indicating copy to clipboard operation
haskell-language-server copied to clipboard

textDocument/references returns references only in the current file for Stack projects

Open alienvspredator opened this issue 5 months ago • 2 comments

textDocument/references method returns results for type references found only in the current file for a Stack project.

Given the following project structure:

app/
  | Main.hs
src/
  | Game/
    | State.hs
package.yaml
Setup.hs
stack.yaml
example.cabal

And the State.hs content:

-- State.hs
module Game.State (GameState (..)) where

import Game.Core.Position (Position)

data GameState where
  GameState :: {position :: Position, message :: String} -> GameState

GameState is used in the Main.hs as well.

Sending the textDocument/definition request to the LSP with the following params

{
    "textDocument": {
        "uri": "file:///example/src/Game/State.hs"
    },
    "position": {
        "line": 5,
        "character": 7
    },
    "context": {
        "includeDeclaration": true
    }
}

Results in the following response

[
    {
        "range": {
            "end": {
                "character": 11,
                "line": 5
            },
            "start": {
                "character": 2,
                "line": 5
            }
        },
        "uri": "file:///example/src/Game/State.hs"
    },
    {
        "range": {
            "end": {
                "character": 24,
                "line": 5
            },
            "start": {
                "character": 16,
                "line": 5
            }
        },
        "uri": "file:///example/src/Game/State.hs"
    },
    {
        "range": {
            "end": {
                "character": 45,
                "line": 5
            },
            "start": {
                "character": 38,
                "line": 5
            }
        },
        "uri": "file:///example/src/Game/State.hs"
    }
]

Results for example/src/app/Main.hs are missing.

Your environment

Which OS do you use? macOS

Which version of GHC do you use and how did you install it? 9.10.2 from ghcup

How is your project built? Stack 3.7.1

Which version of HLS do you use and how did you install it? 2.11.0

Have you configured HLS in any way (especially: a hie.yaml file)? No

Steps to reproduce

Send textDocument/references request for a data definition using GADTs.

Expected behaviour

Results are found in the whole project.

Actual behaviour

Results are found only in the file where the data is defined.

Debug information

Request/Response from LSP above

alienvspredator avatar Jul 29 '25 22:07 alienvspredator

This seems to be happening due to an error during analysing Main.hs mentioned here https://github.com/haskell/haskell-language-server/issues/4634#issuecomment-3123674524.

Running stack build doesn't resolve the issue.

alienvspredator avatar Jul 29 '25 23:07 alienvspredator

Hi!

Thanks for the bug report!

You may be running into "lazy" component loading. By default (and always when using stack) HLS loads only components into the IDE if you open a "witness" from this component by opening the file in your editor. Features such as Goto Definition and Goto References only work for components that have already been loaded into the HLS session.

So, in your example, I expect that you only opened State.hs, but not Main.hs, so the component executable is not loaded, but only the library is. In theory, it should work if you opened the Main.hs at least once before running "Go to References".

If you use cabal, it is possible to use "eager" component loading, e.g. load all components immediately, but this is not possible when using stack.

fendor avatar Jul 30 '25 07:07 fendor