Skip to content
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

attach(rocket_dyn_templates::Template::fairing()) panics with open square bracket in project path #2627

Closed
Familex opened this issue Oct 14, 2023 · 1 comment
Labels
bug Deviation from the specification or expected behavior

Comments

@Familex
Copy link

Familex commented Oct 14, 2023

Description

If project path looks like D:\folder[\rocket-project\, then call rocket::build().attach(Template::fairing()) crashes program with this error:

> cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.53s
     Running `target\debug\rocket-issue.exe`
thread 'main' panicked at C:\Users\User\.cargo\registry\src\index.crates.io-6f17d22bba15001f\rocket_dyn_templates-0.1.0-rc.3\src\context.rs:43:47:
called `Result::unwrap()` on an `Err` value: PatternError { pos: 9, msg: "invalid range pattern" }
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
error: process didn't exit successfully: `target\debug\rocket-issue.exe` (exit code: 101)

To Reproduce

Absolute path to project must contain open square bracket. Symlinks will be ignored.

Cargo.toml

[package]
name = "rocket-issue"
version = "0.1.0"
edition = "2021"

[dependencies]
rocket = "0.5.0-rc.3"

[dependencies.rocket_dyn_templates]
version = "0.1.0-rc.3"
features = ["handlebars"]
# same with "tera"

main.rs

#[rocket::launch]
fn rocket() -> _ {
    rocket::build().attach(rocket_dyn_templates::Template::fairing())
}

Expected Behavior

I don't think this bracket should be considered as special character, and app must work normally.

Environment:

  • OS Distribution and Kernel: Windows 10 Home 22H2, Arch Linux x86_64 6.5.7-arch1-1 (same behavior on both systems)
  • Rocket Version: 0.5.0-rc.3
  • rocket_dyn_templates version: 0.1.0-rc.3

Additional Context

With path looks like D:\[folder]\rocket-project\ app runs normally, but can't find any templates automatically by Template::fairing (writes Known templates: .). File addition via Template::custom(|t| t.tera.add_template_files(...)) works.

@Familex Familex added the triage A bug report being investigated label Oct 14, 2023
@SergioBenitez SergioBenitez added bug Deviation from the specification or expected behavior and removed triage A bug report being investigated labels Oct 14, 2023
@SergioBenitez
Copy link
Member

We use the path as a prefix to a string we construct and parse as a glob pattern. In this case, the path contains special characters that are recognized by the glob pattern parser but are ultimately used incorrectly, resulting in this error.

Looking at the relevant piece of code, what we're doing here is rather odd:

            let mut glob_path = root.join("**").join("*");
            glob_path.set_extension(ext);
            let glob_path = glob_path.to_str().expect("valid glob path string");

We effectively use glob to find all of the files in a given directory with a certain extension. Using glob for this feels like overkill and has resulted in the bug here. Instead, we simply need to walk the directory (e.g., using the walkdir crate) and filter out files that don't have the extension we're looking for. That would be simpler and avoid the problems here. I'll work on making this change now.

SergioBenitez added a commit that referenced this issue Oct 27, 2023
Previously, `dyn_templates` walked the user-provided `template_dir` path by
constructing a glob pattern prefixed with `template_dir`. If `template_dir`
contained characters recognized by the glob pattern parser, then at best the
pattern failed to parse, and at worst, incorrect directories were searched.

This commit removes the use of `glob` to walk the templates directory and
instead uses `walkdir`, obviating the issues described above.

Fixes #2627.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Deviation from the specification or expected behavior
Projects
None yet
Development

No branches or pull requests

2 participants