Skip to content

Commit

Permalink
Return empty list on invalid globs
Browse files Browse the repository at this point in the history
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
  • Loading branch information
vimpostor committed Apr 6, 2023
1 parent 1f95878 commit 22ce96d
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/tera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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());
}
}

0 comments on commit 22ce96d

Please sign in to comment.