Allow using generated assets#5490
Conversation
f90194e to
570e044
Compare
|
We do need to expand the path check to allow the out dir path for assets, but we can do this without extra syntax. We support the same macros inside the path as const GENERATED_ASSET: Asset = asset!(concat!(env!("OUT_DIR"), "/file.css"));Linking this pr with #4426 |
|
That's what I originally imagined would be possible, but like the linked issue, I was met with resistance. Making no changes to the macro syntax obviously has an upside—even if the changes are optional. Checking if an absolute path resolves to an actual file before trying it as a crate-relative one is easy enough, but in case no file is found, it's difficult to provide the user with correct and exact feedback about where the asset is supposed to be, because we can't reasonably tell whether they meant to use an absolute or a crate-relative path. It's also technically a breaking change because if I happen to have an The explicit resolution strategy also allows using In any case, if you'd like me to head in a different direction, please let me know, and I'll adjust the PR accordingly. I'd be more than happy to implement an alternative solution if that's what you think is best. |
|
If we resolve assets like this, it seems unlikely there would be much overlap: asset!("/assets/asset.txt"); // Resolved relative to the crate root because it starts with slash and isn't point to the dir `OUT_DIR` resolves to in target
asset!("./assets/asset.txt"); // Resolved relative to the current file because it starts with `./`
asset!(concat!(env!("OUT_DIR"), "/generated-asset.txt")); // Resolved relative to the output folder because the file starts with the output folderYou would need to have something like The restriction about which assets you can link to is intended to prevent builds passing locally, but failing elsewhere like when you publish to crates.io. For libraries, only inside the workspace or build directory should be allowed. |
|
I think I would prefer something like or where OUT_DIR and other env vars or hard-coded replacements can be passed through the macro system in a way that the proc macros can see. I lean towards the second approach a bit since it's cleaner and easy to parse. One issue that OUT_DIR for the proc macro might not be the same OUT_DIR for the target crate. I think a dtolnay crate has a solution for that somewhere |
e58061e to
48d85b3
Compare
|
@ealmloff @jkelleyrtp Thank you both for the feedback! I've rebased and updated to PR to address your comments. The updated version doesn't introduce any new syntax and still avoids breaking changes (except in the most unrealistic conditions). |
|
Looks like we may need to canonicalize both paths before checking if it starts with the out dir from the failing tests? Api looks good after that fix |
48d85b3 to
0f10459
Compare
|
I added canonicalization to the output directory path in the test context. Hopefully that'll do it! |
0f10459 to
fdc8889
Compare
Currently, all assets are resolved either relative to the current file (if the path begins with a
.) or the crate root (in all other cases).Changes
This PR introduces a new option for asset path resolution that enables reading generated assets.
The path resolution heuristics are as follows:
.or..both resolve relative to the current file;/ignored).I also updated the documentation of the macros, added some tests for new and existing behavior, and refactored the internal error handling to improve clarity and consistency.
Alternatives considered
Following are a number of alternatives considered before settling on the current solution.
Explicit path resolution strategy
Explicitly declaring the resolution base would make the code more explicit.
Skipping the explicit strategy would fall back to the original behavior by automatically detecting whether
crateorselfshould be used.This was ultimately dropped in response to #5490 (comment) to avoid introducing new syntax.
Prefix character
A special character (like
"!") at the beginning of the path could denote a generated path.While the likelihood of anyone having a local folder named
"!"is practically non-existent, it's still technically allowed by the operating system and thus this would constitute a breaking change. On Linux particularly, the only character that's not allowed in a file name is/.Separate macros
A new
generated_assetcould be introduced so no changes would need to be made to the existing ones. However, this would lead to needless duplication without any practical gains.