Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using assets in a crate #11683

Closed
Inspirateur opened this issue Feb 5, 2023 · 7 comments
Closed

Using assets in a crate #11683

Inspirateur opened this issue Feb 5, 2023 · 7 comments
Labels
C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted`

Comments

@Inspirateur
Copy link

Inspirateur commented Feb 5, 2023

Problem

I'm writing a crate for crates.io that needs to use assets and none of these solutions satisfy me:

  • embed them in the binary -> too heavy
  • have the user provide them to my crate -> I don't want the user to bother with that
  • have my crate download them at runtime -> too slow

That seemed like a simple need but after hours of searching I still couldn't find anything except this post https://stackoverflow.com/questions/74606543/ship-tera-templates-in-a-crate-without-embedding-them-into-the-binary
with 0 replies.

Proposed Solution

If there is any way to achieve that it would be nice to have it clearly documented somewhere (I couldn't find anything about it in the cargo book either). If there is not, it would be great to add a way for my crate to use its own assets, or explain somewhere why this is not possible/desirable.

Notes

No response

@Inspirateur Inspirateur added the C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` label Feb 5, 2023
@arlosi
Copy link
Contributor

arlosi commented Feb 6, 2023

You can include files in the crate package - see the include manifest key for details on what's included by default. Then access the path to the files relative to the CARGO_MANIFEST_DIR environment variable .

let path_to_crate= env!("CARGO_MANIFEST_DIR");

Note that crates.io has a 10MB limit on the .crate file.

@Inspirateur
Copy link
Author

Thank you ! that's exactly what I needed ❤

@epage
Copy link
Contributor

epage commented Feb 6, 2023

Note that that doesn't help with cargo install. Asset handling for cargo install is being tracked in #2729.

@Inspirateur
Copy link
Author

You can include files in the crate package - see the include manifest key for details on what's included by default.

If i understand the doc correctly, an assets/ folder at the root of the crate that is not in the gitignore would be included by default ?

@weihanglo
Copy link
Member

If i understand the doc correctly, an assets/ folder at the root of the crate that is not in the gitignore would be included by default ?

I believe so. You can also use cargo package --list to verify what will be included in the .crate tarball.

@schneems
Copy link

This is great, thanks! Did you verify if this technique works? I think we can close the issue if so.

@Inspirateur
Copy link
Author

Yes ! I made this crate using this technique that was perfect for my needs https://github.com/Inspirateur/twemoji-rs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted`
Projects
None yet
Development

No branches or pull requests

5 participants