Skip to content

Commit

Permalink
Always canonicalize glob paths passed to Tera
Browse files Browse the repository at this point in the history
This is to work around an issue in globwalk
(Gilnaa/globwalk#28) due to which paths
starting with `./` or `../` never match anything.

This fixes #574.
  • Loading branch information
eguiraud committed Jan 26, 2023
1 parent 957cd8d commit 27e5a28
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions src/tera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,10 @@ impl Tera {

/// Loads all the templates found in the glob that was given to Tera::new
fn load_from_glob(&mut self) -> Result<()> {
if self.glob.is_none() {
let Some(glob) = &self.glob else {
return Err(Error::msg("Tera can only load from glob if a glob is provided"));
}
};

// We want to preserve templates that have been added through
// Tera::extend so we only keep those
self.templates = self
Expand All @@ -125,14 +126,13 @@ impl Tera {

let mut errors = String::new();

let dir = self.glob.clone().unwrap();
// We clean the filename by removing the dir given
// to Tera so users don't have to prefix everytime
let mut parent_dir = dir.split_at(dir.find('*').unwrap()).0;
// Remove `./` from the glob if used as it would cause an error in strip_prefix
if parent_dir.starts_with("./") {
parent_dir = &parent_dir[2..];
}
// Need to canonicalize the glob path because globwalk always returns
// an empty list for paths starting with `./` or `../`.
// 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).unwrap();
let dir = parent_dir.join(glob_end).into_os_string().into_string().unwrap();

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

0 comments on commit 27e5a28

Please sign in to comment.