From ce9a467a05739cd126396bddf5f935cce69bb74c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magnus=20Gro=C3=9F?= Date: Thu, 6 Apr 2023 08:40:16 +0200 Subject: [PATCH] Return empty list on invalid globs Regression was introduced in 9479c2801093d856811fda3660e50fb8be9d3f5c. Since that commit, providing an invalid glob to tera will make it fail to create an instance, first (in that commit) by making it panic and since 1f958784b8be7d8ccba443a19c1417f8ff48b329 by making it return an error. While returning an error is not entirely unlogical, it doesn't match what most languages do with invalid globs. - Bash will by default return an empty set on invalid globs, as the `failglob` option is off by default - Python will likewise return the empty set instead of throwing an exception, when doing something like `glob.glob("/dev/null/*")` - Rust's `globwalk` will also not error, but return an empty set In fact, we use globwalk in tera and the only reason we panic is by accident, because `std::fs::canonicalize()` checks the path. It is better to match other language's glob behaviour, therefore we resort back to the original path if `canonicalize()` fails. We should especially restore the previous behaviour, as this caused a lot of regressions already, including in zola. Fixes https://github.com/getzola/zola/issues/2150 ref: #819 ref: #820 ref: #799 --- src/tera.rs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/tera.rs b/src/tera.rs index bf29f6d8..81f9057b 100644 --- a/src/tera.rs +++ b/src/tera.rs @@ -132,7 +132,13 @@ impl Tera { // See https://github.com/Keats/tera/issues/574 for the Tera discussion // and https://github.com/Gilnaa/globwalk/issues/28 for the upstream issue. let (parent_dir, glob_end) = glob.split_at(glob.find('*').unwrap()); - let parent_dir = std::fs::canonicalize(parent_dir)?; + let parent_dir = match std::fs::canonicalize(parent_dir) { + Ok(d) => d, + // If canonicalize fails, just abort it and resume with the given path. + // Consumers expect invalid globs to just return the empty set instead of failing. + // See https://github.com/Keats/tera/issues/819#issuecomment-1480392230 + Err(_) => std::path::PathBuf::from(parent_dir), + }; let dir = parent_dir.join(glob_end).into_os_string().into_string().unwrap(); // We are parsing all the templates on instantiation @@ -1222,9 +1228,10 @@ mod tests { // https://github.com/Keats/tera/issues/819 #[test] - fn doesnt_panic_on_invalid_glob() { + fn empty_list_on_invalid_glob() { let tera = Tera::new("\\dev/null/*"); println!("{:?}", tera); - assert!(tera.is_err()); + assert!(tera.is_ok()); + assert!(tera.unwrap().templates.is_empty()); } }