Getting “Could not find module `Yesod'” when I

2019-05-25 20:21发布

问题:

I know this seems a duplicate to Could not find module `Yesod', but unlike that user, ghc-pkg list does not show Yesod in its output on my computer, they didn't seem to be using stack (I am, and I'm not sure if that means I don't need to worry about ghc-pkg list), and additionally, the answer (code) to that question did not help my situation.

The Yesod Book has an example that I've been trying to get to work for several hours now. I'll reprint it here

{-# LANGUAGE OverloadedStrings     #-}
{-# LANGUAGE QuasiQuotes           #-}
{-# LANGUAGE TemplateHaskell       #-}
{-# LANGUAGE TypeFamilies          #-}
import           Yesod

data HelloWorld = HelloWorld

mkYesod "HelloWorld" [parseRoutes|
/ HomeR GET
|]

instance Yesod HelloWorld

getHomeR :: Handler Html
getHomeR = defaultLayout [whamlet|Hello World!|]

main :: IO ()
main = warp 3000 HelloWorld

I'm using the latest 64-bit Ubuntu OS. The problem I consistently run into is that runhaskell hello-world.hs will time-and-time again return

hello-world.hs:6:18:
    Could not find module `Yesod'
    Use -v to see a list of the files searched for.

Likewise when I add a module name to the file and try loading it with stack ghci hello-world.hs. I'm using stack to build things, and I've tried many different iterations of stack new (with yesod templates), stack init, and stack build's left and right, with stack update's here and there for good measure, and even a few cabal install's, along with the entirety of the Yesod quickstart guide, all to no avail, and all within the proper directories.

When I use Yesod templates with simpleSQL, the template site loads properly, and moreover a much larger project that I'm working with, Snowdrift, runs as well (albeit as a site, using stack exec yesod devel and not a runhaskell FILE command; but still, it works, and I've tried the exact same build process for handling the hello-world.hs file above.

I feel like this problem has a simple solution that I'm missing, but I've tried and tried, and searched all over, and I just haven't come across an answer.

Thanks a bunch for taking the time to help me out.

回答1:

runhaskell hello-world.hs and ghc-pkg list will default to using the global system and user package databases (that is, the ones cabal-instal normally uses), and not the snapshot and project-specific ones used by stack (see also: Why doesn't stack add packages to the ghc package database?). You should instead use stack runghc hello-world.hs and stack exec -- ghc-pkg list. The stack commands ensure the GHC tools use the appropriate package databases (and also the appropriate versions of GHC, in case you ever need a stack setup to use a different GHC than the one installed system-wide).



回答2:

Your results may vary, but this worked for me (on 13 Jan 2018):

  • Mac OS 10.13.2 (High Sierra)
  • Searched Hackage for Yesod, found 1.4.5 at https://hackage.haskell.org/package/yesod-1.4.5/docs/Yesod.html
  • stack install yesod-1.4.5
  • Got dependency error with suggestion to add conduit-extra-1.1.13.1 to extra-deps (in ~/.stack/global/stack.yaml).
  • Tried again; got more dependency errors, with the result being that I ended up with:
  • extra-deps: ["gtk-mac-integration-0.3.4.0","conduit-extra-1.2.3.2","conduit-1.2.13","streaming-commons-0.1.18","typed-process-0.2.1.0","unliftio-core-0.1.1.0"] (You might get different recommendations; I just followed the ones I got. Note: I already had the gtk-mac-integration extra-dep before I started).
  • successfully stack-installed yesod-1.4.5
  • stack runghc helloworld.hs
  • success!

In hindsight, it should seem obvious that module "Yesod" would not be found it it had not yet been installed. I usually work within a stack-created Haskell project, and in that case would have taken the usual steps to ensure the dependency was present. I usually don't run applications with "stack runghc".