skip to Main Content

I have a standalone Haskell file I am editing in Visual Studio Code. I’m learning Haskell and simply using ghci to run my code in the integrated terminal.

However, the VS Code editor is complaining that it Could not find module... for one of my import statements (e.g., import System.Random).

Question: If I don’t want to create a full project with a dependencies list, what can I do?

2

Answers


  1. Chosen as BEST ANSWER

    This seems to work, says the newbie:

    1. In a terminal, run cabal install --lib random (for example)
    2. In VS Code's Command Palette, run Developer: Reload Window

    The cabal install --help says:

    If TARGET is a library and --lib (provisional) is used,
    it will be added to the global environment.
    

  2. You can make a very light-weight cabal-package-alike inline in Haskell source files like this:

    {- cabal:
    build-depends: base, random
    -}
    import System.Random
    

    Then you can load ghci by running cabal repl foo.hs (where foo.hs is the name of the file with this content). I’m not sure how to configure VS Code to run cabal repl instead of ghci but perhaps you can poke around and find it yourself now that you know about this feature of cabal. As a bonus, this gives you machine-checkable documentation of what packages you installed along your way to creating your toys.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search