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-cancelable.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'theme-check-rs': patch
---

Add `Checker::cancelable`, for a run that stops between files, and `Error::is_cancelled`
3 changes: 3 additions & 0 deletions crates/theme-check/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
142 changes: 121 additions & 21 deletions crates/theme-check/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,21 +161,46 @@ 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<String>) -> 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)
}
}

impl std::error::Error for Error {}

impl From<node::JsError> for Error {
fn from(e: node::JsError) -> Self {
Error(e.message)
Error::new(e.message)
}
}

Expand Down Expand Up @@ -251,14 +276,14 @@ impl Docs {
system_translations: &str,
) -> Result<Self, Error> {
let parse = |name: &str, json: &str| {
serde_json::from_str::<Value>(json).map_err(|e| Error(format!("{name}: {e}")))
serde_json::from_str::<Value>(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)?,
Expand Down Expand Up @@ -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<Path>) -> Result<Report, Error> {
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<Path>) -> Result<Report, Error> {
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| {
Expand All @@ -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<String> = paths
.into_iter()
Expand All @@ -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::<Result<Vec<_>, _>>()?;
let theme: Vec<SourceCode> = 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::<Result<Vec<SourceCode>, _>>()?;
// 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,
Expand All @@ -424,19 +499,23 @@ impl Checker {
&fs::Uris(fs),
docset,
schemas,
self.threads,
);
checker.threads,
cancelled,
)
.ok_or_else(Error::cancelled)?;
Ok(report(&theme, &offenses))
}
}

#[cfg(test)]
mod tests {
use super::*;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Mutex;
use std::thread::ThreadId;

static THREADS: Mutex<Vec<ThreadId>> = Mutex::new(Vec::new());
static COUNTED: AtomicUsize = AtomicUsize::new(0);

struct RecordThread;

Expand All @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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);
}
}
15 changes: 11 additions & 4 deletions crates/theme-check/src/node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,9 @@ pub(crate) fn run<R>(
docs.docset(),
schemas,
0,
);
&|| false,
)
.unwrap_or_default();
let result = after(&theme, &offenses);
let theme = theme.iter().map(|f| f.uri.clone()).collect();
Ok((
Expand All @@ -391,7 +393,8 @@ pub(crate) fn run<R>(

/// 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,
Expand All @@ -403,7 +406,8 @@ pub(crate) fn check_theme(
docset: &ThemeDocset,
schemas: &[Schema],
threads: usize,
) -> Vec<Offense> {
cancelled: &(dyn Fn() -> bool + Sync),
) -> Option<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
// resolution state is per validator.
Expand Down Expand Up @@ -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)
}

// ---------------------------------------------------------------------------------------------
Expand Down
16 changes: 12 additions & 4 deletions crates/theme-check/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<CheckOutput> {
let ignores: Vec<Vec<Minimatch>> = config
.checks
.iter()
Expand All @@ -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)));
Expand All @@ -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> {
Expand Down
4 changes: 2 additions & 2 deletions crates/theme-check/tests/specs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()),
Expand Down
Loading