Skip to content

Commit 93ac6e1

Browse files
authored
fix(script): Tweak cargo script build-dir / target-dir (#16086)
### What does this PR try to resolve? This is meant to unblock efforts for allowing cargo scripts to use artifact names like `deps` without being sanitized or erroring. This is done by making the default `target-dir` exclude all `build-dir` content by putting the default cargo script `target-dir` under `build-dir`. Cargo script target-dir precedence | before | after| |-|-| | explicit `target-dir`| explicit `target-dir`| | `"{cargo-cache-home}/target/{workspace-path-hash}"` | **`"{build-dir}/target"`** | Cargo script build-dir precedence | before | after| |-|-| | explicit `build-dir`| explicit `build-dir`| | explicit `target-dir`| explicit `target-dir`| | `"{cargo-cache-home}/target/{workspace-path-hash}"` | `"{cargo-cache-home}/target/{workspace-path-hash}"` | In doing this split, it highlighted the fact that we still had `Cargo.lock` for cargo scripts in `target-dir` despite being an intermediate build artifact, so this also moves that into `build-dir`. This does mean that if someone has a shared `build-dir` between their projects, their scripts will get a bit confusing. I plan to highlight that in the tracking issue / stabilization report. ### How to test and review this PR? ## Notes Work left to do for artifact names - Stop creating `examples/` when its not used - Move the conflict check from parse time to build time - Make the conflict check dependent on if `target_dir == build_dir`
2 parents 021f167 + 9ece28b commit 93ac6e1

File tree

2 files changed

+66
-55
lines changed

2 files changed

+66
-55
lines changed

src/cargo/core/workspace.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -439,21 +439,32 @@ impl<'gctx> Workspace<'gctx> {
439439
}
440440

441441
pub fn build_dir(&self) -> Filesystem {
442-
self.build_dir.clone().unwrap_or_else(|| self.target_dir())
442+
self.build_dir
443+
.clone()
444+
.or_else(|| self.target_dir.clone())
445+
.unwrap_or_else(|| self.default_build_dir())
443446
}
444447

445448
fn default_target_dir(&self) -> Filesystem {
449+
if self.root_maybe().is_embedded() {
450+
self.build_dir().join("target")
451+
} else {
452+
Filesystem::new(self.root().join("target"))
453+
}
454+
}
455+
456+
fn default_build_dir(&self) -> Filesystem {
446457
if self.root_maybe().is_embedded() {
447458
let default = ConfigRelativePath::new(
448-
"{cargo-cache-home}/target/{workspace-path-hash}"
459+
"{cargo-cache-home}/build/{workspace-path-hash}"
449460
.to_owned()
450461
.into(),
451462
);
452463
self.gctx()
453464
.custom_build_dir(&default, self.root_manifest())
454465
.expect("template is correct")
455466
} else {
456-
Filesystem::new(self.root().join("target"))
467+
self.default_target_dir()
457468
}
458469
}
459470

0 commit comments

Comments
 (0)