hoovercj / vscode-ghc-mod

An extension to bring ghc-mod to vs code
MIT License
31 stars 13 forks source link

[Support] Red Squiggles under Import statement stops code compilation #42

Closed ghost closed 7 years ago

ghost commented 7 years ago

Forgive me if this is a newbie mistake, I am currently learning Haskell. I am basically just using the REPL and code files atm, not working very much with full projects. If I try to load the file below:

module Test where

import           Test.QuickCheck.Checkers 

data List a =
    Nil
    | Cons a (List a)
    deriving (Eq, Showasdadasdasda)

there is a red squiggle under the word import. This says

"Failed to load interface for ‘Test.QuickCheck.Checkers’ Use -v to see a list of the files searched for."

When this happens, I am unable to see other errors further into the file.

I'm curious as to why this happens, I've run stack build QuickCheck to bring the dependency down so I'm not sure why it isn't being found.

javra commented 7 years ago

I have the same problem. And I'm completely unsure if it's because of stack or because i got a wrong order on my PATH... Does stack yourfile.hs work from outside VS Code?

ghost commented 7 years ago

Yes, it does. I sort of gave up on this for now and went ahead and took the drastic leap to spacemacs. The haskell support is very good. It's a bit of a learning curve but I'm liking it so far. I'm still watching this in the meantime.

danstiner commented 7 years ago

I think I understand what is happening here, I'll first try to explain what is happening and how creating projects for your Haskell code may avoid this issue, then later in this reply I'll take a stab at what bug might exist in the extension.

Note: Generally the #haskell-beginners IRC channel is a great place to ask questions, but this instance is a bit tricky since it involves a specific editor and extension. (https://www.haskell.org/irc)

So on a fresh install of stack, if I paste the code you helpfully gave into a file named Test.hs and then run stack runghc Test.hs I see the following output:

Run from outside a project, using implicit global project config
Using resolver: lts-3.19 from implicit global project's config file: /home/dan/.stack/global/stack.yaml

Test.hs:3:18:
    Could not find module ‘Test.QuickCheck.Checkers’
    Use -v to see a list of the files searched for.

If I then install the checkers package (The module Test.QuickCheck.Checkers actually comes from this package, not QuickCheck itself) (Running stack build also works but install is probably the right command)

stack install checkers

And then run stack runghc Test.hs again I see the following output:

Run from outside a project, using implicit global project config
Using resolver: lts-3.19 from implicit global project's config file: /home/dan/.stack/global/stack.yaml

Test.hs:8:19:
    Not in scope: type constructor or class ‘Showasdadasdasda’

At this point the module is being found from the installed package in the global stack directory. However relying on the global stack config is generally not a good approach, it is much better to create a project and explicitly list in it which packages you depend on. (Good intro tutorial: https://howistart.org/posts/haskell/1) I suspect having a project would actually work around the difference in behavior between command line and VSCode you are seeing here. Oh and the error here is just because you should have typed Show instead of Showasdadasdasda.

So @hoovercj to address the difference in behavior between the command line and VSCode, I suspect either due to how you are running ghc-mod or just ghc-mod itself is not correctly handling the global package directory stack has. I looked briefly at your code and I don't see anything odd, if you don't have any ideas either (like maybe you run it with an odd working directory or such?) it probably is a bug in ghc-mod itself and there isn't much to do except maybe make them aware of it.

danstiner commented 7 years ago

Confirmed running ghc-mod check Test.hs -v outputs the following which supports the idea ghc-mod is probably not falling back to the global stack package directory since it classifies this as a "plain GHC project".

info: Found no other project type, falling back to plain GHC project
Test.hs:3:18:     Could not find module ‘Test.QuickCheck.Checkers’     Use -v to see a list of the files searched for.

Follow up could be to open an issue against ghc-mod if one does not already exist, but honestly it is hard to say what ghc-mod should do in this situation, it doesn't know if it should be falling back to the global cabal or stack package directory.

javra commented 7 years ago

I'm not sure if I completely understand what you wrote. Let me just write what I did, maybe it's the same problem as @nickolasacosta's, or maybe it's not:

  1. I'm on a fresh stack install

  2. I do stack new stacktest simple, add yesod as a dependency in stacktest.cabal.

  3. I change stacktest/src/Main.hs to

    
    module Main where

import Yesod

main :: IO () main = putStrLn "hello world"



4. AutoComplete still works in VSCode (including AutoCompleting on the import), `stack build` works, `stack runghc Main.hs` works. But the import is underlined red and reports

>  "Failed to load interface for ‘Yesod’ Use -v to see a list of the files searched for."
danstiner commented 7 years ago

Oh, I would have expected that to work. It may still actually be a ghc-mod issue, but I would have thought that would work. Will try to reproduce and poke at it some more.

Thanks for the additional context.

hoovercj commented 7 years ago

@javra and @nickolasacosta

I have added slightly more sophisticated stack support in v1.2.0.

Could you update the extension, set "haskell.ghcMod.executablePath: "stack", and see if that solves this issue?

ghost commented 7 years ago

It seems the extension is seeing my imports now! woot! That is a major pain point gone and was the core reason for this issue. Thank you for your time and effort on this.

It's worth noting this worked with the setting set to "ghc-mod" or "stack".

danstiner commented 7 years ago

Did some additional verification and confirmed the approach of using the setting "stack" so that stack exec ghc-mod is used works for even for files not in a project so this should be completely resolved!

Dockerfile for refenerence:

FROM ubuntu:latest

RUN apt-get -y update && apt-get -y upgrade && apt-get -y install curl

RUN curl -sSL https://get.haskellstack.org/ | sh

RUN PATH="$PATH:/root/.local/bin"

RUN stack setup

RUN stack install ghc-mod

RUN stack install checkers

ADD Test.hs Test.hs

CMD stack exec ghc-mod check Test.hs
javra commented 7 years ago

It also works for me! Thanks for the support :)