Skip to content

Commit 12ec314

Browse files
committed
Error out if glob passed to Tera::new starts with ./
This is because globwalk always returns an empty list in that case, which is likely not what users expect. The corresponding issue in globwalk is Gilnaa/globwalk#28. See Keats#574 for more discussion.
1 parent 2c9c8d9 commit 12ec314

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

src/tera.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,15 @@ impl Tera {
114114
if self.glob.is_none() {
115115
return Err(Error::msg("Tera can only load from glob if a glob is provided"));
116116
}
117+
118+
let dir = self.glob.clone().unwrap();
119+
120+
if dir.starts_with("./") || dir.starts_with("../") {
121+
// Because globwalk has issues with globs starting with ./ or ../
122+
// jee https://github.com/Keats/tera/issues/574
123+
return Err(Error::msg("Globs starting with './' or '../' are not supported, please canonicalize the path."));
124+
}
125+
117126
// We want to preserve templates that have been added through
118127
// Tera::extend so we only keep those
119128
self.templates = self
@@ -125,14 +134,9 @@ impl Tera {
125134

126135
let mut errors = String::new();
127136

128-
let dir = self.glob.clone().unwrap();
129137
// We clean the filename by removing the dir given
130138
// 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-
}
139+
let parent_dir = dir.split_at(dir.find('*').unwrap()).0;
136140

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

0 commit comments

Comments
 (0)