-
Notifications
You must be signed in to change notification settings - Fork 984
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Regression] Doesn't build without top-level templates directory #2150
Comments
This is extremely unfriendly, especially since you can't check an empty folder into git. We can do better then just ignoring all errors here. At the very least, we can check the err kind and apply defaults only if the expected file is missing: components/templates/src/lib.rs | 13 ++++++++++---
1 file changed, 10 insertions(+), 3 deletions(-)
diff --git a/components/templates/src/lib.rs b/components/templates/src/lib.rs
index 6441ff49..7f1aaa53 100644
--- a/components/templates/src/lib.rs
+++ b/components/templates/src/lib.rs
@@ -5,7 +5,7 @@ use std::path::Path;
use config::Config;
use libs::once_cell::sync::Lazy;
-use libs::tera::{Context, Tera};
+use libs::tera::{self, Context, Tera};
use errors::{bail, Context as ErrorContext, Result};
use utils::templates::rewrite_theme_paths;
@@ -47,8 +47,15 @@ pub fn load_tera(path: &Path, config: &Config) -> Result<Tera> {
// Only parsing as we might be extending templates from themes and that would error
// as we haven't loaded them yet
- let mut tera =
- Tera::parse(&tpl_glob).context("Error parsing templates from the /templates directory")?;
+ let mut tera = match Tera::parse(&tpl_glob) {
+ Ok(t) => t,
+ Err(e) if matches!(e.kind, tera::ErrorKind::Io(std::io::ErrorKind::NotFound)) => {
+ Tera::default()
+ }
+ Err(e) => {
+ return Err(e).context("Error parsing templates from the /templates directory");
+ }
+ };
if let Some(ref theme) = config.theme {
// Test that the templates folder exist for that theme I'm going to open a PR. |
You can, you just need to add a dummy file in it (usually an empty .gitkeep by convention). I agree it needs to be fixed though |
As a supplement: This error also occurs if a theme is defined. Then the template directory should generally not matter, or? |
You can overwrite any templates of a theme so we need to load both.
…On Thu, 30 Mar 2023, 01:10 Sven Jörns, ***@***.***> wrote:
As a supplement: This error also occurs if a theme is defined. Then the
template directory should generally not matter, or?
—
Reply to this email directly, view it on GitHub
<#2150 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAFGDI7LQP7AUFASIKDA34TW6S6NDANCNFSM6AAAAAAWFZVE2Y>
.
You are receiving this because you commented.Message ID:
***@***.***>
|
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. Even more so, since this caused a lot of regressions already, including in zola. Fixes getzola/zola#2150 ref: Keats#819 ref: Keats#820 ref: Keats#799
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
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. 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
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. 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
- Because of <getzola/zola#2150>
Since version 0.17.1 zola requires the top-level
/templates
directory to exist, even if a theme is used. Otherwise it will fail with:This is because now tera handles globs differently, as discussed in Keats/tera#819 (comment).
I believe that this is not very user-friendly, as it can quickly lead to unexpected build failures with the default setup as follows:
zola init mysite
zola serve
index.html
or add a theme to useIn my opinion this is not optimal, and zola should still work if there is no top-level templates directory.
The following patch fixes the problem from zola's side:
However it's not really a good solution, because it will shadow actual parsing problems if
/templates
exists and there are real problems. This is why I believe this problem is better fixed on tera's side, i.e. making invalid globs just return the empty set instead of an error, which is how it is done in most languages.The text was updated successfully, but these errors were encountered: