Skip to content

Commit 27e5a28

Browse files
committed
Always canonicalize glob paths passed to Tera
This is to work around an issue in globwalk (Gilnaa/globwalk#28) due to which paths starting with `./` or `../` never match anything. This fixes Keats#574.
1 parent 957cd8d commit 27e5a28

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

src/tera.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,10 @@ impl Tera {
111111

112112
/// Loads all the templates found in the glob that was given to Tera::new
113113
fn load_from_glob(&mut self) -> Result<()> {
114-
if self.glob.is_none() {
114+
let Some(glob) = &self.glob else {
115115
return Err(Error::msg("Tera can only load from glob if a glob is provided"));
116-
}
116+
};
117+
117118
// We want to preserve templates that have been added through
118119
// Tera::extend so we only keep those
119120
self.templates = self
@@ -125,14 +126,13 @@ impl Tera {
125126

126127
let mut errors = String::new();
127128

128-
let dir = self.glob.clone().unwrap();
129-
// We clean the filename by removing the dir given
130-
// to Tera so users don't have to prefix everytime
131-
let mut parent_dir = dir.split_at(dir.find('*').unwrap()).0;
132-
// Remove `./` from the glob if used as it would cause an error in strip_prefix
133-
if parent_dir.starts_with("./") {
134-
parent_dir = &parent_dir[2..];
135-
}
129+
// Need to canonicalize the glob path because globwalk always returns
130+
// an empty list for paths starting with `./` or `../`.
131+
// See https://github.com/Keats/tera/issues/574 for the Tera discussion
132+
// and https://github.com/Gilnaa/globwalk/issues/28 for the upstream issue.
133+
let (parent_dir, glob_end) = glob.split_at(glob.find('*').unwrap());
134+
let parent_dir = std::fs::canonicalize(parent_dir).unwrap();
135+
let dir = parent_dir.join(glob_end).into_os_string().into_string().unwrap();
136136

137137
// We are parsing all the templates on instantiation
138138
for entry in glob_builder(&dir)

0 commit comments

Comments
 (0)