1

I am experimenting with building a web app in Rust using Tera and there is a panic every time on the render() call below. Example of the chunk of code:

async fn login(tera: web::Data<Tera>) -> impl Responder {
    let mut data = Context::new();
    data.insert("title", "Login");

    let rendered = tera.render("login.html", &data).unwrap();
    HttpResponse::Ok().body(rendered)
}

Link to my repo to review: https://github.com/ClusterberrySquirrels/oasis/blob/oasis_db/src/main.rs

I get the following panic call:

thread 'actix-rt:worker:0' panicked at 'called `Result::unwrap()` on an `Err` value: Error { kind: TemplateNotFound("login.html"), source: None }', src/main.rs:101:53

Something fixed this panic when I was having the same problem at the beginning of this project on the index.html page. At first I thought it was Tera causing the issue but I didn't find any steps to fix it. Eventually something I did made it behave as intended and everything was fine until now. I would appreciate any advice on how to fix this and avoid it happening in the future.

File structure:

Project Folder\
  templates\
    login.html
    index.html
  src\
    main.rs

Trouble shooting on my side not necessarily in order:

  1. Switch toolchain default from stable -> cargo build/run, to nightly -> cargo build/run and back.
  2. rustup update
  3. sudo apt update
  4. sudo apt upgrade
  5. wsl --set-default-version 2
  6. cargo clean
  7. Delete target folder then cargo clean, cargo build, cargo run
  8. cargo update

Update to question: Minimum reproducible example.

use actix_web::{HttpServer, App, web, HttpResponse, Responder};
use tera::{Tera, Context};

async fn index(tera: web::Data<Tera>) -> impl Responder {
    let mut data = Context::new();

    let posts = [
        Post {
            title: String::from("This is the first link"),
            link: String::from("https://example.com"),
            author: String::from("Nutrition-Tracker"),
        },
        Post {
            title: String::from("This is the second Link"),
            link: String::from("https://example.com"),
            author: String::from("Other cool app"),
        },
    ];

    data.insert("title", "index");
    data.insert("posts", &posts);

    let rendered = tera.render("index.html", &data).unwrap(); // TemplateNotFound??
    HttpResponse::Ok().body(rendered)
}

async fn login(tera: web::Data<Tera>) -> impl Responder {
    let mut data = Context::new();
    data.insert("title", "Login");

    let rendered = tera.render("login.html", &data).unwrap(); // TemplateNotFound??
    HttpResponse::Ok().body(rendered)
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    HttpServer::new(|| {
        let tera = Tera::new("templates/**/*").unwrap(); 
        App::new()
            .data(tera)
            .route("/", web::get().to(index))
            .route("/login", web::get().to(login))
    })
        .bind("127.0.0.1:8000")?
        .run()
        .await
}

Matt H
  • 19
  • 2
  • 2
    A generic debugging technique for file not found errors, not Rust-specific, is to run your program with `strace -e file -f ` and see the low-level `open` syscall that's failing. `-e file` only shows file-related syscalls and `-f` traces child processes in case your program forks. This will tell you where it's looking for the file so you can confirm what directories it's searching. – John Kugelman Apr 01 '21 at 01:25
  • It's hard to answer your question because it doesn't include a [MRE]. We can't tell what crates (and their versions), types, traits, fields, etc. are present in the code. It would make it easier for us to help you if you try to reproduce your error on the [Rust Playground](https://play.rust-lang.org) if possible, otherwise in a brand new Cargo project, then [edit] your question to include the additional info. There are [Rust-specific MRE tips](//stackoverflow.com/tags/rust/info) you can use to reduce your original code for posting here. Thanks! – Shepmaster Apr 01 '21 at 01:37
  • John's removals are fine, but they highlight what the question doesn't provide — directions for anyone else to _reproduce the problem_. – Shepmaster Apr 01 '21 at 01:37
  • @Shepmaster - Here is the link for the Rust Playground: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=80dfaab8677f1d61783dea544a52cc42 – Matt H Apr 01 '21 at 02:56
  • @Shepmaster - However what you're asking isn't possible as the playground can't find the crates that I am using such as Tera, Diesel, and other modules. – Matt H Apr 01 '21 at 02:57
  • *try to reproduce your error on the Rust Playground **if possible, otherwise in a brand new Cargo project*** – Shepmaster Apr 01 '21 at 02:58
  • @JohnKugelman - there is RUST_TRACEBACK=1 that I can run it with however, there is no output for the error when I use that environment variable. This issue occurs during run time where the program compiles without error but as soon as I navigate to the local host to view the page, the panic call from my question appears and the html page doesn't show. – Matt H Apr 01 '21 at 03:05
  • @Shepmaster - I don't understand how to reproduce the error in the playground if it can't find the crates that I'm using. Starting a brand new project doesn't seem intuitive to do. This is my senior project and starting from scratch isn't the kind of solution I was looking for. Thanks. – Matt H Apr 01 '21 at 03:11
  • 1
    `cargo new` creates a new project. Add the bare minimum of things to cause the exact same problem. Then post that bare minimum here. Then any one else can reproduce the problem and can help you fix it. – Shepmaster Apr 01 '21 at 03:14
  • @Shepmaster - I provided a link to my repo for now and will work on the reproduction of the problem in a new project as you described. – Matt H Apr 01 '21 at 03:27
  • Trying to reproduce a problem in a new project is a great debugging technique. It's not just useful for posting here. It helps you eliminate all the unrelated stuff and focus on the parts that are broken. You don't have to start from scratch and build up; you could also copy your code to a new project and trim mercilessly. It helps to not have to keep the project "useful". You just need code that does something you can't explain, even if it's otherwise pointless. – John Kugelman Apr 01 '21 at 03:28
  • I updated my question with the minimum reproducible example to recreate. – Matt H Apr 01 '21 at 14:18

1 Answers1

-1

I figured out the issue. I am also running kubernetes for this application and the ports I was selecting were being occupied by kubernetes at random times. I killed all of the processes however, the problem still remains which is an issue for another thread.

Matt H
  • 19
  • 2