2

I installed package with stack install but I cannot import the package unless I use stack ghc or stack ghci ...

The problem is I have installed atom haskell ide, that uses ghc-mod backend, which also doesn't see packages installed by stack!

ErikR
  • 51,541
  • 9
  • 73
  • 124
Ford O.
  • 1,394
  • 8
  • 25
  • I'm not particularly familiar with `ghc-mod` but I'm certain it provides some way to configure the `ghc` command which it uses - change this to `stack ghc --` or `stack ghc --stack-yaml --` if you want to use a particular environment (not the global). – user2407038 Aug 13 '16 at 14:48
  • Are you working with a stack project (i.e. with both a .cabal and stack.yaml file)? – ErikR Aug 13 '16 at 15:29
  • @ErikR I am working on my personal project that won't be distributed anywhere. I am new to haskell and I don't fully understand what's the difference between stack and cabal, so I am not sure what are you asking for... – Ford O. Aug 13 '16 at 16:41
  • Two questions: Do you have a `(project-name).cabal` file in your project directory? Do you have a `stack.yaml` file in your project directory? – ErikR Aug 13 '16 at 16:44
  • It's possible that you don't have either of those files in your project directory - maybe you are just editing a single file - I just need to know what your directory looks like in order to determine what's wrong with ghc-mod. – ErikR Aug 13 '16 at 16:49
  • Also - which platform are you using Atom on? OSX, Linux, Windows? – ErikR Aug 13 '16 at 16:51
  • @ErikR I have none of them in my directory. I use Ubuntu. – Ford O. Aug 13 '16 at 17:05
  • Ok - so apparently you are not working with a stack / cabal project. There may be a way to get ghc-mod to work with just a single file and specify a resolver (so that you can use all of the packages in a resolver set), but I'm not aware of it. – ErikR Aug 13 '16 at 17:35
  • 1
    `stack install` is really for installing executables into your environment, not making packages available to the compiler. You should consult the [stack manual](https://docs.haskellstack.org/en/stable/README/#quick-start-guide) for how to start a new project using stack. – R B Aug 13 '16 at 17:41
  • So I should install the package again trough cabal ? – Ford O. Aug 13 '16 at 18:04
  • 1
    Maybe, but what you should really do is read a manual and start a project the usual way so as to take advantage of the features your build tool provides you. – R B Aug 13 '16 at 18:14
  • 1
    This is a pretty good tutorial on using stack to build a project: http://seanhess.github.io/2015/08/04/practical-haskell-getting-started.html However, instead of `lts-3.1` substitute a more recent resolver - like the one you are currently using. – ErikR Aug 13 '16 at 19:12
  • Thanks, diving into Haskell is pretty hard on its own, so any help in form of tutorial is welcomed! – Ford O. Aug 13 '16 at 19:15
  • I would say get comfortable with using `stack` from the command line - e.g. `stack new ...`, `stack build`, `stack repl`. The main difference when using a cabal / stack project is having to declare your dependencies in the `.cabal` file. – ErikR Aug 13 '16 at 21:30
  • Then to get ghc-mod working with Atom have a look at https://github.com/erantapaa/atom-haskell-scripts and let me know if you have any problems. – ErikR Aug 13 '16 at 21:37

1 Answers1

1

You'll want to ensure that you have a ghc-mod that is both recent and built by the same version of ghc as the one your project is using. Do this by installing with stack (using the same resolver as your project). e.g.

$ stack install --resolver lts-6.11 ghc-mod
$ which ghc-mod
/Users/steshaw/.local/bin/ghc-mod
$ ghc-mod --version
ghc-mod version 5.5.0.0 compiled by GHC 7.10.3

To test this, perhaps set up an example project with stack new.

$ stack new example
$ cd example

Change example.cabal to include, say, the HTTP package for the example-exe.

e.g.

executable example-exe
  hs-source-dirs:      app
  main-is:             Main.hs
  ghc-options:         -threaded -rtsopts -with-rtsopts=-N
  build-depends:       base
                     , example
                     , HTTP
  default-language:    Haskell2010

Build with stack to install the HTTP package.

$ stack build

Now start Atom.

$ atom .

You should find that the imports from HTTP are visible within Atom.

HTTP imports

Steven Shaw
  • 6,063
  • 3
  • 33
  • 44
  • A bit off topic: What is the easiest way to profile with stack? Until now I have compiled everything with plain ghc. So I wanted to try it now the stack way - but when I use `stack build --profile` and `stack exec example +RTS -p` I get an error complaining I didn't compile with profiling enabled. – Ford O. Aug 14 '16 at 15:52
  • 1
    You need `stack exec -- example +RTS -p` http://stackoverflow.com/a/34678897/482382 – Steven Shaw Aug 14 '16 at 23:24
  • I'd be happy to look into if you ask a (top-level) question :) – Steven Shaw Aug 15 '16 at 10:58
  • Hi, what if I do not have an example.cabal? I opened a file with atom that is not a part of a stack project and I have experienced trouble with System.Random. What should I do? – Noam Oct 10 '17 at 22:29