inotifywait - Only restarting ghci on every second save

108 Views Asked by At

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.

0

There are 0 best solutions below