Stack: Failures, Errors, Frustrations

The “Could not find module …” error message usually means that you are missing a dependency in your package.yaml file.

The Data.GI.Base is a bit confusing because I believe you say that you have haskell-gi-base listed in your package.yaml. That should be enough. Maybe you should check it again? Also note that there might be multiple dependencies: fields in one package.yaml file.

For the others like aeson and random it seems you are just missing the dependency in your package.yaml.

There are some more pointers about adding dependencies in the Stack documentation.

Ok, homework for me. Read the complete Stack documentation…
Well, I list all these dependencies like so

dependencies :
base
classy-prelude
time
aeson
random

safe the file do stack build and still get the same errors…

Can you post the full package.yaml file that you are using? Preferably via https://pastebin.com or something like that.

I dont have an account for pastebin yet. So here is the entire file content.

name:                hauth
version:             0.1.0.0
github:              "githubuser/hauth"
license:             BSD3
author:              "Author name here"
maintainer:          "example@example.com"
copyright:           "2022 Author name here"

extra-source-files:
- README.md
- ChangeLog.md

# Metadata used when publishing your package
# synopsis:            Short description of your package
# category:            Web

# To avoid duplicated efforts in documentation and dealing with the
# complications of embedding Haddock markup inside cabal files, it is
# common to point users to the README.md file.
description:         Please see the README on GitHub at <https://github.com/githubuser/hauth#readme>

dependencies:
- base
- classy-prelude
- time
- aeson
- random


default-extensions: 
- NoImplicitPrelude 
- OverloadedStrings
- QuasiQuotes

library:
  source-dirs: src

executables:
  hauth-exe:
    main:                Main.hs
    source-dirs:         app
    ghc-options:
    - -threaded
    - -rtsopts
    - -with-rtsopts=-N
    dependencies:
    - hauth

tests:
  hauth-test:
    main:                Spec.hs
    source-dirs:         test
    ghc-options:
    - -threaded
    - -rtsopts
    - -with-rtsopts=-N
    dependencies:
    - hauth

I think that should already work for aeson and random, but you should still add those GTK dependencies:

dependencies:
- base
- classy-prelude
- time
- aeson
- random
- gi-gtk == 3.0.*
- gi-gtk-hs
- haskell-gi-base

Does that work? If not, what is the full error message you get?

Edit: Oh wait, maybe I see what is going wrong with random and aeson. Do you literally mean it says “Could not find module ‘aeson’”? Then you have probably written import aeson. That is not the correct way to do it. You see, ‘aeson’ is the package name while you can only use import with module names. You probably want to write import Data.Aeson which is the main module of the aeson package. Similarly you probably want to import System.Random from the random package.

You can see all the modules in a package on hackage, e.g. aeson and random. There you can also find documentation on how to use these packages.

1 Like

No no, I mported with

System.Random and Data.Aeson.

Still, when I hover over it or do ghci from the file directly I get these messages. Strange though, that I can use functions from these modules and the program compiles…

What are you doing exactly that causes the warnings? Are you saying stack build works correctly but some use of GHCi is giving these errors? And what are you using for hovering, VSCode + HLS?

Exactly. If I use stack ghci and test out functions, everything works and compiles. Then I once cd into the file and tried the same just using ghci. Immediatly I get these errors. Yes, VSCode + HLS. I was suspecting that there is some issue with VSCode but the error should not be present when trying to load a file into ghci…really I dont know. I am happy getting stuff done in Haskell but I am pretty fresh to all this setup stuff…

Ah, so the problem is that normal GHCi doesn’t know anything about your stack project, so in particular it doesn’t have access to the dependencies. It may be possible to just use stack ghci path/to/YourFile.hs to load it with the proper dependencies or alternatively you can try stack exec ghci -- path/to/YourFile.hs. I don’t know that much about Stack, so I’m not sure what works.

VSCode + HLS should automatically be able to detect your Stack project. I don’t know why that is not working for you.

1 Like

Me neither :joy:. Anyway, thank you so much for your time and effort. Maybe I’ll figure it out somehow. I will post the solution or what I am doing wrong once I have it sorted out.

When using HLS with stack you might need to generate a hie.yaml. The HLS docs say it’s not necessary but I’ve found it’s helped some projects Configuration — haskell-language-server 1.7.0.0 documentation.

Also, make sure with vscode that you have the folder containing stack.yaml as the root in your workspace or use multiple workspaces in vscode so that you’ve added the top level folder to vscode.

If you are still having issues, try checking the extension logs for HLS in vscode.

Ok, maybe I messed something up during installation of Haskell. So I completely removed Haskell as it was installed via GHCup. I use VSCode ++ HLS

Uninstall:
1: ghcup nuke
2: removed any ghcup lines in .bashrc
3: removed .stack, .ghc and .cabal files from my home directory
4: removed stack from /usr/local/bin directory.

Install:
1: copied and pasted “curl --proto ‘=https’ --tlsv1.2 -sSf https://get-ghcup.haskell.org | sh”
2: followed the instruction on the terminal

New Project as per User guide - The Haskell Tool Stack
1: stack new helloworld new-template
2: cd helloworld
3: stack build
4: ghc --version :: The Glorious Glasgow Haskell Compilation System, version 8.10.7
5: stack setup
6: Modify Lib.hs according to Stack User guide Adding Dependencies
7: stack build
8: Adding -text to dependencies in package.yaml

dependencies:
- base >= 4.7 && < 5
- text

9: stack build
10: adding -filepath and - containers

dependencies:
- base >= 4.7 && < 5
- text
- filepath
- containers

11: stack build
12: add -classy-prelude to package.yaml
13: stack build.
14: Edit Lib.hs to:

{-# LANGUAGE OverloadedStrings #-}

module Lib
    ( someFunc
    ) where

import qualified Data.Text.IO as T
import ClassyPrelude


someFunc :: IO ()
someFunc = putStrLn "someFunc"

Note. When hover over ClassyPrelude I get the message

Could not find module ‘ClassyPrelude’
It is not a module in the current program, or in any known package.not found

15: Add - aeson and -random to package.yaml

dependencies:
- base >= 4.7 && < 5
- text
- filepath
- containers
- classy-prelude
- aeson 
- random

16: Edit Lib.hs to:

{-# LANGUAGE OverloadedStrings #-}

module Lib
    ( someFunc
    ) where

import qualified Data.Text.IO as T
import ClassyPrelude
import System.Random  
import Data.Aeson


someFunc :: IO ()
someFunc = putStrLn "someFunc"

Same Errors:

Could not find module ‘System.Random’
It is not a module in the current program, or in any known package.not found

Could not find module ‘Data.Aeson’
Perhaps you meant Data.Version (from base-4.14.3.0)not found

17: stack ls dependencies
Output:
OneTuple 0.3.1
QuickCheck 2.14.2
StateVar 1.2.2
adjunctions 4.4.1
aeson 2.0.3.0
array 0.5.4.0
assoc 1.0.2
async 2.2.4
attoparsec 0.14.4
base 4.15.1.0
base-compat 0.11.2
base-compat-batteries 0.11.2
base-orphans 0.8.6
basic-prelude 0.7.0
bifunctors 5.5.12
binary 0.8.8.0
bytestring 0.10.12.1
chunked-data 0.3.1
classy-prelude 1.5.0.2
comonad 5.0.8
containers 0.6.4.1
contravariant 1.5.5
data-default-class 0.1.2.0
data-fix 0.3.2
deepseq 1.4.5.0
directory 1.3.6.2
distributive 0.6.2.1
dlist 1.0
dlist-instances 0.1.1.1
exceptions 0.10.4
filepath 1.4.2.1
free 5.1.9
ghc-bignum 1.1
ghc-boot-th 9.0.2
ghc-prim 0.7.0
hashable 1.3.5.0
helloworld 0.1.0.0
indexed-traversable 0.1.2
indexed-traversable-instances 0.1.1
integer-logarithms 1.0.3.1
invariant 0.5.6
kan-extensions 5.2.5
keys 3.12.3
mono-traversable 1.0.15.3
mono-traversable-instances 0.1.1.0
mtl 2.2.2
mutable-containers 0.3.4
pointed 5.0.4
pretty 1.1.3.6
primitive 0.7.3.0
process 1.6.13.2
profunctors 5.6.2
random 1.2.1.1
rts 1.0.2
say 0.1.0.1
scientific 0.3.7.0
semialign 1.2.0.1
semigroupoids 5.3.7
semigroups 0.19.2
split 0.2.3.4
splitmix 0.1.0.4
stm 2.5.0.0
stm-chans 3.0.0.6
strict 0.4.0.1
tagged 0.8.6.1
template-haskell 2.17.0.0
text 1.2.5.0
text-short 0.1.5
th-abstraction 0.4.3.0
these 1.1.1.1
time 1.9.3
time-compat 1.9.6.1
transformers 0.5.6.2
transformers-base 0.4.6
transformers-compat 0.6.6
unix 2.7.2.2
unliftio 0.2.22.0
unliftio-core 0.2.0.1
unordered-containers 0.2.17.0
uuid-types 1.0.5
vector 0.12.3.1
vector-algorithms 0.8.0.4
vector-instances 3.4
void 0.7.3
witherable 0.4.2

Am I missing something???

Now, when I cd into src, run ghci and try to load Lib.hs I get this.
1: cd src
2: ghci
3: :l Lib.hs (does not compile)

GHCi, version 8.10.7: https://www.haskell.org/ghc/  :? for help
Prelude> :l Lib.hs
[1 of 1] Compiling Lib              ( Lib.hs, interpreted )

Lib.hs:8:1: error:
    Could not find module ‘ClassyPrelude’
    Use -v (or `:set -v` in ghci) to see a list of the files searched for.
  |
8 | import ClassyPrelude
  | ^^^^^^^^^^^^^^^^^^^^
Failed, no modules loaded.
Prelude> :l Lib.hs
[1 of 1] Compiling Lib              ( Lib.hs, interpreted )

Lib.hs:9:1: error:
    Could not find module ‘System.Random’
    Use -v (or `:set -v` in ghci) to see a list of the files searched for.
  |
9 | import System.Random  
  | ^^^^^^^^^^^^^^^^^^^^

Lib.hs:10:1: error:
    Could not find module ‘Data.Aeson’
    Perhaps you meant Data.Version (from base-4.14.3.0)
    Use -v (or `:set -v` in ghci) to see a list of the files searched for.
   |
10 | import Data.Aeson
   | ^^^^^^^^^^^^^^^^^
Failed, no modules loaded.

I’d suggest checking out the HLS troubleshooting page:
https://haskell-language-server.readthedocs.io/en/latest/troubleshooting.html

They have a section about problems with multi-component stack projects that might be relevant:
https://haskell-language-server.readthedocs.io/en/latest/troubleshooting.html#problems-with-multi-component-support-using-stack

Don’t cd into src. Start GHCI with stack ghci.

2 Likes

That’s what I usually do. I just wanted to see what happens…

I guess it starts the system installed ghci, or whatever ghci found in your path, without loading anything. From the src directory you can : load source files but I doubt it will find packages that are not installed globally.

One thing I haven’t said yet but might be the missing piece. Before I reinstalled haskell with GHcup, I had a .ghc folder and some files in there. Also a file which listed all installed “packages/libraries”? ClassyPrelude was included in that list as well. So when i imported it, in some file in the src folder, I didn’t get the error message. That tells me that I somehow imported or installed it globally? Could that be the case? Thing is, I must have done it by accident, so I have no Idea how i did it :persevere:. Now when I do everything the way I described above, ClassyPrelude again gives me that “module not found” message.

Similar question here:

Just reading through your steps and I don’t see anything amiss. It is a common bug where ghci has lost sight of the project. My guesses:

When you hover to see if stuff has worked, there hasn’t been a compilation that has worked yet. Try running ‘cabal build’ of ‘stack build’ and, on success, reboot the lsp service.

Use ‘cabal repl’ or ‘stack repl’ rather than ‘ghci’. I dont know why, but this helps.

If you start from the top, and get a successful build early, track builds external to hls with stack build or ghcid, then hls will be much happier.

Hello Tony, I really appreciate your time and effort. I am now building an app with a TUI. The program compiles and runs no problems whatsoever. Still, the imports are all red and even after restarting the language server, it reverts back to red. I guess I will have to live with it for now. Generally I think trying to solve all these problems gives you a deeper understanding of how things work but it also takes away the focus on what you actually want to achieve. Tradeoffs, tradeoffs tradeoffs :joy:. I still appreciate and respect all those people who develop such programs for us to have an easier life (in their spare time? I guess).