Haskell stack script with a dependency from Github? - haskell

Is there a way to run a stack script (https://docs.haskellstack.org/en/stable/GUIDE/#script-interpreter) with a dependency from Github? I would like to run a script with the dependency somePackageFromGithub, so e.g.:
#!/usr/bin/env stack
-- stack --resolver lts-18.0 script --package somePackageFromGithub
module Main where
main :: IO ()
main = putStrLn "Hello World!"
Usually I would put this in the stack.yaml file as an extra dependency:
extra-deps:
- git: https://github.com/user/somePackageFromGithub.git
commit: "some hash"
But the stack script subcommand does not allow to specify a stack.yaml file (v.2.7.1): Ignoring override stack.yaml file for script command. Is there a way around this limitation of the stack cli interface?

Related

Is it possible to specify a `stack.yaml` file for a haskell script?

Let's say I have the following script inside my haskell project directory:
#!/usr/bin/env stack
-- stack --resolver lts-12.5 script
-- some imports here
main :: IO ()
main = do
-- some code here
I'd like to use the stack.yaml file that exists inside the project directory, in order to get the required packages, as I want to get them from a specific git commit instead of lts-12.5.
I've tried adding --stack-yaml stack.yaml after --resolver lts-12.5 but I'm getting this warning when I run the script: Ignoring override stack.yaml file for script command: stack.yaml.
Is it possible to use my stack.yaml file for the script? or is it possible to specify the git commit from which I want to get the package (like using location with commit and git inside stack.yaml)?
You can achieve this using custom snapshot files. For example, in snapshot.yaml:
resolver: ghc-8.4.3
name: has-acme-missiles
packages:
- acme-missiles-0.3
In Main.hs:
#!/usr/bin/env stack
-- stack --resolver snapshot.yaml script
import Acme.Missiles
main :: IO ()
main = launchMissiles
Results in:
$ ./Main.hs
Nuclear launch detected.

Intero : Failed to load interface for Lib

I am trying to setup my Haskero (Visual Studio Code extension that uses Intero) for my Haskell project, yet I get the following error :
app\Main.hs:3:1: error:
Failed to load interface for `Lib'
Use -v to see a list of the files searched for.
Steps to reproduce:
stack new project
cd project
stack build intero
stack exec intero
> :l app/Main.hs
app/Main.hs :
module Main where
import Lib
main :: IO ()
main = someFunc
src/Lib.hs :
module Lib
( someFunc
) where
someFunc :: IO ()
someFunc = putStrLn "someFunc"
I had similar issue occuring in Visual Studio Code.
Under the hood Haskero properly uses:
stack ghci --with-ghc intero --no-build --no-load
However Haskero assumes that the stack project is the working directory loaded to VSCode. If instead the stack project is one of the subdirectories then the same error appears in IDE, because stack command is run from that main directory. At least it's what happens currently with Haskero 1.3.1.
The solution is to always make sure that stack project is equal to working directory in VSCode.
I don't have experience with Haskero but can duplicate the problem with a plain old Intero installation on a Linux machine.
The issue is that you're invoking the Intero backend via stack exec instead of stack ghci. You would observe the same problem if you tried using stack exec ghci instead of stack ghci to invoke a usual GHC interactive session (see the documentation for stack ghci for more information).
Instead of stack exec intero, try:
stack ghci --with-ghc intero --no-build --no-load
and it should work okay.
(Note that stack exec intero actually works okay if you stack build your project first, but the interactive session is still supposed to be invoked via stack ghci.)

Force Haskell Stack build to use the Resolver Specified after Shebang

I'm trying to use haskell for scripting in the pattern specified here on the "Script interpreter" section:
https://haskell-lang.org/tutorial/stack-script
When I have comments like this in my script after the shebang:
#!/usr/bin/env stack
{- stack
--resolver nightly-2016-11-26
--install-ghc
runghc
--package http-conduit
-}
{-# LANGUAGE OverloadedStrings #-}
I know I can safely say run:
$ stack myfile.hs
And it correctly picks up the resolver version I specified I need.
However, if I also try to build my script, to have the best of both worlds in scripting for development while I figure things out, and compilation once I'm more sure...
username#myhost:~/$ stack exec -- ghc myfile.hs && ./myfile
Run from outside a project, using implicit global project config
Using resolver: lts-7.10 from implicit global project's config file: /home/username/.stack/global-project/stack.yaml
That will error out because it's using resolver 'lts-7.10' from my global config instead of 'nightly-2016-11-26' even though my comments after the shebang specify a specific, non-global resolver to use.
While I know I could run ghc above and explicitly call --resolver on the command line, I would prefer if this were picked up from what's within the file itself. Is this possible?

How to load tests in ghci with stack

I created a very simple project with stack. It contains: an executable, a library and test targets in the associated cabal file. When I load the code to ghci via stack ghci, I can't access test there, even if they are in separate module. Is there any way to use it in such a way?
Try stack ghci (your project name):(the test suite name). Then you should be able to enter main and your tests will run.
Example:
If your .cabal project file had the following values:
name: ExampleProject
...
test-suite Example-test
Then the command to run would be stack ghci ExampleProject:Example-test
(edit suggested by #Chris Stryczynski)
To watch the test and src directories so they are updated when you reload with :r, run:
stack ghci --ghci-options -isrc --ghci-options -itest ExampleProduct:Example-test

Stack interpreter option - Add an external dependency

I'm making a script with Turtle and I need a dependency from GitHub. I saw that you can add such a dependency when you make a project by putting this in your stack.yaml:
packages:
- location:
git: https://github.com/githubuser/reponame.git
commit: somecommitID
But is it possible to add it via the command line?
This is the command line used to run the script:
stack --resolver lts-3.2 --install-ghc runghc --package turtle
Edit:
These are the first lines of my script:
#!/usr/bin/env stack
-- stack --resolver lts-3.1 --install-ghc runghc --package turtle
import Turtle
...
We do not support all of stack.yaml options on the commandline. I recommend putting a stack.yaml somewhere. If you don't want to just put it in the same folder as the script, use "--stack-yaml"

Resources