Wizek
Wizek

Reputation: 4993

Is there a `stack run` similar to `cabal run`?

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...

Upvotes: 19

Views: 9996

Answers (3)

Wizek
Wizek

Reputation: 4993

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

Upvotes: 11

Zeta
Zeta

Reputation: 105935

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.

Upvotes: 11

Govind Krishna Joshi
Govind Krishna Joshi

Reputation: 139

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.

Upvotes: 13

Related Questions