Skip to content

Commit 3377441

Browse files
Keep watcher running after full rebuild initialization errors
Signed-off-by: Florian Hammerschmidt <florianh89@gmail.com>
1 parent 5f2f1b7 commit 3377441

4 files changed

Lines changed: 116 additions & 5 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
#### :bug: Bug fix
2525

26+
- Keep watch mode running after full rebuild initialization fails so it can recover after a later valid change. https://github.com/rescript-lang/rescript/pull/8641
2627
- Fix compiler crashes when wrapping values in `Some` or passing optional arguments whose types come from a transitive dependency. https://github.com/rescript-lang/rescript/pull/8719
2728
- Fix unused-pattern warnings for variant spreads reported without a location. https://github.com/rescript-lang/rescript/pull/8744
2829
- Keep `@warning("-4")` on `if` expressions when formatting; the formatter dropped it. https://github.com/rescript-lang/rescript/pull/8741

‎rewatch/src/watcher.rs‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@ async fn async_watch(
569569
let timing_total = Instant::now();
570570
// Reinitialization runs cleanup for previous build artifacts, so full rebuilds need
571571
// the same build lock boundary as regular `rescript build`.
572-
let result = build::with_build_lock(path, || {
572+
let result = build::with_build_lock(path, || -> Result<_> {
573573
let mut next_build_state = build::initialize_build(
574574
None,
575575
filter,
@@ -581,7 +581,7 @@ async fn async_watch(
581581
features.clone(),
582582
SourceMapCommand::Watch,
583583
)
584-
.expect("Could not initialize build");
584+
.context("Could not initialize build")?;
585585

586586
// Full rebuilds can be triggered by editor atomic saves that surface as rename events.
587587
// Preserve warnings and blocked dirty modules when fresh state replaces the previous one.
@@ -603,10 +603,10 @@ async fn async_watch(
603603
plain_output,
604604
);
605605
build::write_build_ninja(&build_state);
606-
result
606+
Ok(result)
607607
});
608608
match result {
609-
Ok(result) => {
609+
Ok(Ok(result)) => {
610610
finish_successful_watch_compile(
611611
after_build.clone(),
612612
timing_total,
@@ -617,7 +617,13 @@ async fn async_watch(
617617
result,
618618
);
619619
}
620-
Err(_) => {
620+
Ok(Err(_)) => {
621+
if should_clear_screen(clear_screen, show_progress, plain_output) {
622+
print_build_failed_footer();
623+
}
624+
}
625+
Err(error) => {
626+
eprintln!("{error:#}");
621627
if should_clear_screen(clear_screen, show_progress, plain_output) {
622628
print_build_failed_footer();
623629
}

‎rewatch/tests/suite.sh‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ fi
145145
./watch/05-watch-ignores-non-source.sh &&
146146
./watch/06-watch-missing-source-folder.sh &&
147147
./watch/07-changed-interface-after-failed-implementation.sh &&
148+
./watch/07-watch-recovers-from-invalid-config.sh &&
148149

149150
# Lock tests
150151
./lock/01-lock-when-watching.sh &&
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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+
exit_watcher
38+
mv rescript.json.watch-recovery.bak rescript.json
39+
exit 1
40+
fi
41+
42+
if ! wait_for_pattern rewatch.log "Finished initial compilation" 60; then
43+
error "Initial watch build did not finish"
44+
cat rewatch.log
45+
exit_watcher
46+
mv rescript.json.watch-recovery.bak rescript.json
47+
exit 1
48+
fi
49+
50+
initial_completed=$(grep -c "Finished compilation" rewatch.log 2> /dev/null || true)
51+
printf '{"name":' > rescript.json.watch-recovery.next
52+
mv rescript.json.watch-recovery.next rescript.json
53+
54+
if ! wait_for_pattern rewatch.log "Could not initialize build" 30; then
55+
error "Watcher did not report the invalid config"
56+
cat rewatch.log
57+
exit_watcher
58+
mv rescript.json.watch-recovery.bak rescript.json
59+
exit 1
60+
fi
61+
62+
node -e '
63+
const fs = require("fs");
64+
const config = JSON.parse(fs.readFileSync(process.argv[1], "utf8"));
65+
config.suffix = ".recovered.mjs";
66+
fs.writeFileSync(process.argv[2], `${JSON.stringify(config, null, 2)}\n`);
67+
' rescript.json.watch-recovery.bak rescript.json.watch-recovery.next
68+
mv rescript.json.watch-recovery.next rescript.json
69+
70+
expected_completed=$((initial_completed + 1))
71+
if ! wait_for_pattern_count rewatch.log "Finished compilation" "$expected_completed" 30; then
72+
error "Watcher did not recover after restoring a valid config"
73+
cat rewatch.log
74+
exit_watcher
75+
mv rescript.json.watch-recovery.bak rescript.json
76+
exit 1
77+
fi
78+
79+
echo '// watch-recovery-test' >> src/Test.res
80+
if ! wait_for_file "./src/Test.recovered.mjs" 20; then
81+
error "Recovered watcher did not compile with the restored config"
82+
cat rewatch.log
83+
exit_watcher
84+
mv rescript.json.watch-recovery.bak rescript.json
85+
git checkout -- src/Test.res
86+
exit 1
87+
fi
88+
89+
exit_watcher
90+
rewatch clean > /dev/null 2>&1
91+
mv rescript.json.watch-recovery.bak rescript.json
92+
git checkout -- src/Test.res
93+
rm -f rewatch.log
94+
rewatch build > /dev/null 2>&1
95+
96+
if git diff --exit-code . > /dev/null 2>&1 && [ -z "$(git ls-files --others --exclude-standard .)" ]; then
97+
success "Watcher recovered from invalid config"
98+
else
99+
error "Watcher recovery test left repository changes"
100+
git diff .
101+
git ls-files --others --exclude-standard .
102+
exit 1
103+
fi

0 commit comments

Comments
 (0)