19

Up until recently, I was executing this beauty to build + run a project with stack:

stack build && .stack-work/install/x86_64-linux/lts-4.1/7.10.3/bin/<project-name>

I was told on IRC that this can be simplified to

stack build && stack exec <project-name>

Can this be simplified even more, to

stack run

or at least

stack run <project-name>

?

If I recall correctly this was possible with cabal run.

Edit:

@haoformayor's comment is getting close:

alias b='stack build --fast --ghc-options="-Wall" && stack exec'

Although this still needs the project name, right?

I've also started to get close with

function stack-run () { stack build && stack exec `basename "$PWD"` }

Although this only works if the project name matches with the folder name. Maybe we can query cabal/stack for the first executable entry in the .cabal file? Or Maybe we could do it with sed...

Wizek
  • 4,854
  • 2
  • 25
  • 52
  • I don't believe there is an equivalent command. I use shell aliases to do something similar. Something like `alias b='stack build --fast --ghc-options="-Wall" && stack exec'` – hao Jan 17 '16 at 21:13
  • @haoformayor This is getting close to what I am looking for. One sec, I'll extend my question a bit. – Wizek Jan 17 '16 at 21:58
  • 1
    it's fairly easy to grab the executable name if you need it: `grep '^executable' *.cabal | cut -d ' ' -f 2 | head -n 1` – hao Jan 17 '16 at 22:20
  • There is also `stack runghc ./src/Main.hs` – epsilonhalbe Jan 18 '16 at 00:04
  • 1
    This is an old question, but: there is now a real `stack run` command in the latest release! – bradrn Mar 10 '19 at 20:48

3 Answers3

13

As it's mentioned here http://docs.haskellstack.org/en/stable/README.html#quick-start-guide, you can use stack exec my-project-exe where my-project-exe is the name of the executable in your .cabal file.

  • it's also possible to pass some command line arguments: `stack exec -- ` – max taldykin Jan 17 '16 at 19:15
  • 7
    Well, I am a bit confused by this answer. @Govind How is your answer different than what I mentioned in my question about `stack exec`? I'm looking for a command that would `stack build` + `stack exec` a project, similarly to how I remember `cabal run` doing it. – Wizek Jan 17 '16 at 20:38
11

You can use --exec to tell stack what program should be run after a successful built:

stack build --exec <executable-name>

You can also specify arguments for the executable, e.g.

stack unpack pandoc && cd pandoc*
stack build --exec "pandoc --version"

That's probably the closest you'll get compared to cabal run, since both stack exec and the --exec flag need an executable name. The cleanest variant, however, would be an additional stack-run command, that does stack build --exec <first-executable in .cabal>. It could be worth a feature request on the project's GitHub issue tracker.

Zeta
  • 103,620
  • 13
  • 194
  • 236
  • 1
    By the way, if you have both cabal _and_ stack installed, you could also run `stack build --exec "cabal run"`. But that's somewhat contrived. – Zeta Jan 17 '16 at 22:10
11

I've had quite a good experience using:

https://hackage.haskell.org/package/stack-run


Edit 2018-04-05: Relevant stack issue.


Old answer:

This is what I ended up doing for now.

#/usr/bin/env sh

stack build && stack exec `basename "$PWD"` "$@"

I've put the following into a file named stack-run under my $PATH. ~/.local/bin/stack-run in my case.

Which allows me to

$ stack-run

in any directory, and even

$ stack run

Since in almost all of my projects the executable of the project bears the same name as the folder, this works. But I hope to extend it with support for differing names as well.


Edit 2016-09-26: I've also found this, but haven't given it a try yet: https://hackage.haskell.org/package/stack-run

Wizek
  • 4,854
  • 2
  • 25
  • 52