Stack: Make stack kill executed process when running `stack build --file-watch --exec "foo"`

Created on 9 Feb 2016  ·  6Comments  ·  Source: commercialhaskell/stack

When I am programming a long running application (like a web api), I'd like to have a feature where the program that has been executed after building is killed when a source code change is found.

For example, I am building an api using Servant and I would like to run it like this:

$ stack build --file-watch --exec "stack exec -- my-api"

Everytime I change one of my source files, it would be nice if stack automatically killed and restarted my-api.

Currently stack just waits until my-api is finished running (which never happens).

help wanted enhancement

All 6 comments

Something like that might be the domain of stack-run. That said, I wouldn't mind having something like stack run, or something similar to yesod devel built into stack. It'd particularly be great if it could optionally run in the interpreter, so that you get blazing fast reloads of the app.

Did anyone get started on a PR for this?

@JoeMShanahan I have not.

It would be really nice to have as a feature though.

You can get this behavior using pkill:

$ stack build --file-watch --exec "bash -c \"pkill my-api; stack exec my-api &\""

@JoeMShanahan, @cdepillabout please indicate whether that resolves the issue. If so, would one of you kindly add it to the documentation?

If it doesn't resolve the issue, I'll move it to the wishlist.

@dbaynard I haven't tried this, but it looks like it would solve the issue.

Recently, I've been using ghcid to do this:

$ ghcid --command 'stack ghci' --test 'defaultMain' --warnings

ghcid is used to automatically quickly recompile your project by loading it in GHCi. I find that this normally gets recompilation times quicker than just using stack --build --file-watch.

The argument to --test is a Haskell function within your codebase that is run every time all the code compiles correctly. Generally, it should have the type IO (). Since I often work on web apps, it can be used to re-run your web application after every recompilation cycle. This works quite well.

The --warnings flag is used to force the defaultMain function to be run even if there are GHC warnings as a result of recompiling everything.

Was this page helpful?
0 / 5 - 0 ratings