Skip to content

Commit 7bb748a

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 7bb748a

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

src/tera.rs

+12-11
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,11 @@ 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() {
115-
return Err(Error::msg("Tera can only load from glob if a glob is provided"));
116-
}
114+
let glob = match &self.glob {
115+
Some(g) => g,
116+
None => return Err(Error::msg("Tera can only load from glob if a glob is provided")),
117+
};
118+
117119
// We want to preserve templates that have been added through
118120
// Tera::extend so we only keep those
119121
self.templates = self
@@ -125,14 +127,13 @@ impl Tera {
125127

126128
let mut errors = String::new();
127129

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-
}
130+
// Need to canonicalize the glob path because globwalk always returns
131+
// an empty list for paths starting with `./` or `../`.
132+
// See https://github.com/Keats/tera/issues/574 for the Tera discussion
133+
// and https://github.com/Gilnaa/globwalk/issues/28 for the upstream issue.
134+
let (parent_dir, glob_end) = glob.split_at(glob.find('*').unwrap());
135+
let parent_dir = std::fs::canonicalize(parent_dir).unwrap();
136+
let dir = parent_dir.join(glob_end).into_os_string().into_string().unwrap();
136137

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

0 commit comments

Comments
 (0)