Skip to content

Commit 22ce96d

Browse files
committed
Return empty list on invalid globs
Regression was introduced in 9479c28. 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 1f95878 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 to return the empty set again when encountering an invalid glob. We should especially restore the previous behaviour, as this caused a lot of regressions already, including in zola. Fixes getzola/zola#2150 ref: Keats#819 ref: Keats#820 ref: Keats#799
1 parent 1f95878 commit 22ce96d

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

src/tera.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,13 @@ impl Tera {
132132
// See https://github.com/Keats/tera/issues/574 for the Tera discussion
133133
// and https://github.com/Gilnaa/globwalk/issues/28 for the upstream issue.
134134
let (parent_dir, glob_end) = glob.split_at(glob.find('*').unwrap());
135-
let parent_dir = std::fs::canonicalize(parent_dir)?;
135+
let parent_dir = match std::fs::canonicalize(parent_dir) {
136+
Ok(d) => d,
137+
// If canonicalize fails, just abort it and resume with the given path.
138+
// Consumers expect invalid globs to just return the empty set instead of failing.
139+
// See https://github.com/Keats/tera/issues/819#issuecomment-1480392230
140+
Err(_) => std::path::PathBuf::from(parent_dir),
141+
};
136142
let dir = parent_dir.join(glob_end).into_os_string().into_string().unwrap();
137143

138144
// We are parsing all the templates on instantiation
@@ -1222,9 +1228,10 @@ mod tests {
12221228

12231229
// https://github.com/Keats/tera/issues/819
12241230
#[test]
1225-
fn doesnt_panic_on_invalid_glob() {
1231+
fn empty_list_on_invalid_glob() {
12261232
let tera = Tera::new("\\dev/null/*");
12271233
println!("{:?}", tera);
1228-
assert!(tera.is_err());
1234+
assert!(tera.is_ok());
1235+
assert!(tera.unwrap().templates.is_empty());
12291236
}
12301237
}

0 commit comments

Comments
 (0)