Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/theme-check-rs-threads.md
Original file line number Diff line number Diff line change
@@ -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
7 changes: 4 additions & 3 deletions crates/theme-check/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
93 changes: 93 additions & 0 deletions crates/theme-check/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ pub struct Checker {
checks: Option<Vec<&'static Check>>,
settings: Map<String, Value>,
ignore: Vec<String>,
threads: usize,
downloaded: docs::ThemeLiquidDocsManager,
}

Expand All @@ -297,6 +298,7 @@ impl Checker {
checks: None,
settings: Map::new(),
ignore: Vec::new(),
threads: 0,
downloaded: docs::ThemeLiquidDocsManager::new(Box::new(|_| {})),
}
}
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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<Vec<ThreadId>> = 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");
}
}
}
7 changes: 5 additions & 2 deletions crates/theme-check/src/node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ pub(crate) fn run<R>(
&Uris(&Disk),
docs.docset(),
schemas,
0,
);
let result = after(&theme, &offenses);
let theme = theme.iter().map(|f| f.uri.clone()).collect();
Expand All @@ -389,7 +390,8 @@ pub(crate) fn run<R>(
}

/// 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,
Expand All @@ -400,6 +402,7 @@ pub(crate) fn check_theme(
fs: &dyn FileSystem,
docset: &ThemeDocset,
schemas: &[Schema],
threads: usize,
) -> Vec<Offense> {
// `JSONValidator.create(themeLiquidDocsManager, config)`, whose `isValid` the schema getters
// use, and the one `check()` makes again for `validateJSON`. Two instances, as in TS: schema
Expand Down Expand Up @@ -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
}

// ---------------------------------------------------------------------------------------------
Expand Down
44 changes: 23 additions & 21 deletions crates/theme-check/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<Minimatch>> = config
.checks
Expand All @@ -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 {
Expand Down
Loading