0

The Script

I have a short bash script that uses inotifywait to restart a ghci repl on a given file:

#!/bin/sh

$(while inotifywait -e close_write $1; do pkill ghc; done) &

while inotifywait -e close_write $1
do 
    sleep 2
    stack ghci $1    
done

Explanation

This line:

$(while inotifywait -e close_write $1; do pkill ghc; done) &

takes care of stopping ghc whenever the file is saved.

This section:

while inotifywait -e close_write $1
do 
    sleep 2
    stack ghci $1    
done

takes care of starting ghc whenever the file is saved (after a 2 second delay).

Trying it out

Given the following Haskell file loaded in vscode:

abc = 123

launch the script as follows in a terminal window:

$ ./inotifywait-stack-ghci.sh abc.hs

If you save the file twice (see issue below) the repl should restart.

The issue

The script pretty much works! There's one issue however: the repl only reloads on every second file save. So basically, to get the repl to reload, I have to save the file twice.

Has anyone seen this sort of thing before? Any suggestions for resolving it?

Notes

  • I've tagged this post with inotify, but also with haskell in case the issue is somehow idiosyncratic to ghci.

  • The script works well when the file is edited in vscode. Vim and Emacs don't seem to work well with it due to how they save their files.

  • The script works under the assumption that you only have one copy of ghc running at a time.

  • I've been testing this in Ubuntu 20.04.2 under WSL.

dharmatech
  • 8,979
  • 8
  • 42
  • 88
  • 1
    Have you ever seen [ghcid](https://github.com/ndmitchell/ghcid)? It's a very nice little program, similar to what you're trying to do. – Ari Fordsham Aug 01 '21 at 14:53
  • @AriFordsham, yup! Folks on this [reddit thread](https://www.reddit.com/r/haskell/comments/orgjz2/automatically_reloading_ghci_when_a_file_changes/) suggested that. However, it seems that `ghcid` doesn't run a repl, which the above script aims to do. – dharmatech Aug 01 '21 at 20:56

0 Answers0