Skip to content

derive: track included files with relative path#546

Merged
GuillaumeGomez merged 2 commits intoaskama-rs:masterfrom
Kijewski:pr-relative-path
Jul 31, 2025
Merged

derive: track included files with relative path#546
GuillaumeGomez merged 2 commits intoaskama-rs:masterfrom
Kijewski:pr-relative-path

Conversation

@Kijewski
Copy link
Copy Markdown
Member

@Kijewski Kijewski commented Jul 27, 2025

To make the compiler understand which files contributed to the generated code, in order to make it aware when a recompilation is needed, we track the file paths using include_bytes!() calls in the generated code. This can be a problem for reproducible builds, because the working directory contributes to the file hash.

This PR makes Askama generate relative paths if possible. This is always possible on Unix systems, but on Windows hosts absolute paths have to be used if the code is kept on different drives.

Span::local_file() is only stable since rust 1.88, but that's our MSRV for the next release, anyway.

Open questions:

  • [ ] Should this behavior be feature gated?
  • path_clean::clean() and pathdiff:diff_paths() are tiny, and both MIT OR Apache-2.0 licensed. Should we simply copy the code (mentioning the authors, of course)?

Resolves #461.
Cc @frankdavid.

@Kijewski Kijewski added the derive Related to the code generating label Jul 27, 2025
Comment thread askama_derive/src/generator.rs Fixed
@GuillaumeGomez
Copy link
Copy Markdown
Collaborator

  • Should this behavior be feature gated?

Is there any reason why it should?

I'd say yes. One less dependency and as long as we mention where the code comes from, it gives us more liberty to adapt it more to ours needs if needed.

@Kijewski
Copy link
Copy Markdown
Member Author

Is there any reason why it should?

Not really, no. :) If the new import path handling was feature gated, then the new dependencies could be optional. But if we copy the two functions, then there are not new dependencies, so no feature-gate is needed.

The function `clean()` was copied from the project [`path_clean`] in version 1.0.1 (rev. [`d8948ae`]).
License: MIT OR Apache-2.0.
Authors: Dan Reeves <hey@danreev.es>, Alex Guerra <alex@heyimalex.com>, Adam Reichold

The function `diff_paths()` was copied in from the project [`pathdiff`] in version 0.2.3 (rev. [`5180ff5`]).
License: MIT OR Apache-2.0.
Copyright 2012-2015 The Rust Project Developers.

Please see their commit history for more information.

[`path_clean`]: <https://github.com/danreeves/path-clean>
[`pathdiff`]: <https://github.com/Manishearth/pathdiff>
[`d8948ae`]: <https://github.com/danreeves/path-clean/blob/d8948ae69d349ec33dfe6d6b9c6a0fe30288a117/src/lib.rs#L50-L86>
[`5180ff5`]: <https://github.com/Manishearth/pathdiff/blob/5180ff5b23d9d7eef0a14de13a3d814eb5d8d65c/src/lib.rs#L18-L86>
To make the compiler understand which files contributed to the generated
code, in order to make it aware when a recompilation is needed, we track
the file paths using `include_bytes!()` calls in the generated code.
This can be a problem for reproducible builds, because the working
directory contributes to the file hash.

This PR makes Askama generate relative paths if possible. This is always
possible on Unix systems, but on Windows hosts absolute paths have to be
used if the code is kept on different drives.
@GuillaumeGomez
Copy link
Copy Markdown
Collaborator

Thanks!

@GuillaumeGomez GuillaumeGomez merged commit d12afff into askama-rs:master Jul 31, 2025
42 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

derive Related to the code generating

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Absolute template paths cause build determinism issues

3 participants