Skip to content

Commit 1b797e9

Browse files
committed
Move stale checking after we start the watch
1 parent dfbba1f commit 1b797e9

File tree

2 files changed

+12
-17
lines changed

2 files changed

+12
-17
lines changed

integration/invalidation/watch-source-input/src/WatchSourceInputTests.scala

+1-6
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ object WatchSourceTests extends WatchTests {
8383
"Running qux foo contents initial-foo1 initial-foo2 Running qux bar contents initial-bar"
8484
)
8585

86-
Thread.sleep(1000) // Wait for the watching to begin
8786
os.write.over(workspacePath / "foo1.txt", "edited-foo1")
8887
awaitCompletionMarker(tester, "quxRan1")
8988
expectedErr.append(
@@ -93,8 +92,7 @@ object WatchSourceTests extends WatchTests {
9392
expectedShows.append(
9493
"Running qux foo contents edited-foo1 initial-foo2 Running qux bar contents initial-bar"
9594
)
96-
97-
Thread.sleep(1000) // Wait for the watching to begin
95+
9896
os.write.over(workspacePath / "foo2.txt", "edited-foo2")
9997
awaitCompletionMarker(tester, "quxRan2")
10098
expectedErr.append(
@@ -105,7 +103,6 @@ object WatchSourceTests extends WatchTests {
105103
"Running qux foo contents edited-foo1 edited-foo2 Running qux bar contents initial-bar"
106104
)
107105

108-
Thread.sleep(1000) // Wait for the watching to begin
109106
os.write.over(workspacePath / "bar.txt", "edited-bar")
110107
awaitCompletionMarker(tester, "quxRan3")
111108
expectedErr.append(
@@ -116,7 +113,6 @@ object WatchSourceTests extends WatchTests {
116113
"Running qux foo contents edited-foo1 edited-foo2 Running qux bar contents edited-bar"
117114
)
118115

119-
Thread.sleep(1000) // Wait for the watching to begin
120116
os.write.append(workspacePath / "build.mill", "\ndef unrelated = true")
121117
awaitCompletionMarker(tester, "initialized1")
122118
expectedOut.append(
@@ -128,7 +124,6 @@ object WatchSourceTests extends WatchTests {
128124
)
129125

130126
if (show) expectedOut.append("{}")
131-
Thread.sleep(1000) // Wait for the watching to begin
132127
os.write.over(workspacePath / "watchValue.txt", "exit")
133128
awaitCompletionMarker(tester, "initialized2")
134129
expectedOut.append("Setting up build.mill")

runner/daemon/src/mill/daemon/Watching.scala

+11-11
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@ object Watching {
6262

6363
watch match {
6464
case None =>
65-
val Result(watchables, errorOpt, result) =
66-
evaluate(enterKeyPressed = false, previousState = None)
65+
val Result(watchables, errorOpt, result) = evaluate(enterKeyPressed = false, previousState = None)
6766
handleError(errorOpt)
6867
(errorOpt.isEmpty, result)
6968

@@ -77,19 +76,13 @@ object Watching {
7776
prevState = Some(result)
7877
handleError(errorOpt)
7978

80-
// Do not enter watch if already stale, re-evaluate instantly.
81-
val alreadyStale = watchables.exists(w => !validateAnyWatchable(w))
82-
if (alreadyStale) {
83-
enterKeyPressed = false
84-
} else {
85-
enterKeyPressed = watchAndWait(streams, streams.in, watchables, watchArgs)
86-
}
79+
enterKeyPressed = watchAndWait(streams, streams.in, watchables, watchArgs)
8780
}
8881
throw new IllegalStateException("unreachable")
8982
}
9083
}
9184

92-
def watchAndWait(
85+
private def watchAndWait(
9386
streams: SystemStreams,
9487
stdin: InputStream,
9588
watched: Seq[Watchable],
@@ -195,7 +188,14 @@ object Watching {
195188
logger = (eventType, data) =>
196189
writeToWatchLog(s"[watch:event] $eventType: ${pprint.apply(data).plainText}")
197190
)) { _ =>
198-
doWatch(notifiablesChanged = () => pathChangesDetected)
191+
// If already stale, re-evaluate instantly.
192+
//
193+
// We need to do this to prevent any changes from slipping through the gap between the last evaluation and
194+
// starting the watch.
195+
val alreadyStale = watched.exists(w => !validateAnyWatchable(w))
196+
197+
if (alreadyStale) false
198+
else doWatch(notifiablesChanged = () => pathChangesDetected)
199199
}
200200
}
201201
}

0 commit comments

Comments
 (0)