diff --git a/.changeset/theme-check-rs-threads.md b/.changeset/theme-check-rs-threads.md new file mode 100644 index 000000000..ce5f63542 --- /dev/null +++ b/.changeset/theme-check-rs-threads.md @@ -0,0 +1,5 @@ +--- +'theme-check-rs': patch +--- + +Add `Checker::threads`, where 1 checks the files on the calling thread, and resume a check's panic with its own message diff --git a/crates/theme-check/README.md b/crates/theme-check/README.md index 1139abfa6..43442d7e6 100644 --- a/crates/theme-check/README.md +++ b/crates/theme-check/README.md @@ -45,9 +45,10 @@ let report = Checker::new() .run(&fs, "/theme")?; ``` -It also takes the docs, schemas, check settings and files to ignore. Without them, it runs the -recommended checks, with the docs and schemas downloaded from theme-liquid-docs. Checks of your -own walk the AST, as in the [crate docs](https://docs.rs/theme-check). +It also takes the docs, schemas, check settings, files to ignore and a thread count. Without +them, it runs the recommended checks on every core, with the docs and schemas downloaded from +theme-liquid-docs. Checks of your own walk the AST, as in the +[crate docs](https://docs.rs/theme-check). ## The CLI diff --git a/crates/theme-check/src/lib.rs b/crates/theme-check/src/lib.rs index 751508c81..0468a188f 100644 --- a/crates/theme-check/src/lib.rs +++ b/crates/theme-check/src/lib.rs @@ -280,6 +280,7 @@ pub struct Checker { checks: Option>, settings: Map, ignore: Vec, + threads: usize, downloaded: docs::ThemeLiquidDocsManager, } @@ -297,6 +298,7 @@ impl Checker { checks: None, settings: Map::new(), ignore: Vec::new(), + threads: 0, downloaded: docs::ThemeLiquidDocsManager::new(Box::new(|_| {})), } } @@ -353,6 +355,13 @@ impl Checker { self } + /// How many threads check the files. 0, the default, is one per core. With 1, they're checked + /// on the calling thread. A check that panics panics on the calling thread either way. + pub fn threads(mut self, threads: usize) -> Self { + self.threads = threads; + self + } + /// Checks the theme at `root` in `fs`: its `.liquid` and `.json` files, but not dot files, /// `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. @@ -415,7 +424,91 @@ impl Checker { &fs::Uris(fs), docset, schemas, + self.threads, ); Ok(report(&theme, &offenses)) } } + +#[cfg(test)] +mod tests { + use super::*; + use std::sync::Mutex; + use std::thread::ThreadId; + + static THREADS: Mutex> = Mutex::new(Vec::new()); + + struct RecordThread; + + impl<'a> LiquidCheck<'a> for RecordThread { + fn start(&mut self, _: &mut Context<'a>) -> CheckResult { + THREADS.lock().unwrap().push(std::thread::current().id()); + Ok(()) + } + } + + struct Panic; + + impl<'a> LiquidCheck<'a> for Panic { + fn start(&mut self, _: &mut Context<'a>) -> CheckResult { + panic!("boom") + } + } + + const fn check(code: &'static str, create: Create) -> Check { + Check { + code, + name: code, + aliases: &[], + severity: Severity::Error, + recommended: false, + deprecated: false, + schema: &[], + create, + } + } + + static RECORD_THREAD: Check = check( + "RecordThread", + Create::Liquid(|_| Some(Box::new(RecordThread))), + ); + static PANIC: Check = check("Panic", Create::Liquid(|_| Some(Box::new(Panic)))); + + fn checker(check: &'static Check) -> Checker { + Checker::new() + .docs(Docs::from_json("[]", "[]", "[]", "{}").unwrap()) + .schemas(Vec::new()) + .checks([check]) + } + + fn theme() -> MemoryFs { + [ + ("/theme/snippets/a.liquid", ""), + ("/theme/snippets/b.liquid", ""), + ] + .into_iter() + .collect() + } + + #[test] + fn one_thread_is_the_calling_thread() { + checker(&RECORD_THREAD) + .threads(1) + .run(&theme(), "/theme") + .unwrap(); + assert_eq!(*THREADS.lock().unwrap(), [std::thread::current().id(); 2]); + } + + #[test] + fn a_panic_is_resumed_with_its_message() { + for threads in [0, 1, 2] { + let checker = checker(&PANIC).threads(threads); + let fs = theme(); + let e = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { + checker.run(&fs, "/theme") + })) + .unwrap_err(); + assert_eq!(e.downcast_ref::<&str>(), Some(&"boom"), "{threads} threads"); + } + } +} diff --git a/crates/theme-check/src/node/mod.rs b/crates/theme-check/src/node/mod.rs index 52eff8ef7..ec1cec34d 100644 --- a/crates/theme-check/src/node/mod.rs +++ b/crates/theme-check/src/node/mod.rs @@ -375,6 +375,7 @@ pub(crate) fn run( &Uris(&Disk), docs.docset(), schemas, + 0, ); let result = after(&theme, &offenses); let theme = theme.iter().map(|f| f.uri.clone()).collect(); @@ -389,7 +390,8 @@ pub(crate) fn run( } /// The rest of `themeCheckRun`: core `check(theme, config, dependencies)` with theme-check-node's -/// dependencies. `root` is the one the run was given, and `root_uri` the config's. +/// dependencies, on `threads` threads as in [`crate::runner::check`]. `root` is the one the run +/// was given, and `root_uri` the config's. #[allow(clippy::too_many_arguments)] pub(crate) fn check_theme( core: &Config, @@ -400,6 +402,7 @@ pub(crate) fn check_theme( fs: &dyn FileSystem, docset: &ThemeDocset, schemas: &[Schema], + threads: usize, ) -> Vec { // `JSONValidator.create(themeLiquidDocsManager, config)`, whose `isValid` the schema getters // use, and the one `check()` makes again for `validateJSON`. Two instances, as in TS: schema @@ -442,7 +445,7 @@ pub(crate) fn check_theme( // `getWebComponentDefinitionReference: () => undefined`. `getReferences` and // `getDependencies` answer `[]` when it fails. (`getAppBlockSchema` is `Deps::get_app_block_schema`.) deps.theme_graph = Some(crate::graph::build_theme_graph(&deps, &|_| None, None)); - crate::runner::check(core, &deps, 0).offenses + crate::runner::check(core, &deps, threads).offenses } // --------------------------------------------------------------------------------------------- diff --git a/crates/theme-check/src/runner.rs b/crates/theme-check/src/runner.rs index 4341c0761..65f5c71e0 100644 --- a/crates/theme-check/src/runner.rs +++ b/crates/theme-check/src/runner.rs @@ -28,7 +28,8 @@ pub struct CheckOutput { } /// `check(theme, config, dependencies)`. Files are independent, so they run on `threads` threads -/// (`0` = all cores); output is in file order either way. +/// (`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 { let ignores: Vec> = config .checks @@ -45,26 +46,27 @@ pub fn check<'a>(config: &'a Config, deps: &'a Deps<'a>, threads: usize) -> Chec } .min(n.max(1)); let next = AtomicUsize::new(0); - let mut per_file: Vec<(usize, CheckOutput)> = std::thread::scope(|s| { - let handles: Vec<_> = (0..threads) - .map(|_| { - s.spawn(|| { - let mut out = Vec::new(); - loop { - let i = next.fetch_add(1, Ordering::Relaxed); - if i >= n { - break out; - } - out.push((i, run(i))); - } - }) - }) - .collect(); - handles - .into_iter() - .flat_map(|h| h.join().expect("check thread panicked")) - .collect() - }); + let work = || { + let mut out = Vec::new(); + loop { + let i = next.fetch_add(1, Ordering::Relaxed); + if i >= n { + break out; + } + out.push((i, run(i))); + } + }; + let mut per_file: Vec<(usize, CheckOutput)> = if threads == 1 { + work() + } else { + std::thread::scope(|s| { + let handles: Vec<_> = (0..threads).map(|_| s.spawn(work)).collect(); + handles + .into_iter() + .flat_map(|h| h.join().unwrap_or_else(|e| std::panic::resume_unwind(e))) + .collect() + }) + }; per_file.sort_by_key(|(i, _)| *i); let mut out = CheckOutput::default(); for (_, o) in per_file {