Skip to content

Commit 48ce386

Browse files
Keep watch alive after rebuild errors
Signed-off-by: Florian Hammerschmidt <florianh89@gmail.com>
1 parent 7132187 commit 48ce386

3 files changed

Lines changed: 113 additions & 5 deletions

File tree

‎rewatch/src/watcher.rs‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ async fn async_watch(
486486
let timing_total = Instant::now();
487487
// Reinitialization runs cleanup for previous build artifacts, so full rebuilds need
488488
// the same build lock boundary as regular `rescript build`.
489-
let result = build::with_build_lock(path, || {
489+
let result = build::with_build_lock(path, || -> Result<_> {
490490
let mut next_build_state = build::initialize_build(
491491
None,
492492
filter,
@@ -498,7 +498,7 @@ async fn async_watch(
498498
features.clone(),
499499
SourceMapCommand::Watch,
500500
)
501-
.expect("Could not initialize build");
501+
.context("Could not initialize build")?;
502502

503503
// Full rebuilds can be triggered by editor atomic saves that surface as rename events.
504504
// Preserve warning state for unchanged modules so their warnings are re-emitted after the
@@ -521,10 +521,10 @@ async fn async_watch(
521521
plain_output,
522522
);
523523
build::write_build_ninja(&build_state);
524-
result
524+
Ok(result)
525525
});
526526
match result {
527-
Ok(result) => {
527+
Ok(Ok(result)) => {
528528
finish_successful_watch_compile(
529529
after_build.clone(),
530530
timing_total,
@@ -535,7 +535,13 @@ async fn async_watch(
535535
result,
536536
);
537537
}
538-
Err(_) => {
538+
Ok(Err(_)) => {
539+
if should_clear_screen(clear_screen, show_progress, plain_output) {
540+
print_build_failed_footer();
541+
}
542+
}
543+
Err(error) => {
544+
eprintln!("{error:#}");
539545
if should_clear_screen(clear_screen, show_progress, plain_output) {
540546
print_build_failed_footer();
541547
}

‎rewatch/tests/suite.sh‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ fi
147147
./watch/04-watch-config-change.sh &&
148148
./watch/05-watch-ignores-non-source.sh &&
149149
./watch/06-watch-missing-source-folder.sh &&
150+
./watch/07-watch-recovers-from-invalid-config.sh &&
150151

151152
# Lock tests
152153
./lock/01-lock-when-watching.sh &&
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#!/bin/bash
2+
cd $(dirname $0)
3+
source "../utils.sh"
4+
cd ../../testrepo
5+
6+
bold "Test: Watcher recovers from an invalid config change"
7+
8+
wait_for_pattern() {
9+
local file="$1" pattern="$2" timeout="${3:-30}"
10+
while [ "$timeout" -gt 0 ]; do
11+
grep -q "$pattern" "$file" 2> /dev/null && return 0
12+
sleep 1
13+
timeout=$((timeout - 1))
14+
done
15+
return 1
16+
}
17+
18+
wait_for_pattern_count() {
19+
local file="$1" pattern="$2" expected="$3" timeout="${4:-30}"
20+
while [ "$timeout" -gt 0 ]; do
21+
local count
22+
count=$(grep -c "$pattern" "$file" 2> /dev/null || true)
23+
[ "$count" -ge "$expected" ] && return 0
24+
sleep 1
25+
timeout=$((timeout - 1))
26+
done
27+
return 1
28+
}
29+
30+
rewatch clean > /dev/null 2>&1
31+
cp rescript.json rescript.json.watch-recovery.bak
32+
rewatch_bg watch > rewatch.log 2>&1 &
33+
34+
if ! wait_for_file "./src/Test.mjs" 20 || ! wait_for_file "lib/watch.lock" 20; then
35+
error "Initial watch build did not complete"
36+
cat rewatch.log
37+
mv rescript.json.watch-recovery.bak rescript.json
38+
exit_watcher
39+
exit 1
40+
fi
41+
42+
initial_completed=$(grep -c "Finished compilation" rewatch.log 2> /dev/null || true)
43+
printf '{"name":' > rescript.json
44+
45+
if ! wait_for_pattern rewatch.log "Could not initialize build" 30; then
46+
error "Watcher did not report the invalid config"
47+
cat rewatch.log
48+
mv rescript.json.watch-recovery.bak rescript.json
49+
exit_watcher
50+
exit 1
51+
fi
52+
53+
watcher_pid=$(cat lib/watch.lock)
54+
if ! kill -0 "$watcher_pid" 2> /dev/null; then
55+
error "Watcher exited after the invalid config"
56+
cat rewatch.log
57+
mv rescript.json.watch-recovery.bak rescript.json
58+
exit 1
59+
fi
60+
61+
node -e '
62+
const fs = require("fs");
63+
const config = JSON.parse(fs.readFileSync(process.argv[1], "utf8"));
64+
config.suffix = ".recovered.mjs";
65+
fs.writeFileSync(process.argv[2], `${JSON.stringify(config, null, 2)}\n`);
66+
' rescript.json.watch-recovery.bak rescript.json
67+
68+
expected_completed=$((initial_completed + 1))
69+
if ! wait_for_pattern_count rewatch.log "Finished compilation" "$expected_completed" 30; then
70+
error "Watcher did not recover after restoring a valid config"
71+
cat rewatch.log
72+
mv rescript.json.watch-recovery.bak rescript.json
73+
exit_watcher
74+
exit 1
75+
fi
76+
77+
echo '// watch-recovery-test' >> src/Test.res
78+
if ! wait_for_file "./src/Test.recovered.mjs" 20; then
79+
error "Recovered watcher did not compile with the restored config"
80+
cat rewatch.log
81+
mv rescript.json.watch-recovery.bak rescript.json
82+
git checkout -- src/Test.res
83+
exit_watcher
84+
exit 1
85+
fi
86+
87+
exit_watcher
88+
rewatch clean > /dev/null 2>&1
89+
mv rescript.json.watch-recovery.bak rescript.json
90+
git checkout -- src/Test.res
91+
rm -f rewatch.log
92+
rewatch build > /dev/null 2>&1
93+
94+
if git diff --exit-code . > /dev/null 2>&1 && [ -z "$(git ls-files --others --exclude-standard .)" ]; then
95+
success "Watcher recovered from invalid config"
96+
else
97+
error "Watcher recovery test left repository changes"
98+
git diff .
99+
git ls-files --others --exclude-standard .
100+
exit 1
101+
fi

0 commit comments

Comments
 (0)