[Haskell-cafe] Using stack outside the project box

Travis Cardwell travis.cardwell at extrema.is
Mon Jun 7 23:45:39 UTC 2021

Sorry to hear that it is still not working for you yet.

On Tue, Jun 8, 2021 at 6:22 AM Galaxy Being wrote:
> I installed stack on my Windows computer using chocolatey, which went
> smoothly. But I had nothing anywhere I could find. On a hunch, at the
> prompt I tried to run stack ghci, then it started downloading and
> installing stuff. Good. I then was able to track down an example of a
> stack.yaml in the ...stack/global-project directory. My whole
> motivation was to see an actual global stack.yaml in the wild, and not
> have to guess-and-test from the stack docs.

Stack provides functionality to initialize a project with a "template"
so that you do not have to start from scratch.  For example, if you want
to start a new project called "HaskellTest" in your `~/Dropbox/org`
directory, run the following commands:

    $ cd ~/Dropbox/org
    $ stack new HaskellTest

The default template provides you with a `stack.yaml` file that includes
documentation and examples.  The template also provides you with a
`package.yaml` file (used to generate the `.cabal` file using hpack)
that has a library, executable, and test suite configured, as well as
stub source code and various other files.

Note that in cases where you already have a Haskell project (with a
`.cabal` or `package.yaml` file), the `stack init` command can be used
to add just the `stack.yaml` to the existing project.

> Good. I duplicated
> packages: []
> resolver: lts-17.15
> on my Ubuntu 21.04 in the .stack/global-project/stack.yaml.

Note that this `stack.yaml` file is for the "global-project" as it does
not have any packages configured.  The `stack.yaml` file for any (other)
project should list at least one package.

> Tried running stack ghci from the Ubuntu prompt, and, similarly, it
> started installing lots of stuff. (The resolver version I replaced was
> lts-17.08. Why that old I don't know.).

The resolver snapshot in the "global-project" is set when you first run
Stack.  After that, it is up to you to update it *if/when* you would
like to use a different snapshot.

Similarly, the resolver snapshot for a project `stack.yaml` is set when
you run `stack new` or `stack init`, and then it is up to you to
maintain it as desired.

> But then I got a cryptic error as it seemed to clash or not like
> something it saw in some obscure github directory I had cloned ages
> ago with Haskell example code. I deleted everything in the github
> directory that looked stack/cabal-ish and tried stack ghci again. Now
> it says
> Warning (added by new or init): Some packages were found to have names conflicting with others and have been commented out in the packages section.
> Warning (added by new or init): Some packages were found to be incompatible with the resolver and have been left commented out in the packages section.
> You can omit this message by removing it from stack.yaml
> Stack looks for packages in the directories configured in
> the 'packages' and 'extra-deps' fields defined in your stack.yaml
> The current entry points to /home/galaxybeing/Dropbox/org/HaskellRoad/,
> but no .cabal or package.yaml file could be found there.
> Yes, that was the directory I purged. My
> .stack/global-project/stack.yaml contains only
> packages: []
> resolver: lts-17.15

These errors indicate that `~/.stack/global-project/stack.yaml` is *not*
being used.  It is likely that you are running `stack` commands in a
directory that contains a `stack.yaml` file which references these other
directories, and that file is being used.  Try (re)moving that
`stack.yaml` file and trying again.  Do the errors persist?

Once you (re)move the project `stack.yaml` configuration, the
`stack ghci` will use the `~/.stack/global-project/stack.yaml`
configuration.  With the above content, the LTS 17.15 resolver will be
used, with no packages configured.  As explained in my previous email,
this does *not* expose libraries to GHCi.  If you want to expose
libraries in the global project, you need to follow the instructions I
gave previously for configuring the global project.

> Not sure how to proceed. Seeing how that vast majority of Haskell
> intro books don't use projects, just install, start ghci, load code at
> the REPL prompt, I'd really like to nail this "global", non-project
> stack down.

Indeed.  Installation is tricky, particularly across multiple operating
systems.  Most book authors avoid writing about it, as writing about it
in sufficient detail would likely fill a book itself.  Most beginner
books even stick with the `base` package so that readers do not even
have to worry about packages.

Global installation of packages leads to a *lot* of trouble when you get
to more complex development.  When I first learned Haskell, I would
occasionally completely remove my package database when I got into a
situation that was difficult to resolve.  The situation improved a lot
when Cabal introduced sandbox features, allowing dependencies to be
managed separately per project.  Stack is project-based for the same
reason: allowing each project to use separate sets of packages makes
things a lot easier.  The "global-project" is called "global" because
it is used when outside of any other Haskell project (determined by the
existence of a `stack.yaml` file in the current working directory), but
note that it is actually a project.  Following the instructions that I
wrote in my previous email, you can configure Stack to work in the way
that you want *without* running into the aforementioned trouble even
when you start to develop other projects.

Good luck!


More information about the Haskell-Cafe mailing list