11use crate :: { SpirvBuilder , SpirvBuilderError , leaf_deps} ;
22use notify:: { Event , RecommendedWatcher , RecursiveMode , Watcher } ;
33use rustc_codegen_spirv_types:: CompileResult ;
4+ use std:: sync:: mpsc:: TrySendError ;
45use std:: {
56 collections:: HashSet ,
67 path:: PathBuf ,
@@ -40,19 +41,21 @@ impl SpirvWatcher {
4041 return Err ( SpirvWatcherError :: WatchWithPrintMetadata . into ( ) ) ;
4142 }
4243
43- let ( tx, rx) = sync_channel ( 0 ) ;
44+ let ( tx, rx) = sync_channel ( 1 ) ;
4445 let watcher =
4546 notify:: recommended_watcher ( move |result : notify:: Result < Event > | match result {
4647 Ok ( event) => match event. kind {
4748 notify:: EventKind :: Any
4849 | notify:: EventKind :: Create ( _)
4950 | notify:: EventKind :: Modify ( _)
5051 | notify:: EventKind :: Remove ( _)
51- | notify:: EventKind :: Other => {
52- if let Err ( err) = tx. try_send ( ( ) ) {
53- log:: error!( "send error: {err:?}" ) ;
54- }
55- }
52+ | notify:: EventKind :: Other => match tx. try_send ( ( ) ) {
53+ Ok ( _) => ( ) ,
54+ // disconnect is fine, SpirvWatcher is currently dropping
55+ Err ( TrySendError :: Disconnected ( _) ) => ( ) ,
56+ // full is fine, we just need to send a single event anyway
57+ Err ( TrySendError :: Full ( _) ) => ( ) ,
58+ } ,
5659 notify:: EventKind :: Access ( _) => { }
5760 } ,
5861 Err ( err) => log:: error!( "notify error: {err:?}" ) ,
@@ -78,7 +81,7 @@ impl SpirvWatcher {
7881 return self . recv_first_result ( ) ;
7982 }
8083
81- self . rx . recv ( ) . expect ( "watcher should be alive" ) ;
84+ self . rx . recv ( ) . map_err ( |_| SpirvWatcherError :: WatcherDied ) ? ;
8285 let metadata_file = crate :: invoke_rustc ( & self . builder ) ?;
8386 let result = self . builder . parse_metadata_file ( & metadata_file) ?;
8487
@@ -97,7 +100,7 @@ impl SpirvWatcher {
97100 . watch ( watch_path, RecursiveMode :: Recursive )
98101 . map_err ( SpirvWatcherError :: NotifyFailed ) ?;
99102 let path = loop {
100- self . rx . recv ( ) . expect ( "watcher should be alive" ) ;
103+ self . rx . recv ( ) . map_err ( |_| SpirvWatcherError :: WatcherDied ) ? ;
101104 match crate :: invoke_rustc ( & self . builder ) {
102105 Ok ( path) => break path,
103106 Err ( err) => log:: error!( "{err}" ) ,
@@ -136,4 +139,6 @@ pub enum SpirvWatcherError {
136139 WatchWithPrintMetadata ,
137140 #[ error( "could not notify for changes: {0}" ) ]
138141 NotifyFailed ( #[ from] notify:: Error ) ,
142+ #[ error( "watcher died and closed channel" ) ]
143+ WatcherDied ,
139144}
0 commit comments