11package mill .runner
22
33import mill .api .{SystemStreams , internal }
4- import mill .main .client .DebugLog
54import mill .util .{Colors , Watchable }
65
76import java .io .InputStream
@@ -14,8 +13,6 @@ import scala.util.Using
1413 */
1514@ internal
1615object Watching {
17- private final val enableDebugLog = false
18-
1916 case class Result [T ](watched : Seq [Watchable ], error : Option [String ], result : T )
2017
2118 trait Evaluate [T ] {
@@ -24,11 +21,13 @@ object Watching {
2421
2522 /**
2623 * @param useNotify whether to use filesystem based watcher. If it is false uses polling.
24+ * @param serverDir the directory for storing logs of the mill server
2725 */
2826 case class WatchArgs (
2927 setIdle : Boolean => Unit ,
3028 colors : Colors ,
31- useNotify : Boolean
29+ useNotify : Boolean ,
30+ serverDir : os.Path
3231 )
3332
3433 def watchLoop [T ](
@@ -81,7 +80,8 @@ object Watching {
8180 streams.in,
8281 watchables,
8382 watchArgs.colors,
84- useNotify = watchArgs.useNotify
83+ useNotify = watchArgs.useNotify,
84+ serverDir = watchArgs.serverDir
8585 )
8686 }
8787 }
@@ -95,7 +95,8 @@ object Watching {
9595 stdin : InputStream ,
9696 watched : Seq [Watchable ],
9797 colors : Colors ,
98- useNotify : Boolean
98+ useNotify : Boolean ,
99+ serverDir : os.Path
99100 ): Boolean = {
100101 setIdle(true )
101102 val (watchedPollables, watchedPathsSeq) = watched.partitionMap {
@@ -121,16 +122,17 @@ object Watching {
121122 enterKeyPressed
122123 }
123124
124- if (useNotify) {
125+ if (useNotify) Using .resource(os.write.outputStream(serverDir / " fsNotifyWatchLog" )) { watchLog =>
126+ def writeToWatchLog (s : String ): Unit = {
127+ watchLog.write(s.getBytes(java.nio.charset.StandardCharsets .UTF_8 ))
128+ watchLog.write('\n ' )
129+ }
130+
125131 @ volatile var pathChangesDetected = false
126132
127133 // oslib watch only works with folders, so we have to watch the parent folders instead
128134
129- if (enableDebugLog) DebugLog .println(
130- colors.info(
131- s " [watch:watched-paths:unfiltered] ${watchedPathsSet.toSeq.sorted.mkString(" \n " )}"
132- ).toString
133- )
135+ writeToWatchLog(s " [watched-paths:unfiltered] ${watchedPathsSet.toSeq.sorted.mkString(" \n " )}" )
134136
135137 val workspaceRoot = mill.api.WorkspaceRoot .workspaceRoot
136138
@@ -156,38 +158,28 @@ object Watching {
156158 val filterPaths = pathsUnderWorkspaceRoot.flatMap { path =>
157159 path.relativeTo(workspaceRoot).segments.inits.map(segments => workspaceRoot / segments)
158160 }
159- if (enableDebugLog) DebugLog .println(colors.info(
160- s " [watch:watched-paths:filtered] ${filterPaths.toSeq.sorted.mkString(" \n " )}"
161- ).toString())
161+ writeToWatchLog(s " [watched-paths:filtered] ${filterPaths.toSeq.sorted.mkString(" \n " )}" )
162162
163163 Using .resource(os.watch.watch(
164164 // Just watch the root folder
165165 Seq (workspaceRoot),
166166 filter = path => {
167167 val shouldBeWatched =
168168 filterPaths.contains(path) || watchedPathsSet.exists(watchedPath => path.startsWith(watchedPath))
169- if (enableDebugLog) {
170- DebugLog .println(colors.info(s " [watch:filter] $path (shouldBeWatched= $shouldBeWatched) " ).toString)
171- }
169+ writeToWatchLog(s " [filter] (shouldBeWatched= $shouldBeWatched) $path" )
172170 shouldBeWatched
173171 },
174172 onEvent = changedPaths => {
175173 // Make sure that the changed paths are actually the ones in our watch list and not some adjacent files in the
176174 // same folder
177175 val hasWatchedPath =
178176 changedPaths.exists(p => watchedPathsSet.exists(watchedPath => p.startsWith(watchedPath)))
179- if (enableDebugLog) DebugLog .println(colors.info(
180- s " [watch:changed-paths] hasWatchedPath= $hasWatchedPath) ${changedPaths.mkString(" \n " )}"
181- ).toString)
177+ writeToWatchLog(s " [changed-paths] (hasWatchedPath= $hasWatchedPath) ${changedPaths.mkString(" \n " )}" )
182178 if (hasWatchedPath) {
183179 pathChangesDetected = true
184180 }
185181 },
186- logger =
187- if (enableDebugLog) (eventType, data) => {
188- DebugLog .println(colors.info(s " [watch] $eventType: ${pprint.apply(data)}" ).toString)
189- }
190- else (_, _) => {}
182+ logger = (eventType, data) => writeToWatchLog(s " [watch:event] $eventType: ${pprint.apply(data).plainText}" )
191183 )) { _ =>
192184 doWatch(notifiablesChanged = () => pathChangesDetected)
193185 }
0 commit comments