Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Watch command should not spawn more than one tsc watch #5391

Merged
merged 1 commit into from
Mar 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 29 additions & 10 deletions yarn-project/watch.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env bash
set -eu
set -u

DEBOUNCE_DURATION=3 # Set a high duration for debounce since nargo build may pause for a long time during a compilation
INOTIFY_EVENTS="modify,create,delete,move"
Expand All @@ -23,33 +23,52 @@ debounce() {

# Start typescript watch process in the background and store process ID in a file
start_tsc_watch() {
yarn tsc -b tsconfig.json --watch &
local tsc_bin=$(yarn bin tsc)
$tsc_bin -b tsconfig.json --watch &
TSC_PID=$!
echo "$TSC_PID" > .tsc.pid
}

# Stops the typescript watch process
stop_tsc_watch() {
local tsc_pid=$(cat ".tsc.pid");
kill $tsc_pid || true
if [ -f .tsc.pid ]; then
echo "Stopping tsc watch..."
local tsc_pid=$(cat ".tsc.pid");
echo KILLING $tsc_pid
kill $tsc_pid
fi
}

# Kill typescript, run a yarn generate, and restart typescript
run_generate() {
echo "Change detected at $1"
# If already generating something, then try again in a few seconds
if [ -f .generating.lock ]; then
debounce run_generate $1
return
fi
# Pause ts watch to generate code and acquire a lock
echo "LOCKED" > .generating.lock
echo "Change detected at $1..."
stop_tsc_watch
FORCE_COLOR=true yarn workspaces foreach --parallel --topological-dev --verbose run generate:$1
echo "Generate complete, restarting typescript..."
if FORCE_COLOR=true yarn workspaces foreach --parallel --topological-dev --verbose run generate:$1; then
echo "Generate complete, restarting tsc watch..."
else
echo "Generate failed, restarting tsc watch..."
fi
# Restart tsc watch and release lock
sleep 3
start_tsc_watch
rm .generating.lock
}

# Remove all temp files with process or run ids on exit
cleanup() {
rm .tsc.pid || true
rm .debounce-* || true
rm -f .tsc.pid || true
rm -f .debounce-* || true
rm -f .generating.lock || true
}
trap cleanup EXIT
cleanup

# Start tsc watch in background
start_tsc_watch
Expand All @@ -68,7 +87,7 @@ while true; do
debounce run_generate "l1-contracts"
;;
*)
echo "Change at $folder not matched with any project"
echo "Error: change at $folder not matched with any project"
exit 1
;;
esac
Expand Down
Loading