Skip to content

Commit 6f322e8

Browse files
committed
feat: Do not lock the artifact-dir for check builds
1 parent a1d20db commit 6f322e8

File tree

6 files changed

+32
-16
lines changed

6 files changed

+32
-16
lines changed

src/cargo/core/compiler/build_runner/mod.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::sync::{Arc, Mutex};
66

77
use crate::core::PackageId;
88
use crate::core::compiler::compilation::{self, UnitOutput};
9-
use crate::core::compiler::{self, Unit, artifact};
9+
use crate::core::compiler::{self, Unit, UserIntent, artifact};
1010
use crate::util::cache_lock::CacheLockMode;
1111
use crate::util::errors::CargoResult;
1212
use annotate_snippets::{Level, Message};
@@ -352,11 +352,27 @@ impl<'a, 'gctx> BuildRunner<'a, 'gctx> {
352352
#[tracing::instrument(skip_all)]
353353
pub fn prepare_units(&mut self) -> CargoResult<()> {
354354
let dest = self.bcx.profiles.get_dir_name();
355-
let host_layout = Layout::new(self.bcx.ws, None, &dest)?;
355+
// We try to only lock the artifact-dir if we need to.
356+
// For example, `cargo check` does not write any files to the artifact-dir so we don't need
357+
// to lock it.
358+
let must_take_artifact_dir_lock = match self.bcx.build_config.intent {
359+
UserIntent::Check { .. } => false,
360+
UserIntent::Build
361+
| UserIntent::Test
362+
| UserIntent::Doc { .. }
363+
| UserIntent::Doctest
364+
| UserIntent::Bench => true,
365+
};
366+
let host_layout = Layout::new(self.bcx.ws, None, &dest, must_take_artifact_dir_lock)?;
356367
let mut targets = HashMap::new();
357368
for kind in self.bcx.all_kinds.iter() {
358369
if let CompileKind::Target(target) = *kind {
359-
let layout = Layout::new(self.bcx.ws, Some(target), &dest)?;
370+
let layout = Layout::new(
371+
self.bcx.ws,
372+
Some(target),
373+
&dest,
374+
must_take_artifact_dir_lock,
375+
)?;
360376
targets.insert(target, layout);
361377
}
362378
}

src/cargo/core/compiler/layout.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ impl Layout {
127127
ws: &Workspace<'_>,
128128
target: Option<CompileTarget>,
129129
dest: &str,
130+
must_take_artifact_dir_lock: bool,
130131
) -> CargoResult<Layout> {
131132
let is_new_layout = ws.gctx().cli_unstable().build_dir_new_layout;
132133
let mut root = ws.target_dir();
@@ -153,7 +154,9 @@ impl Layout {
153154
// For now we don't do any more finer-grained locking on the artifact
154155
// directory, so just lock the entire thing for the duration of this
155156
// compile.
156-
let artifact_dir_lock = if is_on_nfs_mount(root.as_path_unlocked()) {
157+
let artifact_dir_lock = if !must_take_artifact_dir_lock
158+
|| is_on_nfs_mount(root.as_path_unlocked())
159+
{
157160
None
158161
} else {
159162
Some(dest.open_rw_exclusive_create(".cargo-lock", ws.gctx(), "artifact directory")?)

src/cargo/ops/cargo_clean.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,15 +116,17 @@ fn clean_specs(
116116
let target_data = RustcTargetData::new(ws, &requested_kinds)?;
117117
let (pkg_set, resolve) = ops::resolve_ws(ws, dry_run)?;
118118
let prof_dir_name = profiles.get_dir_name();
119-
let host_layout = Layout::new(ws, None, &prof_dir_name)?;
119+
let host_layout = Layout::new(ws, None, &prof_dir_name, true)?;
120120
// Convert requested kinds to a Vec of layouts.
121121
let target_layouts: Vec<(CompileKind, Layout)> = requested_kinds
122122
.into_iter()
123123
.filter_map(|kind| match kind {
124-
CompileKind::Target(target) => match Layout::new(ws, Some(target), &prof_dir_name) {
125-
Ok(layout) => Some(Ok((kind, layout))),
126-
Err(e) => Some(Err(e)),
127-
},
124+
CompileKind::Target(target) => {
125+
match Layout::new(ws, Some(target), &prof_dir_name, true) {
126+
Ok(layout) => Some(Ok((kind, layout))),
127+
Err(e) => Some(Err(e)),
128+
}
129+
}
128130
CompileKind::Host => None,
129131
})
130132
.collect::<CargoResult<_>>()?;

tests/testsuite/build_dir.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,7 +1005,6 @@ fn template_workspace_path_hash_should_handle_symlink() {
10051005

10061006
p.root().join("target").assert_build_dir_layout(str![[r#"
10071007
[ROOT]/foo/target/CACHEDIR.TAG
1008-
[ROOT]/foo/target/debug/.cargo-lock
10091008
10101009
"#]]);
10111010

@@ -1044,7 +1043,6 @@ fn template_workspace_path_hash_should_handle_symlink() {
10441043

10451044
p.root().join("target").assert_build_dir_layout(str![[r#"
10461045
[ROOT]/foo/target/CACHEDIR.TAG
1047-
[ROOT]/foo/target/debug/.cargo-lock
10481046
10491047
"#]]);
10501048

tests/testsuite/build_dir_legacy.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -943,7 +943,6 @@ fn template_workspace_path_hash_should_handle_symlink() {
943943

944944
p.root().join("target").assert_build_dir_layout(str![[r#"
945945
[ROOT]/foo/target/CACHEDIR.TAG
946-
[ROOT]/foo/target/debug/.cargo-lock
947946
948947
"#]]);
949948

@@ -978,7 +977,6 @@ fn template_workspace_path_hash_should_handle_symlink() {
978977

979978
p.root().join("target").assert_build_dir_layout(str![[r#"
980979
[ROOT]/foo/target/CACHEDIR.TAG
981-
[ROOT]/foo/target/debug/.cargo-lock
982980
983981
"#]]);
984982

tests/testsuite/check.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1705,17 +1705,16 @@ fn check_build_should_not_output_files_to_artifact_dir() {
17051705
.join("target-dir")
17061706
.assert_build_dir_layout(str![[r#"
17071707
[ROOT]/foo/target-dir/CACHEDIR.TAG
1708-
[ROOT]/foo/target-dir/debug/.cargo-lock
17091708
17101709
"#]]);
17111710
}
17121711

17131712
#[cargo_test]
1714-
fn check_build_should_lock_artifact_dir() {
1713+
fn check_build_should_not_lock_artifact_dir() {
17151714
let p = project()
17161715
.file("src/main.rs", r#"fn main() { println!("Hello, World!") }"#)
17171716
.build();
17181717

17191718
p.cargo("check").enable_mac_dsym().run();
1720-
assert!(p.root().join("target/debug/.cargo-lock").exists());
1719+
assert!(!p.root().join("target/debug/.cargo-lock").exists());
17211720
}

0 commit comments

Comments
 (0)