Find the package a Haskell module belongs to - haskell

I'm new to Haskell stack and wondering how to find out the name of the package that contains a particular module.
Currently, I want to use Data.Tuple.Extra(fst3) ( https://hackage.haskell.org/package/extra-1.7.9/docs/Data-Tuple-Extra.html ) and want to know what I should write below
$ stack install ????
I've already installed the tuple package, which, however, doesn't seem to include the Extra part.
All the Internet resources about the installation of a package I've found so far say something along the lines of "To use Blahblah.Anything.Something, you need to install the foofoo package" . . . How can one know? I searched Stackage but it shows only the documentation of Data.Tuple.Extra and I still fail to find the name of the package.
Edit: As K.A.Buhr notes in her/his answer, stack install is the wrong command for the above case. Use stack build instead.

When browsing package documentation in Hackage, the top-left portion of the page header will always give the package, version number, and description. On the page you link, it's here:
You can also use the "Contents" link in the top-right to go to the main page for the extra package, which gives its full list of modules, licensing, links to the package home page and bug tracker, and so on.
As a side note, stack install extra is technically the wrong command to "install" this package. If you want to make the extra package available for use within the Stack global project, the correct command is stack build extra. If you want to use extra within a stack project, then you want to add extra to the dependencies in your package's xxx.cabal or package.yaml file instead and run stack build (no arguments) to build and install it for use in your project.
In contrast, the stack install command is equivalent to stack build --copy-bins which copies any executables in the package to ~/.local/bin so they'll be in your path. See the Stack docs. It's intended to be used for installing programs written in Haskell that are distributed via Stack, so you can do stack install hlint to install the hlint linter, for example.
In this case, because the extra package has no executables, stack install extra and stack build extra will do the same thing, but it's better to get into the habit of using stack build when you aren't intending to install any package binaries, to avoid surprises.

Related

Which stackage snapshot does stack install use?

Trying to run
stack install git-mediate
(per git-mediate's instructions)
I get an error message regarding dependent package versions:
Error: While constructing the build plan, the following exceptions were encountered:
In the dependencies for git-mediate-1.0.8:
Diff-0.3.4 from stack configuration does not match >=0.4 (latest matching version is 0.4.0)
needed since git-mediate is a build target.
Some different approaches to resolving this:
* Set 'allow-newer: true' in /Users/yairchu/.stack/config.yaml to ignore all version constraints and build anyway.
* Recommended action: try adding the following to your extra-deps in /Users/yairchu/.stack/global-project/stack.yaml:
- Diff-0.4.0#sha256:b5cfbeed498f555a18774ffd549bbeff7a24bdfe5984154dcfc9f4328a3c2847,1275
Plan construction failed.
It's odd that the stack configuration has Diff-0.3.4, as currently both LTS and nightly stackage snapshots currently contain Diff-0.4.0 (lts-16.8 and nightly-2020-10-13).
What is this stack configuration thing and why is it pinned to old versions of libraries?
stack install is an alias for stack build --copy-bins, so it's really just stack build plus the additional step of copying the built executables to ~/.local/bin.
So, the real question is "how does stack build decide what resolver to use?" Well, if you provide it on the command line, it uses that one, as in:
stack install --resolver lts-16.18 git-mediate
If you don't give an explicit resolver, the default depends on where the build command is run. If you run it inside a stack project, it'll default to the resolver specified in the project's stack.yaml file. For example:
stack new --resolver lts-16.0 exampleproject # create project with lts-16.0 resolver
cd exampleproject
stack build git-mediate # this will build git-mediate using lts-16.0
If you build it outside of any project, then it uses the global project setting, which will be whatever resolver is set in ~/.stack/global-project/stack.yaml, as mentioned in the comments / other answer.
Note that the stack install alias will always copy the executable into the "global" ~/.local/bin, regardless of where it was built. So, if you run stack install git-mediate in the exampleproject directory, as above, you'll get the version built with lts-16.0 as the globally installed version.
Soooo... be careful where you run stack install!
Note that, with respect to git-mediate specfically, there was recently a buggy version published to Stackage, as documented here. The error message is slightly different than the one you got, but the underlying problem might have been the same. So, it's possible that just running stack update without having to modify the resolver setting would work to fix your build problem, if you haven't already fixed it.
stack is implicitly using it's "global project" defined in ~/.stack/global-project/stack.yaml. To control the stackage snapshot being used this file can be edited (or just deleted to use the latest LTS)

Recommended approach to use Stack as global package manager

I would like to install some Haskell libraries globally, for example hindent which is used by my editor's Haskell integration. What is the recommended way to do this?
I thought that stack install hindent was the correct way to do this. However, then I wanted to update my packages and found that there was no way to do this. According to the GitHub issue report I found,
stack is concerned with managing a local build sandbox for a project. It isn't intended to be a global package manager.
There appear to be workarounds such as maintaining a dummy project with artificial dependencies on the packages I would like installed. This sounds like a terrible hack, and I have been unable to find any official documentation on what approach should actually be taken.
Installing Haskell packages using my system package manager (Homebrew) is not an option since they are not packaged.
I would have opened an issue report against Stack, however the contribution guidelines requested that I instead ask a question here under the haskell-stack tag.
Well, stack install in any project will install to ~/.local/bin therefore making whatever executable you install be globally accessible.
The global project is used when running stack without a project, it is located in ~/.stack/global-project/stack.yaml.
If you want all of your globally accessible tools to have the same dependencies (perhaps to ensure that the ghc version matches or something), then you could make a project intended to build all of these tools. It's up to you whether or not it is the "global project" - there's not much special about it, it's just a default if you run stack and aren't in a project.
In order to record "what haskell executables do I want installed globally", you might consider creating a shell file like
#!/bin/sh
stack install hindent
And then running this whenever you change the versions of the installed tools.
Also, for tools like intero that need to match the ghc version, you can do stack install --copy-compiler-tool intero, and it will be available on the PATH when stack is used with that ghc version.

Local install packages with Stack for use with runhaskell

I am developing many small haskell programs that I indent to use as script files. My ideal use-case is to stack runhaskell one-of-the-scripts.hs to run them.
Some of the files use dependencies on packages in hackage, such as regex-posix. Therefore I get an error message saying
Could not find module `Text.Regex.Posix'
Use -v to see a list of the files searched for.
|
2 | import Text.Regex.Posix
| ^^^^^^^^^^^^^^^^^^^^^^^
I have considered two options:
Use a cabal package and specify dependencies therein. I don't like this solution since it (by what I understand) is used in a workflow where I always compile my code. I would like to use stack runhaskell instead!
Ditch stack and use cabal straight up instead, as suggested in an answer to a similar question here on SO: https://stackoverflow.com/a/13485987/4050510. I dont like this, since I would like to have all my dependencies listed in some file explicitly.
What is the simplest way to install packages from hackage locally, and have then available for stack runhaskell ?
You probably want to use the script interpreter. Sorry over concision on the answer but I'm on my phone. Here's the docs for it: https://docs.haskellstack.org/en/stable/GUIDE/#script-interpreter
You could also just do "stack build regex-posix", and then the package will be available to runhaskell. However, this is more error prone because no package hiding is used so it's trickier to tell what the script actually depends on

Is there a declarative way to specify packages to be installed into global-project using Haskell stack?

Stack allows global installation of packages using stack install <packagename> outside of a project directory.
Stack also has a ~/.stack/global-project/stack.yaml file that allows configuration of the global-project. However I can't see a way to "install" packages declaratively. The extra-deps and packages key in the yaml file don't seem to work for this method.
Instead I just have to run stack install <...> <...> each time the snapshot version gets updated for my global-project.

Installing local package with Stack

Is it possible to install package from sources with something similar to stack build package-name? (latter works with packages on Stackage, but not with custom ones)
Um, stack build (within the source directory)?
Stack doesn't really have a notion of installing libraries though, it only installs executables. To “install” locally-sourced packages, you need to specify what for you want them installed: add them as dependencies to another project, via a location: field in the packages: field in that project's stack.yaml file.
That's arguably sensible since, one might say, there's nothing you can do with an installed library except invoking it in another Haskell project (or in a REPL, which you can get with stack ghci). I personally don't hold with that though, I like actually being able to say install that library now. Which is one of the reasons I have always stuck to good old cabal-install rather than Stack. With that, you can just
cabal install
from within the source directory.
Cabal-install has often been criticised: its local installs can easily get out of sync and then you have weird dependency conflicts and need to rebuild lots of stuff. I never found this that much of a problem, and anyway this has been adressed in recent Cabal through Nix-style builds, which never produce conflicts.

Resources