diff --git a/.changeset/theme-check-rs-cancelable.md b/.changeset/theme-check-rs-cancelable.md new file mode 100644 index 000000000..a39df8ffd --- /dev/null +++ b/.changeset/theme-check-rs-cancelable.md @@ -0,0 +1,5 @@ +--- +'theme-check-rs': patch +--- + +Add `Checker::cancelable`, for a run that stops between files, and `Error::is_cancelled` diff --git a/crates/theme-check/README.md b/crates/theme-check/README.md index 43442d7e6..86b88dc46 100644 --- a/crates/theme-check/README.md +++ b/crates/theme-check/README.md @@ -50,6 +50,9 @@ them, it runs the recommended checks on every core, with the docs and schemas do theme-liquid-docs. Checks of your own walk the AST, as in the [crate docs](https://docs.rs/theme-check). +`checker.cancelable(&|| stop.load(Relaxed)).run(&fs, "/theme")` stops between files once the +closure returns true, with an error whose `is_cancelled()` is true. + ## The CLI In this repository: diff --git a/crates/theme-check/src/lib.rs b/crates/theme-check/src/lib.rs index 0468a188f..b1eb2af46 100644 --- a/crates/theme-check/src/lib.rs +++ b/crates/theme-check/src/lib.rs @@ -161,13 +161,38 @@ pub struct Position { pub column: usize, } -/// Why a run failed, like a config that can't be read. +/// Why a run failed, like a config that can't be read, or a [`Checker::cancelable`] run that was +/// cancelled. #[derive(Clone, Debug)] -pub struct Error(String); +pub struct Error { + message: String, + cancelled: bool, +} + +impl Error { + fn new(message: impl Into) -> Self { + Error { + message: message.into(), + cancelled: false, + } + } + + fn cancelled() -> Self { + Error { + message: "cancelled".into(), + cancelled: true, + } + } + + /// Whether the run was cancelled, rather than failed. + pub fn is_cancelled(&self) -> bool { + self.cancelled + } +} impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - f.write_str(&self.0) + f.write_str(&self.message) } } @@ -175,7 +200,7 @@ impl std::error::Error for Error {} impl From for Error { fn from(e: node::JsError) -> Self { - Error(e.message) + Error::new(e.message) } } @@ -251,14 +276,14 @@ impl Docs { system_translations: &str, ) -> Result { let parse = |name: &str, json: &str| { - serde_json::from_str::(json).map_err(|e| Error(format!("{name}: {e}"))) + serde_json::from_str::(json).map_err(|e| Error::new(format!("{name}: {e}"))) }; let list = |name: &str, json: &str| match parse(name, json)? { Value::Array(a) => Ok(a), - _ => Err(Error(format!("{name}: not an array"))), + _ => Err(Error::new(format!("{name}: not an array"))), }; let Value::Object(translations) = parse("system_translations", system_translations)? else { - return Err(Error("system_translations: not an object".into())); + return Err(Error::new("system_translations: not an object")); }; Ok(Docs(docset::ThemeDocset::augmented( list("filters", filters)?, @@ -366,11 +391,55 @@ impl Checker { /// `node_modules`, which the recommended config ignores, or [`Self::ignore`]'s. It fails if /// `root` or a directory below it can't be listed, or a file can't be read. pub fn run(&self, fs: &dyn FileSystem, root: impl AsRef) -> Result { + self.cancelable(&|| false).run(fs, root) + } + + /// This checker, for a run that stops when `cancelled` returns true. It's called before each + /// file is read, parsed and checked, and before the theme graph is built, from the threads + /// that do it. + /// + /// ``` + /// # use theme_check::{Checker, Docs, MemoryFs}; + /// use std::sync::atomic::{AtomicBool, Ordering}; + /// + /// # let fs: MemoryFs = [("/theme/snippets/a.liquid", "")].into_iter().collect(); + /// # let checker = Checker::new().docs(Docs::from_json("[]", "[]", "[]", "{}")?); + /// # let checker = checker.schemas(Vec::new()); + /// let stop = AtomicBool::new(true); + /// let result = checker.cancelable(&|| stop.load(Ordering::Relaxed)).run(&fs, "/theme"); + /// assert!(result.is_err_and(|e| e.is_cancelled())); + /// # Ok::<(), theme_check::Error>(()) + /// ``` + pub fn cancelable<'a>(&'a self, cancelled: &'a (dyn Fn() -> bool + Sync)) -> Cancelable<'a> { + Cancelable { + checker: self, + cancelled, + } + } +} + +/// A [`Checker`] whose run can be cancelled, from [`Checker::cancelable`]. +pub struct Cancelable<'a> { + checker: &'a Checker, + cancelled: &'a (dyn Fn() -> bool + Sync), +} + +impl Cancelable<'_> { + /// [`Checker::run`], or an error whose [`Error::is_cancelled`] is true if it was cancelled. + pub fn run(&self, fs: &dyn FileSystem, root: impl AsRef) -> Result { + let Cancelable { checker, cancelled } = *self; + let stop = || { + if cancelled() { + Err(Error::cancelled()) + } else { + Ok(()) + } + }; let root = resolve(root.as_ref()); let root_uri = path::file_uri(&root); - let settings = node::config::resolve_check_settings(self.settings.clone())?; + let settings = node::config::resolve_check_settings(checker.settings.clone())?; let recommended = node::config::recommended_with(settings.clone())?; - let mut config = match &self.checks { + let mut config = match &checker.checks { None => recommended, Some(checks) => { let enabled = |c: &&Check| { @@ -386,10 +455,10 @@ impl Checker { ) } }; - config.ignore.extend(self.ignore.iter().cloned()); + config.ignore.extend(checker.ignore.iter().cloned()); let mut paths = Vec::new(); - fs::theme_files(fs, Path::new(&root), &mut paths).map_err(|e| Error(e.to_string()))?; + fs::theme_files(fs, Path::new(&root), &mut paths).map_err(|e| Error::new(e.to_string()))?; let ignores = runner::ignore_patterns(&config, None, &root_uri); let mut paths: Vec = paths .into_iter() @@ -399,21 +468,27 @@ impl Checker { paths.sort_by(|a, b| node::locale_compare(a, b)); let files = paths .iter() - .map(|p| match fs.read_to_string(p.as_ref()) { - Ok(source) => Ok((path::file_uri(p), source)), - Err(e) => Err(Error(format!("{p}: {e}"))), + .map(|p| { + stop()?; + match fs.read_to_string(p.as_ref()) { + Ok(source) => Ok((path::file_uri(p), source)), + Err(e) => Err(Error::new(format!("{p}: {e}"))), + } }) .collect::, _>>()?; - let theme: Vec = files.iter().map(|(u, s)| to_source_code(u, s)).collect(); + let theme = files + .iter() + .map(|(u, s)| stop().map(|()| to_source_code(u, s))) + .collect::, _>>()?; // ponytail: themes only. Theme app extensions need `Mode::App`, and its schemas and // checks; add a `Checker` setting for it when someone checks one. - let schemas = match &self.schemas { + let schemas = match &checker.schemas { Some(schemas) => schemas, - None => self.downloaded.schemas(Mode::Theme), + None => checker.downloaded.schemas(Mode::Theme), }; - let docset = match &self.docs { + let docset = match &checker.docs { Some(docs) => &docs.0, - None => self.downloaded.docset(), + None => checker.downloaded.docset(), }; let offenses = node::check_theme( &config, @@ -424,8 +499,10 @@ impl Checker { &fs::Uris(fs), docset, schemas, - self.threads, - ); + checker.threads, + cancelled, + ) + .ok_or_else(Error::cancelled)?; Ok(report(&theme, &offenses)) } } @@ -433,10 +510,12 @@ impl Checker { #[cfg(test)] mod tests { use super::*; + use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::Mutex; use std::thread::ThreadId; static THREADS: Mutex> = Mutex::new(Vec::new()); + static COUNTED: AtomicUsize = AtomicUsize::new(0); struct RecordThread; @@ -447,6 +526,15 @@ mod tests { } } + struct Count; + + impl<'a> LiquidCheck<'a> for Count { + fn start(&mut self, _: &mut Context<'a>) -> CheckResult { + COUNTED.fetch_add(1, Ordering::Relaxed); + Ok(()) + } + } + struct Panic; impl<'a> LiquidCheck<'a> for Panic { @@ -472,6 +560,7 @@ mod tests { "RecordThread", Create::Liquid(|_| Some(Box::new(RecordThread))), ); + static COUNT: Check = check("Count", Create::Liquid(|_| Some(Box::new(Count)))); static PANIC: Check = check("Panic", Create::Liquid(|_| Some(Box::new(Panic)))); fn checker(check: &'static Check) -> Checker { @@ -511,4 +600,15 @@ mod tests { assert_eq!(e.downcast_ref::<&str>(), Some(&"boom"), "{threads} threads"); } } + + #[test] + fn a_run_cancelled_between_files_is_cancelled() { + let checker = checker(&COUNT).threads(1); + let e = checker + .cancelable(&|| COUNTED.load(Ordering::Relaxed) > 0) + .run(&theme(), "/theme") + .unwrap_err(); + assert!(e.is_cancelled()); + assert_eq!(COUNTED.load(Ordering::Relaxed), 1); + } } diff --git a/crates/theme-check/src/node/mod.rs b/crates/theme-check/src/node/mod.rs index ec1cec34d..c8f1ef382 100644 --- a/crates/theme-check/src/node/mod.rs +++ b/crates/theme-check/src/node/mod.rs @@ -376,7 +376,9 @@ pub(crate) fn run( docs.docset(), schemas, 0, - ); + &|| false, + ) + .unwrap_or_default(); let result = after(&theme, &offenses); let theme = theme.iter().map(|f| f.uri.clone()).collect(); Ok(( @@ -391,7 +393,8 @@ pub(crate) fn run( /// The rest of `themeCheckRun`: core `check(theme, config, dependencies)` with theme-check-node's /// dependencies, on `threads` threads as in [`crate::runner::check`]. `root` is the one the run -/// was given, and `root_uri` the config's. +/// was given, and `root_uri` the config's. `None` if `cancelled` returned true before the graph +/// or a file. #[allow(clippy::too_many_arguments)] pub(crate) fn check_theme( core: &Config, @@ -403,7 +406,8 @@ pub(crate) fn check_theme( docset: &ThemeDocset, schemas: &[Schema], threads: usize, -) -> Vec { + cancelled: &(dyn Fn() -> bool + Sync), +) -> Option> { // `JSONValidator.create(themeLiquidDocsManager, config)`, whose `isValid` the schema getters // use, and the one `check()` makes again for `validateJSON`. Two instances, as in TS: schema // resolution state is per validator. @@ -444,8 +448,11 @@ pub(crate) fn check_theme( // `buildThemeGraph(config.rootUri, ...)` with these schema getters, mode and // `getWebComponentDefinitionReference: () => undefined`. `getReferences` and // `getDependencies` answer `[]` when it fails. (`getAppBlockSchema` is `Deps::get_app_block_schema`.) + if cancelled() { + return None; + } deps.theme_graph = Some(crate::graph::build_theme_graph(&deps, &|_| None, None)); - crate::runner::check(core, &deps, threads).offenses + Some(crate::runner::check(core, &deps, threads, cancelled)?.offenses) } // --------------------------------------------------------------------------------------------- diff --git a/crates/theme-check/src/runner.rs b/crates/theme-check/src/runner.rs index 65f5c71e0..6dfc12944 100644 --- a/crates/theme-check/src/runner.rs +++ b/crates/theme-check/src/runner.rs @@ -29,8 +29,13 @@ pub struct CheckOutput { /// `check(theme, config, dependencies)`. Files are independent, so they run on `threads` threads /// (`0` = all cores, `1` = the calling thread); output is in file order either way. A panic in a -/// check is resumed on the calling thread. -pub fn check<'a>(config: &'a Config, deps: &'a Deps<'a>, threads: usize) -> CheckOutput { +/// check is resumed on the calling thread. `None` if `cancelled` returned true before a file. +pub fn check<'a>( + config: &'a Config, + deps: &'a Deps<'a>, + threads: usize, + cancelled: &(dyn Fn() -> bool + Sync), +) -> Option { let ignores: Vec> = config .checks .iter() @@ -50,7 +55,7 @@ pub fn check<'a>(config: &'a Config, deps: &'a Deps<'a>, threads: usize) -> Chec let mut out = Vec::new(); loop { let i = next.fetch_add(1, Ordering::Relaxed); - if i >= n { + if i >= n || cancelled() { break out; } out.push((i, run(i))); @@ -67,13 +72,16 @@ pub fn check<'a>(config: &'a Config, deps: &'a Deps<'a>, threads: usize) -> Chec .collect() }) }; + if per_file.len() < n { + return None; + } per_file.sort_by_key(|(i, _)| *i); let mut out = CheckOutput::default(); for (_, o) in per_file { out.offenses.extend(o.offenses); out.errors.extend(o.errors); } - out + Some(out) } struct Slot<'a, C: ?Sized> { diff --git a/crates/theme-check/tests/specs.rs b/crates/theme-check/tests/specs.rs index a886388f7..fbf6fadc7 100644 --- a/crates/theme-check/tests/specs.rs +++ b/crates/theme-check/tests/specs.rs @@ -416,7 +416,7 @@ fn run_check_specs(dir: &Path) -> (usize, usize) { "getDependencies".into(), "remoteFileSize".into(), ]; - let out = theme_check::runner::check(&config, &deps, 1); + let out = theme_check::runner::check(&config, &deps, 1, &|| false).unwrap(); let mut expected = case["offenses"].as_array().cloned().unwrap_or_default(); for e in &mut expected { @@ -709,7 +709,7 @@ fn run_parity(dir: &Path) -> (usize, usize) { theme_schemas: true, }, ); - let out = theme_check::runner::check(&config, &deps, 1); + let out = theme_check::runner::check(&config, &deps, 1, &|| false).unwrap(); let reported = !out.offenses.is_empty(); let problem = match ruby { None => Some("no snapshot".to_string()),