Advice on adding arbitrary data to ~/.cargo

I'm currently working on a CLI built in Rust that needs to download several external tool dependencies (namely wasm-bindgen and wasm-opt). There are two places to put these for my program: either in the directory the user runs the CLI in (fine, but that would mean potentially multiple copies of the same tools on the user's system, which takes up unnecessary space), or in a system-wide cache.

Is it considered acceptable to put this system-wide cache inside ~/.cargo/ on the user's system? Specifically, ~/.cargo/perseus_tools/ (the name of the CLI is Perseus). It doesn't matter if this is cleared, since the tools can be trivially re-downloaded, I'm just wondering if there's any specific advice for/against this approach. (Because I could just as easily put it somewhere else.)

My 2 cents is that you probably shouldn't put your own project's stuff into other projects' locations. Unless .cargo is explicitly expected to be extended by anyone other than cargo itself, it can confuse tools and mess things up. You should just create your own ~/.perseus directory and initialize the cache there.

2 Likes

If you don't have backwards compatibility concerns I recommend following XDG Base Directory Specification. For instance, you may want to use $XDG_CACHE_HOME/perseus for your cache. directories crate can help with handling this in a portable manner.

5 Likes

Understood, I'll use the directories crate! Much appreciated! (Side note: is this documented anywhere? If not I might submit a PR on the Rust docs, as niche as this is.)

It's a convention that each OS uses when applications need to store data, and not really specific to Rust.

I'm also not sure where you'd put this, either. There used to be a note about directories in std::env::home_dir(), but that was removed because the standard library prefers to not play favourites with crates and I think there were questions about the crate's maintenance status (not that it really matters - these conventions haven't changed for decades so there's not much to iterate on).

1 Like