Skip to content

Regression introduced by #546 when the derive macro is called from within another macro #706

@cipriancraciun

Description

@cipriancraciun

Describe the bug

The pull request #546, specifically the commit 6035ffb, introduced a regression that results in a build failure when the template (i.e. #[derive(Template)]) is actually generated from within a user defined macro, that happens to live in a different location than the file where the macro is expanded into.

To Reproduce

Here is the short version:

  • have a macro defined in a file that calls #[derive(Template)];
  • have a module that uses the above macro;
  • if the macro file doesn't live in the same folder as the module file, then the computed relative path by rel_path for the include_bytes (in the above mentioned commit) is wrong;
  • this is because the path is computed relative to the macro file, not relative to the final module file where the #[derive(Template)] is actually expanded into, and which the rustc will examine;

When can such a mismatch happen:

  • the macro file and the module file live in different folders, at different depths, but inside the same crate; (as the minimal reproducible example below;)
  • when the macro file is part of another crate, and the module file is part of another crate;
cargo init --lib askama-bugs
cd ./askama-bugs
cargo add askama

mkdir ./templates
touch ./templates/empty.txt

cat > ./src/m.rs << 'EOS'
#[ macro_export ]
macro_rules! define_template {
    () => {
        #[ derive (askama::Template) ]
        #[ template (path = "empty.txt") ]
        struct Empty {}
    }
}
EOS

mkdir ./src/a
cat > ./src/a/a.rs << 'EOS'
crate::define_template!();
EOS

cat >| ./src/lib.rs << 'EOS'

#[ path = "./a/a.rs" ]
mod a;

mod m;

EOS

cargo check

error: couldn't read `src/./a/../templates/empty.txt`: No such file or directory (os error 2)
 --> src/m.rs:4:20
  |
4 |         #[ derive (askama::Template) ]
  |                    ^^^^^^^^^^^^^^^^
  |
 ::: src/./a/a.rs:1:1
  |
1 | crate::define_template!();
  | ------------------------- in this macro invocation

Askama version
v0.15.4

Rust version
1.94.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions