Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it okay to use a single shared directory as Cargo's target directory for all projects?

Cargo has the --target-dir flag which specifies a directory to store temporary or cached build artifacts. You also can set it user-wide in the ~/.cargo/config file. I'd like to set it to single shared directory to make maintenance easier.

I saw some artifact directories are suffixed with some unique(?) hashes in the target-dir which looks safe, but the final products are not suffixed with hashes, which doesn't seem to be safe for name clashes. I'm not sure on this as I am not an expert on Cargo.

I tried setting ~/.cargo/config to

[build]
target-dir = "./.build"

My original intention was to use the project's local ./.build directory, but somehow Cargo places all build files into ~/.build directory. I got curious what would happen I put all build files from every project into a single shared build directory.

It has worked well with several different projects so far, but working for a few samples doesn't mean it's designed or guaranteed to work with every case.

In my case, I am using single shared build directory for all projects of all workspaces of a user. Not only projects in a workspace. Literally every project in every workspace of a user. As far as I know, Cargo is designed to work with a local target directory. If it is designed to work with only local directory, a shared build directory is likely to cause some issues.

  • Rust/Cargo 1.38.0.
like image 311
eonil Avatar asked Nov 07 '22 12:11

eonil


1 Answers

Yes, this is intended to be safe.

I agree with the comments that there are probably better methods of achieving your goal. Workspaces are a simple solution for a small group of crates, and sccache is a more principled caching mechanism.

See also:

  • Fix running Cargo concurrently (PR #2486)
  • Allow specifying a custom output directory (PR #1657)
  • Can I prevent cargo from rebuilding libraries with every new project?
like image 131
Shepmaster Avatar answered Nov 17 '22 08:11

Shepmaster