askama icon indicating copy to clipboard operation
askama copied to clipboard

Optional CARGO_MANIFEST_DIR

Open Ahmed-Ali opened this issue 3 years ago • 10 comments

In askama_shared/src/lib.rs#L36 this will cause the Template macro to fail if the CARGO_MANIFEST_DIR doesn't exist; This works well with the default rustc tool chain; however, this also means if for whatever reason CARGO_MANIFEST_DIR doesn't exist, the crate won't work. (proc-macro panic)

Any chance this can be optional instead?

Ahmed-Ali avatar Apr 22 '21 10:04 Ahmed-Ali

What do you think it should do if the manifest dit is not available?

djc avatar Apr 22 '21 10:04 djc

Looking at the code now, doesn't seem like it is reasonable to be able to have optional "Root" path; but maybe we can provide it through a template parameter or something; currently we can provide path relative to the manifest root dir, but maybe alternatively we can provide an absolute path?

which I guess will be way less portable -_-

Ahmed-Ali avatar Apr 22 '21 15:04 Ahmed-Ali

What is your use case anyway? What build system are you using if not Cargo? It doesn't seem too big of a deal to just set CARGO_MANIFEST_DIR in your environment in some other way.

djc avatar Apr 22 '21 15:04 djc

We are using BUCK; there is away to set the env variables in there; but it felt bit hacky; yet I can't think for better solution; the absolute path doesn't seem like an improvement

Ahmed-Ali avatar Apr 22 '21 17:04 Ahmed-Ali

If there is some other environment variable in a Buck context that we can work off of, I'd be open to having that as a fallback.

djc avatar Apr 22 '21 18:04 djc

doesn't seem like we have an equivalent really, for some reason. The only way that worked just fine, is to create a filegroup buck target, set its location is the value of CARGO_MANIFEST_DIR and place the templates there. Yet the challenge here is any other project uses askama (directly or transitively, might be required to do some additional work arounds).

Ahmed-Ali avatar Apr 22 '21 22:04 Ahmed-Ali

few folks suggested to use include_str instead. Also another approach might be to avoid invoking this code path if one uses source instead of path in the macro; with some document mentioning that path depends on CARGO_MANIFEST_DIR

Ahmed-Ali avatar Apr 23 '21 08:04 Ahmed-Ali

Now I'm wondering if include_str!() works in attribute values...

djc avatar Apr 23 '21 21:04 djc

I'm not entirely up to date on this, but as far as I recall using e.g. include_str!() in an attribute is possible, but it is unstable and requires enabling the extended_key_value_attributes feature. See https://github.com/rust-lang/rust/issues/78835 and https://github.com/rust-lang/rust/pull/78837.

vallentin avatar Apr 24 '21 13:04 vallentin

You can use include_str in attributes on stable through a declarative macro, I believe. It's a bit of a hack but it works well. See https://github.com/facebookincubator/cargo-guppy/blob/main/tools/determinator/src/rules.rs#L159-L187 for an example, and https://docs.rs/determinator/0.4.0/determinator/rules/struct.DeterminatorRules.html#associatedconstant.DEFAULT_RULES_TOML as the result.

sunshowers avatar Apr 26 '21 20:04 sunshowers