Skip to main content

gpui_rhai/
dependency.rs

1use std::collections::{BTreeMap, BTreeSet};
2
3use rhai::{AST, ASTNode, Engine, Expr, OptimizationLevel, Stmt};
4use thiserror::Error;
5
6use crate::{ModuleId, ScriptSource, ScriptSourceError};
7
8#[derive(Clone, Debug, Default, Eq, PartialEq)]
9pub struct ModuleDependencyGraph {
10    dependencies: BTreeMap<ModuleId, BTreeSet<ModuleId>>,
11    dependents: BTreeMap<ModuleId, BTreeSet<ModuleId>>,
12}
13
14impl ModuleDependencyGraph {
15    #[must_use]
16    pub fn new() -> Self {
17        Self::default()
18    }
19
20    pub fn set_dependencies(&mut self, module: &ModuleId, dependencies: BTreeSet<ModuleId>) {
21        if let Some(previous) = self
22            .dependencies
23            .insert(module.clone(), dependencies.clone())
24        {
25            for dependency in previous {
26                if let Some(dependents) = self.dependents.get_mut(&dependency) {
27                    dependents.remove(module);
28                }
29            }
30        }
31        for dependency in dependencies {
32            self.dependents
33                .entry(dependency)
34                .or_default()
35                .insert(module.clone());
36        }
37        self.dependents
38            .retain(|_, dependents| !dependents.is_empty());
39    }
40
41    #[must_use]
42    pub fn dependencies_of(&self, module: &ModuleId) -> BTreeSet<ModuleId> {
43        self.dependencies.get(module).cloned().unwrap_or_default()
44    }
45
46    #[must_use]
47    pub fn affected_by(&self, changed: impl IntoIterator<Item = ModuleId>) -> BTreeSet<ModuleId> {
48        let mut affected = changed.into_iter().collect::<BTreeSet<_>>();
49        let mut pending = affected.iter().cloned().collect::<Vec<_>>();
50        while let Some(module) = pending.pop() {
51            for dependent in self.dependents.get(&module).into_iter().flatten() {
52                if affected.insert(dependent.clone()) {
53                    pending.push(dependent.clone());
54                }
55            }
56        }
57        affected
58    }
59
60    /// Rebuild a graph from the current contents of a script source.
61    ///
62    /// # Errors
63    ///
64    /// Returns source or static-import parsing errors.
65    pub fn from_source(source: &impl ScriptSource) -> Result<Self, DependencyError> {
66        let mut graph = Self::new();
67        for id in source.module_ids() {
68            let asset = source.load(&id)?;
69            graph.set_dependencies(&id, extract_imports(&asset.source)?);
70        }
71        Ok(graph)
72    }
73}
74
75/// Extract literal Rhai imports from an unoptimized Rhai AST.
76///
77/// # Errors
78///
79/// Returns [`DependencyError`] for invalid Rhai syntax, a dynamic import, or an
80/// invalid module ID.
81pub fn extract_imports(source: &str) -> Result<BTreeSet<ModuleId>, DependencyError> {
82    let (global, functions) = crate::engine::EXPRESSION_DEPTHS;
83    extract_imports_with_depths(source, global, functions)
84}
85
86/// [`extract_imports`] with the expression depths of the engine that will
87/// compile the source, so a Host that raises them can import deeper modules.
88pub(crate) fn extract_imports_with_depths(
89    source: &str,
90    global: usize,
91    functions: usize,
92) -> Result<BTreeSet<ModuleId>, DependencyError> {
93    let mut parser = Engine::new_raw();
94    parser.set_optimization_level(OptimizationLevel::None);
95    parser.set_max_expr_depths(global, functions);
96    let ast = parser
97        .compile(source)
98        .map_err(|error| DependencyError::Parse {
99            position: error.position(),
100            message: crate::engine::explain_keyword_name(source, error).to_string(),
101        })?;
102    extract_imports_from_ast(&ast)
103}
104
105fn extract_imports_from_ast(ast: &AST) -> Result<BTreeSet<ModuleId>, DependencyError> {
106    let mut imports = BTreeSet::new();
107    let mut error = None;
108    ast.walk(&mut |path| {
109        let Some(ASTNode::Stmt(Stmt::Import(import, _))) = path.last() else {
110            return true;
111        };
112        if let Expr::StringConstant(module, ..) = &import.0 {
113            match ModuleId::parse(module.to_string()) {
114                Ok(module) => {
115                    imports.insert(module);
116                    true
117                }
118                Err(source) => {
119                    error = Some(DependencyError::ModuleId(source));
120                    false
121                }
122            }
123        } else {
124            error = Some(DependencyError::DynamicImport);
125            false
126        }
127    });
128    error.map_or(Ok(imports), Err)
129}
130
131#[derive(Clone)]
132struct CachedModule {
133    content_hash: u64,
134    ast: AST,
135}
136
137#[derive(Clone, Default)]
138pub struct ModuleCompileCache {
139    modules: BTreeMap<ModuleId, CachedModule>,
140    graph: ModuleDependencyGraph,
141}
142
143impl ModuleCompileCache {
144    #[must_use]
145    pub fn new() -> Self {
146        Self::default()
147    }
148
149    /// Recompile changed modules and their transitive dependants transactionally.
150    ///
151    /// # Errors
152    ///
153    /// Returns [`DependencyError`] without modifying the current cache when any
154    /// affected source cannot load or compile.
155    pub fn refresh(
156        &mut self,
157        engine: &Engine,
158        source: &impl ScriptSource,
159        changed: impl IntoIterator<Item = ModuleId>,
160    ) -> Result<ModuleRefreshReport, DependencyError> {
161        let graph = ModuleDependencyGraph::from_source(source)?;
162        let changed = changed.into_iter().collect::<BTreeSet<_>>();
163        let mut affected = self.graph.affected_by(changed.iter().cloned());
164        affected.extend(graph.affected_by(changed));
165        let mut staged = self.modules.clone();
166        let existing = source.module_ids().into_iter().collect::<BTreeSet<_>>();
167        staged.retain(|id, _| existing.contains(id));
168        let mut compiled = Vec::new();
169        for id in &affected {
170            if !existing.contains(id) {
171                continue;
172            }
173            let asset = source.load(id)?;
174            let mut ast =
175                engine
176                    .compile(&asset.source)
177                    .map_err(|source| DependencyError::Compile {
178                        module: id.clone(),
179                        source: source.into(),
180                    })?;
181            crate::engine::validate_runtime_ast(&ast).map_err(|error| {
182                DependencyError::UnsafeAst {
183                    module: id.clone(),
184                    message: error.to_string(),
185                }
186            })?;
187            ast.set_source(id.as_str());
188            staged.insert(
189                id.clone(),
190                CachedModule {
191                    content_hash: asset.content_hash,
192                    ast,
193                },
194            );
195            compiled.push(id.clone());
196        }
197        self.modules = staged;
198        self.graph = graph;
199        Ok(ModuleRefreshReport { affected, compiled })
200    }
201
202    #[must_use]
203    pub fn contains(&self, module: &ModuleId) -> bool {
204        self.modules.contains_key(module)
205    }
206
207    #[must_use]
208    pub fn content_hash(&self, module: &ModuleId) -> Option<u64> {
209        self.modules.get(module).map(|cached| cached.content_hash)
210    }
211
212    #[must_use]
213    pub fn ast(&self, module: &ModuleId) -> Option<&AST> {
214        self.modules.get(module).map(|cached| &cached.ast)
215    }
216}
217
218#[derive(Clone, Debug, Eq, PartialEq)]
219pub struct ModuleRefreshReport {
220    pub affected: BTreeSet<ModuleId>,
221    pub compiled: Vec<ModuleId>,
222}
223
224#[derive(Debug, Error)]
225pub enum DependencyError {
226    #[error("Rhai imports must use a literal module string")]
227    DynamicImport,
228    #[error("Rhai source failed to parse while extracting imports at {position}: {message}")]
229    Parse {
230        position: rhai::Position,
231        message: String,
232    },
233    #[error("module `{module}` failed to compile: {source}")]
234    Compile {
235        module: ModuleId,
236        #[source]
237        source: Box<rhai::EvalAltResult>,
238    },
239    #[error("module `{module}` contains an unsafe Rhai AST: {message}")]
240    UnsafeAst { module: ModuleId, message: String },
241    #[error(transparent)]
242    ModuleId(#[from] crate::ModuleIdError),
243    #[error(transparent)]
244    Source(#[from] ScriptSourceError),
245}
246
247#[cfg(feature = "dev-reload")]
248mod watcher {
249    use std::path::{Path, PathBuf};
250    use std::sync::mpsc::{Receiver, TryRecvError, channel};
251    use std::time::Duration;
252
253    use notify::{Config, Event, PollWatcher, RecursiveMode, Watcher};
254    use thiserror::Error;
255
256    pub struct FileWatcher {
257        root: PathBuf,
258        receiver: Receiver<notify::Result<Event>>,
259        _watcher: PollWatcher,
260    }
261
262    impl FileWatcher {
263        /// Watch a UI source tree recursively.
264        ///
265        /// # Errors
266        ///
267        /// Returns [`WatcherError`] when the platform watcher cannot initialize.
268        pub fn new(root: impl AsRef<Path>) -> Result<Self, WatcherError> {
269            let root = root.as_ref().canonicalize().map_err(WatcherError::Io)?;
270            let (sender, receiver) = channel();
271            let mut watcher = PollWatcher::new(
272                move |event| {
273                    let _ = sender.send(event);
274                },
275                Config::default()
276                    .with_poll_interval(Duration::from_millis(100))
277                    .with_compare_contents(true),
278            )?;
279            watcher.watch(&root, RecursiveMode::Recursive)?;
280            Ok(Self {
281                root,
282                receiver,
283                _watcher: watcher,
284            })
285        }
286
287        /// Drain currently available relevant file changes without blocking.
288        ///
289        /// # Errors
290        ///
291        /// Returns [`WatcherError`] for platform watcher failures or a closed
292        /// event channel.
293        pub fn poll(&self) -> Result<FileChangeBatch, WatcherError> {
294            let mut paths = std::collections::BTreeSet::new();
295            loop {
296                match self.receiver.try_recv() {
297                    Ok(Ok(event)) => {
298                        paths.extend(event.paths.into_iter().filter(|path| relevant(path)));
299                    }
300                    Ok(Err(error)) => return Err(error.into()),
301                    Err(TryRecvError::Empty) => break,
302                    Err(TryRecvError::Disconnected) => {
303                        return Err(WatcherError::Disconnected);
304                    }
305                }
306            }
307            Ok(FileChangeBatch {
308                root: self.root.clone(),
309                paths,
310            })
311        }
312    }
313
314    pub(super) fn relevant(path: &Path) -> bool {
315        matches!(
316            path.extension().and_then(|extension| extension.to_str()),
317            Some(
318                "rhai"
319                    | "toml"
320                    | "svg"
321                    | "png"
322                    | "jpg"
323                    | "jpeg"
324                    | "gif"
325                    | "webp"
326                    | "bmp"
327                    | "tif"
328                    | "tiff"
329            )
330        )
331    }
332
333    #[derive(Clone, Debug, Eq, PartialEq)]
334    pub struct FileChangeBatch {
335        pub root: PathBuf,
336        pub paths: std::collections::BTreeSet<PathBuf>,
337    }
338
339    #[derive(Debug, Error)]
340    pub enum WatcherError {
341        #[error("file watcher I/O failed: {0}")]
342        Io(std::io::Error),
343        #[error("file watcher failed: {0}")]
344        Notify(#[from] notify::Error),
345        #[error("file watcher event channel disconnected")]
346        Disconnected,
347    }
348}
349
350#[cfg(feature = "dev-reload")]
351pub use watcher::{FileChangeBatch, FileWatcher, WatcherError};
352
353#[cfg(test)]
354mod tests {
355    use super::*;
356    use crate::EmbeddedScriptSource;
357
358    fn id(value: &str) -> ModuleId {
359        ModuleId::parse(value).unwrap()
360    }
361
362    #[test]
363    fn import_scanner_ignores_comments_and_string_contents() {
364        let imports = extract_imports(
365            r#"
366                // import "ignored/line" as ignored;
367                /* import "ignored/block" as ignored; */
368                let message = "import \"ignored/string\"";
369                import "components/button" as button;
370            "#,
371        )
372        .unwrap();
373        assert_eq!(imports, BTreeSet::from([id("components/button")]));
374    }
375
376    #[test]
377    fn import_extraction_uses_rhai_parser_for_nested_comments_and_templates() {
378        for source in [
379            r#"/* outer /* inner */ import "../ignored"; */ fn view() { text("ok") }"#,
380            r#"fn view() { text(`example: import "../ignored" as demo;`) }"#,
381        ] {
382            assert!(extract_imports(source).unwrap().is_empty());
383        }
384        assert_eq!(
385            extract_imports(
386                r#"fn view() { text(`before ${#{ value: 1 }.value} after`) }
387                    import "components/real" as real;"#,
388            )
389            .unwrap(),
390            BTreeSet::from([id("components/real")])
391        );
392    }
393
394    #[test]
395    fn import_extraction_uses_unoptimized_ast_for_nested_templates() {
396        let cases = [
397            (
398                r#"let x = `a${if true { "" } else { `b${1}` }}`;"#,
399                BTreeSet::new(),
400            ),
401            (
402                r#"let x = `a${`b${1}`}`; import "components/real" as real;"#,
403                BTreeSet::from([id("components/real")]),
404            ),
405            (r"let x = `a${`b${1}c${2}`}d${3}`;", BTreeSet::new()),
406            (r"let x = `a${`b${`c${1}`}`}`;", BTreeSet::new()),
407            (
408                r#"let x = `a${{ import "components/real" as real; `b${1}` }}`;"#,
409                BTreeSet::from([id("components/real")]),
410            ),
411            (r#"let x = `a${`import "../fake" ${1}`}`;"#, BTreeSet::new()),
412            (
413                r"let x = `a${#{ value: `b${#{ nested: 1 }.nested}` }.value}`;",
414                BTreeSet::new(),
415            ),
416            (
417                r#"/* outer /* inner */ import "../fake"; */ let x = `a${1}`;"#,
418                BTreeSet::new(),
419            ),
420            (
421                r#"if false { import "components/dead" as dead; }"#,
422                BTreeSet::from([id("components/dead")]),
423            ),
424        ];
425        for (source, expected) in cases {
426            assert_eq!(extract_imports(source).unwrap(), expected, "{source}");
427        }
428    }
429
430    #[test]
431    fn import_extraction_rejects_dynamic_and_invalid_imports() {
432        assert!(
433            extract_imports(r#"let module = "components/real"; import module as real;"#).is_err()
434        );
435        assert!(extract_imports(r#"import "../outside" as outside;"#).is_err());
436        assert!(matches!(
437            extract_imports("let broken = `unterminated${1}"),
438            Err(DependencyError::Parse { .. })
439        ));
440    }
441
442    #[test]
443    fn changes_invalidate_transitive_dependants() {
444        let mut graph = ModuleDependencyGraph::new();
445        graph.set_dependencies(&id("button"), BTreeSet::new());
446        graph.set_dependencies(&id("toolbar"), BTreeSet::from([id("button")]));
447        graph.set_dependencies(&id("main"), BTreeSet::from([id("toolbar")]));
448        assert_eq!(
449            graph.affected_by([id("button")]),
450            BTreeSet::from([id("button"), id("toolbar"), id("main")])
451        );
452    }
453
454    #[test]
455    fn failed_refresh_does_not_commit_partial_cache() {
456        let button = id("button");
457        let toolbar = id("toolbar");
458        let mut source = EmbeddedScriptSource::new(BTreeMap::from([
459            (button.clone(), "fn Button() { 1 }".to_owned()),
460            (
461                toolbar.clone(),
462                "import \"button\" as button; fn Toolbar() { 1 }".to_owned(),
463            ),
464        ]));
465        let engine = Engine::new();
466        let mut cache = ModuleCompileCache::new();
467        cache
468            .refresh(&engine, &source, [button.clone(), toolbar.clone()])
469            .unwrap();
470        let old_hash = cache.content_hash(&button).unwrap();
471
472        source = EmbeddedScriptSource::new(BTreeMap::from([
473            (button.clone(), "fn Button() { 2 }".to_owned()),
474            (toolbar.clone(), "fn Toolbar( {".to_owned()),
475        ]));
476        assert!(cache.refresh(&engine, &source, [button.clone()]).is_err());
477        assert_eq!(cache.content_hash(&button), Some(old_hash));
478    }
479
480    #[cfg(feature = "dev-reload")]
481    #[test]
482    fn file_watcher_reports_rhai_changes() {
483        let directory = tempfile::tempdir().unwrap();
484        let file = directory.path().join("main.rhai");
485        std::fs::write(&file, "fn view(ctx) { text(\"before\") }").unwrap();
486        let watcher = FileWatcher::new(directory.path()).unwrap();
487        std::thread::sleep(std::time::Duration::from_millis(200));
488        std::fs::write(&file, "fn view(ctx) { text(\"after\") }").unwrap();
489        let deadline = std::time::Instant::now() + std::time::Duration::from_secs(10);
490        loop {
491            let batch = watcher.poll().unwrap();
492            if batch
493                .paths
494                .iter()
495                .any(|changed| changed.ends_with("main.rhai"))
496            {
497                break;
498            }
499            assert!(
500                std::time::Instant::now() < deadline,
501                "watcher event timed out"
502            );
503            std::thread::sleep(std::time::Duration::from_millis(25));
504        }
505    }
506
507    #[cfg(feature = "dev-reload")]
508    #[test]
509    fn file_watcher_accepts_all_supported_image_formats() {
510        for extension in [
511            "svg", "png", "jpg", "jpeg", "gif", "webp", "bmp", "tif", "tiff",
512        ] {
513            assert!(watcher::relevant(std::path::Path::new(&format!(
514                "asset.{extension}"
515            ))));
516        }
517        assert!(!watcher::relevant(std::path::Path::new("asset.txt")));
518    }
519}