Implemented `build.build-dir` config option by ranger-ross · Pull Request #15104 · rust-lang/cargo

@ranger-ross ranger-ross changed the title Added build-directory unstable feature flag Implemented build.build-dir config option

Jan 26, 2025

weihanglo

epage

epage

epage

epage

@epage epage mentioned this pull request

Jan 27, 2025

22 tasks

epage

epage

epage

epage

epage

epage

These ares are in preparation to split target-dir into artifact-dir and build-dir

@ranger-ross

This is in preparation for splitting the intermediate build artifacts
from the `target` directory.
This commit adds a `build_dir` option to the `build` table in
`config.toml` and adds the equivalent field to `Workspace` and `GlobalContext`.

@ranger-ross

This commits implements the seperation of the intermidate artifact
directory (called "build directory") from the target directory. (see rust-lang#14125)

@epage epage mentioned this pull request

Feb 26, 2025

bors added a commit to rust-lang-ci/rust that referenced this pull request

Feb 28, 2025

JeillZhang pushed a commit to JeillZhang/cargo that referenced this pull request

Mar 17, 2025
### What does this PR try to resolve?

This PR is a follow up on rust-lang#15104 and and adds support for the path
templating in `build.build-dir` as defined in rust-lang#14125.

Supported templates:
* `{workspace-root}`
* `{cargo-cache}` (pointing to `CARGO_HOME` for now)
* `{workspace-manifest-path-hash}`

#### Unresolved questions

What should we name `{workspace-manifest-path-hash}` and what should it
include? Should we shorten to `{workspace-hash}` or even just `{hash}`?
Should we include the Cargo version so we get unique whole-target
directories for easier cleanup (rust-lang#13136)

How should this handle unknown variables (error) or unclosed `{` / `}`
(ignored), see
rust-lang#15236 (comment)

When using `{workspace-manifest-path-hash}` this hash will change based
on the project path. In the event of a cargo being executed in a
symlinked project, the hash will change.

For example, given the following directory
```
/Users/
└─ user1/
    └─ projects/
        ├─ actual-crate/
        │  └─ Cargo.toml
        └─ symlink-to-crate/ -> actual-crate/
```

the hash will be unique when running cargo from each of the following
directories.
* `/Users/user1/actual-crate`
* `/Users/user1/symlink-to-crate`

Figuring out whether to handle this is deferred out, see
- rust-lang#15236 (comment)
-
https://github.com/poliorcetics/rfcs/blob/cargo-target-directories/text/3371-cargo-target-dir-templates.md#symbolic-links
-
rust-lang#12207 (comment)

### How should we test and review this PR?

This PR is fairly small. I included tests for each template variable.

You can also clone my branch and test it locally with
```console
CARGO_BUILD_BUILD_DIR='{workspace-root}/foo' cargo -Z build-dir build
```

### Additional information

While searching Cargo for any prior art for path templating, I found
[`sources/registry/download.rs`](https://github.com/rust-lang/cargo/blob/master/src/cargo/sources/registry/download.rs#L84)
doing a simple string replace. Thus I followed the same behavior.

r? @epage

@sylfn sylfn mentioned this pull request

Jun 3, 2025

11 tasks