Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/cargo/core/compiler/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ use std::path::{Path, PathBuf};
pub struct Layout {
artifact_dir: Option<ArtifactDirLayout>,
build_dir: BuildDirLayout,
_lock: Option<FileLock>,
}

impl Layout {
Expand Down Expand Up @@ -281,6 +282,15 @@ impl Layout {
let deps = build_dest.join("deps");
let artifact = deps.join("artifact");

// We take a shared lock on `.cargo-lock` to make sure we don't run currently with
// older versions of Cargo (including tools that use Cargo as a library) that don't support
// `.cargo-build-lock`.
let lock = if is_on_nfs_mount(root.as_path_unlocked()) {
None
} else {
Some(dest.open_ro_shared_create(".cargo-lock", ws.gctx(), "artifact directory")?)
};

let artifact_dir = if must_take_artifact_dir_lock {
// For now we don't do any more finer-grained locking on the artifact
// directory, so just lock the entire thing for the duration of this
Expand All @@ -289,7 +299,7 @@ impl Layout {
None
} else {
Some(dest.open_rw_exclusive_create(
".cargo-lock",
".cargo-artifact-lock",
ws.gctx(),
"artifact directory",
)?)
Expand Down Expand Up @@ -320,6 +330,7 @@ impl Layout {
_lock: build_dir_lock,
is_new_layout,
},
_lock: lock,
})
}

Expand Down
14 changes: 14 additions & 0 deletions tests/testsuite/build_dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ fn binary_with_debug() {
.assert_build_dir_layout(str![[r#"
[ROOT]/foo/target-dir/CACHEDIR.TAG
[ROOT]/foo/target-dir/debug/.cargo-lock
[ROOT]/foo/target-dir/debug/.cargo-artifact-lock
[ROOT]/foo/target-dir/debug/foo[EXE]
[ROOT]/foo/target-dir/debug/foo.d

Expand Down Expand Up @@ -110,6 +111,7 @@ fn binary_with_release() {
.assert_build_dir_layout(str![[r#"
[ROOT]/foo/target-dir/CACHEDIR.TAG
[ROOT]/foo/target-dir/release/.cargo-lock
[ROOT]/foo/target-dir/release/.cargo-artifact-lock
[ROOT]/foo/target-dir/release/foo[EXE]
[ROOT]/foo/target-dir/release/foo.d

Expand Down Expand Up @@ -205,6 +207,7 @@ fn should_default_to_target() {
[ROOT]/foo/target/.rustc_info.json
[ROOT]/foo/target/CACHEDIR.TAG
[ROOT]/foo/target/debug/.cargo-lock
[ROOT]/foo/target/debug/.cargo-artifact-lock
[ROOT]/foo/target/debug/.cargo-build-lock
[ROOT]/foo/target/debug/build/foo/[HASH]/fingerprint/bin-foo
[ROOT]/foo/target/debug/build/foo/[HASH]/fingerprint/bin-foo.json
Expand Down Expand Up @@ -373,6 +376,7 @@ fn cargo_tmpdir_should_output_to_build_dir() {
.assert_build_dir_layout(str![[r#"
[ROOT]/foo/target-dir/CACHEDIR.TAG
[ROOT]/foo/target-dir/debug/.cargo-lock
[ROOT]/foo/target-dir/debug/.cargo-artifact-lock
[ROOT]/foo/target-dir/debug/foo[EXE]

"#]]);
Expand Down Expand Up @@ -419,6 +423,7 @@ fn examples_should_output_to_build_dir_and_uplift_to_target_dir() {
.assert_build_dir_layout(str![[r#"
[ROOT]/foo/target-dir/CACHEDIR.TAG
[ROOT]/foo/target-dir/debug/.cargo-lock
[ROOT]/foo/target-dir/debug/.cargo-artifact-lock
[ROOT]/foo/target-dir/debug/examples/foo[EXE]
[ROOT]/foo/target-dir/debug/examples/foo.d

Expand Down Expand Up @@ -471,6 +476,7 @@ fn benches_should_output_to_build_dir() {
.assert_build_dir_layout(str![[r#"
[ROOT]/foo/target-dir/CACHEDIR.TAG
[ROOT]/foo/target-dir/debug/.cargo-lock
[ROOT]/foo/target-dir/debug/.cargo-artifact-lock
[ROOT]/foo/target-dir/debug/foo[EXE]

"#]]);
Expand Down Expand Up @@ -528,6 +534,7 @@ fn cargo_package_should_build_in_build_dir_and_output_to_target_dir() {
p.root().join("build-dir").assert_build_dir_layout(str![[r#"
[ROOT]/foo/build-dir/.rustc_info.json
[ROOT]/foo/build-dir/debug/.cargo-lock
[ROOT]/foo/build-dir/debug/.cargo-artifact-lock
[ROOT]/foo/build-dir/debug/.cargo-build-lock
[ROOT]/foo/build-dir/debug/build/foo/[HASH]/fingerprint/bin-foo
[ROOT]/foo/build-dir/debug/build/foo/[HASH]/fingerprint/bin-foo.json
Expand Down Expand Up @@ -625,6 +632,7 @@ fn cargo_clean_should_clean_the_target_dir_and_build_dir() {
.assert_build_dir_layout(str![[r#"
[ROOT]/foo/target-dir/CACHEDIR.TAG
[ROOT]/foo/target-dir/debug/.cargo-lock
[ROOT]/foo/target-dir/debug/.cargo-artifact-lock
[ROOT]/foo/target-dir/debug/foo[EXE]
[ROOT]/foo/target-dir/debug/foo.d

Expand Down Expand Up @@ -858,6 +866,7 @@ fn template_workspace_root() {
.assert_build_dir_layout(str![[r#"
[ROOT]/foo/target-dir/CACHEDIR.TAG
[ROOT]/foo/target-dir/debug/.cargo-lock
[ROOT]/foo/target-dir/debug/.cargo-artifact-lock
[ROOT]/foo/target-dir/debug/foo[EXE]
[ROOT]/foo/target-dir/debug/foo.d

Expand Down Expand Up @@ -906,6 +915,7 @@ fn template_cargo_cache_home() {
.assert_build_dir_layout(str![[r#"
[ROOT]/foo/target-dir/CACHEDIR.TAG
[ROOT]/foo/target-dir/debug/.cargo-lock
[ROOT]/foo/target-dir/debug/.cargo-artifact-lock
[ROOT]/foo/target-dir/debug/foo[EXE]
[ROOT]/foo/target-dir/debug/foo.d

Expand Down Expand Up @@ -968,6 +978,7 @@ fn template_workspace_path_hash() {
.assert_build_dir_layout(str![[r#"
[ROOT]/foo/target-dir/CACHEDIR.TAG
[ROOT]/foo/target-dir/debug/.cargo-lock
[ROOT]/foo/target-dir/debug/.cargo-artifact-lock
[ROOT]/foo/target-dir/debug/foo[EXE]
[ROOT]/foo/target-dir/debug/foo.d

Expand Down Expand Up @@ -1033,6 +1044,7 @@ fn template_workspace_path_hash_should_handle_symlink() {

p.root().join("target").assert_build_dir_layout(str![[r#"
[ROOT]/foo/target/CACHEDIR.TAG
[ROOT]/foo/target/debug/.cargo-lock

"#]]);

Expand Down Expand Up @@ -1072,6 +1084,7 @@ fn template_workspace_path_hash_should_handle_symlink() {

p.root().join("target").assert_build_dir_layout(str![[r#"
[ROOT]/foo/target/CACHEDIR.TAG
[ROOT]/foo/target/debug/.cargo-lock

"#]]);

Expand Down Expand Up @@ -1213,6 +1226,7 @@ CARGO_BIN_FILE_BAR_bar=[ROOT]/foo/build-dir/debug/build/bar/[HASH]/artifact/bin/
.assert_build_dir_layout(str![[r#"
[ROOT]/foo/target-dir/CACHEDIR.TAG
[ROOT]/foo/target-dir/debug/.cargo-lock
[ROOT]/foo/target-dir/debug/.cargo-artifact-lock
[ROOT]/foo/target-dir/debug/foo[EXE]
[ROOT]/foo/target-dir/debug/foo.d

Expand Down
14 changes: 14 additions & 0 deletions tests/testsuite/build_dir_legacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ fn binary_with_debug() {
.assert_build_dir_layout(str![[r#"
[ROOT]/foo/target-dir/CACHEDIR.TAG
[ROOT]/foo/target-dir/debug/.cargo-lock
[ROOT]/foo/target-dir/debug/.cargo-artifact-lock
[ROOT]/foo/target-dir/debug/foo[EXE]
[ROOT]/foo/target-dir/debug/foo.d

Expand Down Expand Up @@ -102,6 +103,7 @@ fn binary_with_release() {
.assert_build_dir_layout(str![[r#"
[ROOT]/foo/target-dir/CACHEDIR.TAG
[ROOT]/foo/target-dir/release/.cargo-lock
[ROOT]/foo/target-dir/release/.cargo-artifact-lock
[ROOT]/foo/target-dir/release/foo[EXE]
[ROOT]/foo/target-dir/release/foo.d

Expand Down Expand Up @@ -191,6 +193,7 @@ fn should_default_to_target() {
[ROOT]/foo/target/.rustc_info.json
[ROOT]/foo/target/CACHEDIR.TAG
[ROOT]/foo/target/debug/.cargo-lock
[ROOT]/foo/target/debug/.cargo-artifact-lock
[ROOT]/foo/target/debug/.cargo-build-lock
[ROOT]/foo/target/debug/.fingerprint/foo-[HASH]/bin-foo
[ROOT]/foo/target/debug/.fingerprint/foo-[HASH]/bin-foo.json
Expand Down Expand Up @@ -345,6 +348,7 @@ fn cargo_tmpdir_should_output_to_build_dir() {
.assert_build_dir_layout(str![[r#"
[ROOT]/foo/target-dir/CACHEDIR.TAG
[ROOT]/foo/target-dir/debug/.cargo-lock
[ROOT]/foo/target-dir/debug/.cargo-artifact-lock
[ROOT]/foo/target-dir/debug/foo[EXE]

"#]]);
Expand Down Expand Up @@ -385,6 +389,7 @@ fn examples_should_output_to_build_dir_and_uplift_to_target_dir() {
.assert_build_dir_layout(str![[r#"
[ROOT]/foo/target-dir/CACHEDIR.TAG
[ROOT]/foo/target-dir/debug/.cargo-lock
[ROOT]/foo/target-dir/debug/.cargo-artifact-lock
[ROOT]/foo/target-dir/debug/examples/foo[EXE]
[ROOT]/foo/target-dir/debug/examples/foo.d

Expand Down Expand Up @@ -432,6 +437,7 @@ fn benches_should_output_to_build_dir() {
.assert_build_dir_layout(str![[r#"
[ROOT]/foo/target-dir/CACHEDIR.TAG
[ROOT]/foo/target-dir/debug/.cargo-lock
[ROOT]/foo/target-dir/debug/.cargo-artifact-lock
[ROOT]/foo/target-dir/debug/foo[EXE]

"#]]);
Expand Down Expand Up @@ -483,6 +489,7 @@ fn cargo_package_should_build_in_build_dir_and_output_to_target_dir() {
p.root().join("build-dir").assert_build_dir_layout(str![[r#"
[ROOT]/foo/build-dir/.rustc_info.json
[ROOT]/foo/build-dir/debug/.cargo-lock
[ROOT]/foo/build-dir/debug/.cargo-artifact-lock
[ROOT]/foo/build-dir/debug/.cargo-build-lock
[ROOT]/foo/build-dir/debug/.fingerprint/foo-[HASH]/bin-foo
[ROOT]/foo/build-dir/debug/.fingerprint/foo-[HASH]/bin-foo.json
Expand Down Expand Up @@ -574,6 +581,7 @@ fn cargo_clean_should_clean_the_target_dir_and_build_dir() {
.assert_build_dir_layout(str![[r#"
[ROOT]/foo/target-dir/CACHEDIR.TAG
[ROOT]/foo/target-dir/debug/.cargo-lock
[ROOT]/foo/target-dir/debug/.cargo-artifact-lock
[ROOT]/foo/target-dir/debug/foo[EXE]
[ROOT]/foo/target-dir/debug/foo.d

Expand Down Expand Up @@ -785,6 +793,7 @@ fn template_workspace_root() {
.assert_build_dir_layout(str![[r#"
[ROOT]/foo/target-dir/CACHEDIR.TAG
[ROOT]/foo/target-dir/debug/.cargo-lock
[ROOT]/foo/target-dir/debug/.cargo-artifact-lock
[ROOT]/foo/target-dir/debug/foo[EXE]
[ROOT]/foo/target-dir/debug/foo.d

Expand Down Expand Up @@ -829,6 +838,7 @@ fn template_cargo_cache_home() {
.assert_build_dir_layout(str![[r#"
[ROOT]/foo/target-dir/CACHEDIR.TAG
[ROOT]/foo/target-dir/debug/.cargo-lock
[ROOT]/foo/target-dir/debug/.cargo-artifact-lock
[ROOT]/foo/target-dir/debug/foo[EXE]
[ROOT]/foo/target-dir/debug/foo.d

Expand Down Expand Up @@ -887,6 +897,7 @@ fn template_workspace_path_hash() {
.assert_build_dir_layout(str![[r#"
[ROOT]/foo/target-dir/CACHEDIR.TAG
[ROOT]/foo/target-dir/debug/.cargo-lock
[ROOT]/foo/target-dir/debug/.cargo-artifact-lock
[ROOT]/foo/target-dir/debug/foo[EXE]
[ROOT]/foo/target-dir/debug/foo.d

Expand Down Expand Up @@ -948,6 +959,7 @@ fn template_workspace_path_hash_should_handle_symlink() {

p.root().join("target").assert_build_dir_layout(str![[r#"
[ROOT]/foo/target/CACHEDIR.TAG
[ROOT]/foo/target/debug/.cargo-lock

"#]]);

Expand Down Expand Up @@ -982,6 +994,7 @@ fn template_workspace_path_hash_should_handle_symlink() {

p.root().join("target").assert_build_dir_layout(str![[r#"
[ROOT]/foo/target/CACHEDIR.TAG
[ROOT]/foo/target/debug/.cargo-lock

"#]]);

Expand Down Expand Up @@ -1122,6 +1135,7 @@ CARGO_BIN_FILE_BAR_bar=[ROOT]/foo/build-dir/debug/deps/artifact/bar-[HASH]/bin/b
.assert_build_dir_layout(str![[r#"
[ROOT]/foo/target-dir/CACHEDIR.TAG
[ROOT]/foo/target-dir/debug/.cargo-lock
[ROOT]/foo/target-dir/debug/.cargo-artifact-lock
[ROOT]/foo/target-dir/debug/foo[EXE]
[ROOT]/foo/target-dir/debug/foo.d

Expand Down
10 changes: 8 additions & 2 deletions tests/testsuite/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1715,6 +1715,7 @@ fn check_build_should_not_output_files_to_artifact_dir() {
.join("target-dir")
.assert_build_dir_layout(str![[r#"
[ROOT]/foo/target-dir/CACHEDIR.TAG
[ROOT]/foo/target-dir/debug/.cargo-lock

"#]]);
}
Expand Down Expand Up @@ -1745,8 +1746,12 @@ fn check_build_should_not_lock_artifact_dir_when_build_dir_is_not_same_dir() {

p.cargo("check").enable_mac_dsym().run();

// Verify we did NOT take the build-dir lock
assert!(!p.root().join("target-dir/debug/.cargo-lock").exists());
// Verify we did NOT take the artifact-dir lock
assert!(
!p.root()
.join("target-dir/debug/.cargo-artifact-lock")
.exists()
);
// Verify we did take the build-dir lock
assert!(p.root().join("build-dir/debug/.cargo-build-lock").exists());
}
Expand Down Expand Up @@ -1838,6 +1843,7 @@ fn check_build_should_not_uplift_proc_macro_dylib_deps() {
.join("target-dir")
.assert_build_dir_layout(str![[r#"
[ROOT]/foo/target-dir/CACHEDIR.TAG
[ROOT]/foo/target-dir/debug/.cargo-lock

"#]]);
}
7 changes: 5 additions & 2 deletions tests/testsuite/clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -736,8 +736,11 @@ fn assert_all_clean(build_dir: &Path) {
}) {
let entry = entry.unwrap();
let path = entry.path();
if let ".rustc_info.json" | ".cargo-lock" | ".cargo-build-lock" | "CACHEDIR.TAG" =
path.file_name().unwrap().to_str().unwrap()
if let ".rustc_info.json"
| ".cargo-lock"
| ".cargo-build-lock"
| ".cargo-artifact-lock"
| "CACHEDIR.TAG" = path.file_name().unwrap().to_str().unwrap()
{
continue;
}
Expand Down
7 changes: 5 additions & 2 deletions tests/testsuite/clean_new_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -702,8 +702,11 @@ fn assert_all_clean(build_dir: &Path) {
}) {
let entry = entry.unwrap();
let path = entry.path();
if let ".rustc_info.json" | ".cargo-lock" | ".cargo-build-lock" | "CACHEDIR.TAG" =
path.file_name().unwrap().to_str().unwrap()
if let ".rustc_info.json"
| ".cargo-lock"
| ".cargo-build-lock"
| ".cargo-artifact-lock"
| "CACHEDIR.TAG" = path.file_name().unwrap().to_str().unwrap()
{
continue;
}
Expand Down