7

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.
eonil
  • 83,476
  • 81
  • 317
  • 516
  • What do you mean by "okay"? Did you try it? – trent Nov 02 '19 at 11:05
  • @trentcl I meant "okay" as "intended/designed to work with no trouble". Sorry for ambiguity. – eonil Nov 02 '19 at 12:59
  • I tried to use single `~/.build` directory to build several project and it's working well so far. But working for a few cases doesn't mean it would work well for every cases. Therefore, I am asking for original design intention of this flag if used with single shared build directory. – eonil Nov 02 '19 at 13:00
  • If the goal is just to speed up compilation with cached build artifacts, [sccache](https://github.com/mozilla/sccache) would probably be a better option. – apetranzilla Nov 02 '19 at 13:57
  • 2
    I'm with @apemanzilla on this one. You're trying to solve a problem with a false solution, and creating two more problems along the way. In particular, it will no longer be possible for you to build two projects with the same dependencies concurrently. – Sébastien Renauld Nov 02 '19 at 15:13

1 Answers1

3

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:

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
  • Why is sccache better? With a shared target directory I would expect any time any project tries to build a crate cargo checks if it's already in the target directory, and if it is then cargo does nothing. If using sccache instead, than cargo will think it does need to build, and start triggering a bunch of builds that will so happen to be very quickly satisfied by the cache, but I assume involve copying out of the cache and into your project specific target directory. That both sounds slower and like it would use more disk space. Is some part of this incorrect? Is it not ideal to just do both? – Joseph Garvin Aug 02 '20 at 22:23
  • 1
    I got here because I am trying to understand: why isn't a single shared target folder the default? Unlike sccache it would require no user setup and it seems like it would be purely better. – Joseph Garvin Aug 02 '20 at 22:25
  • @JosephGarvin as mentioned [in the comments](https://stackoverflow.com/questions/58669482/is-it-okay-to-use-a-single-shared-directory-as-cargos-target-directory-for-all/58880993#comment103646508_58669482), create project A and B that have dependency C, but use different target features for C. Then build A, B, A. Does A rebuild? Then try cleaning your target and build B, A, B. Then try cleaning and building A and B concurrently. – Shepmaster Aug 03 '20 at 12:48
  • 1
    Ah, so a shared target directory is not smart enough to separate crates by feature flags? If it were, would the scenario still workout as I describe? I'm trying to understand if "try changing cargo to put a feature hash in the directory name" is the best way to go or if there is 0 perf advantage that would have over sccache (I suspect it would still avoid copying). The linked issue seems to be about it used to be the case that you couldn't run two cargo instances period. – Joseph Garvin Aug 03 '20 at 21:11
  • How do I set `build.target-dir` on a workspace? Is that possible. I get no errors but also not the expected result. – Zingam Nov 21 '21 at 19:34