Skip to content

Commit 298152d

Browse files
committed
feat: Do not lock the artifact-dir for check builds
1 parent fb6dcba commit 298152d

File tree

6 files changed

+61
-29
lines changed

6 files changed

+61
-29
lines changed

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

Lines changed: 29 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,37 @@ 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 { .. } => {
360+
// Generally cargo check does not need to take the artifact-dir lock but there are
361+
// two notable exceptions:
362+
// 1. If check has `--test` or `--tests` we need to pass `CARGO_BIN_EXE_` which
363+
// points into the artifact-dir. In theory we don't need to take the lock here
364+
// but we do to satify the Layout struct.
365+
// 2. If check has `--timings` we still need to lock artifact-dir since we will
366+
// output the report files.
367+
let is_test = self.bcx.roots.iter().any(|u| u.mode.is_any_test());
368+
is_test || !self.bcx.build_config.timing_outputs.is_empty()
369+
}
370+
UserIntent::Build
371+
| UserIntent::Test
372+
| UserIntent::Doc { .. }
373+
| UserIntent::Doctest
374+
| UserIntent::Bench => true,
375+
};
376+
let host_layout = Layout::new(self.bcx.ws, None, &dest, must_take_artifact_dir_lock)?;
356377
let mut targets = HashMap::new();
357378
for kind in self.bcx.all_kinds.iter() {
358379
if let CompileKind::Target(target) = *kind {
359-
let layout = Layout::new(self.bcx.ws, Some(target), &dest)?;
380+
let layout = Layout::new(
381+
self.bcx.ws,
382+
Some(target),
383+
&dest,
384+
must_take_artifact_dir_lock,
385+
)?;
360386
targets.insert(target, layout);
361387
}
362388
}

src/cargo/core/compiler/layout.rs

Lines changed: 23 additions & 14 deletions
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();
@@ -150,15 +151,6 @@ impl Layout {
150151
// actual destination (sub)subdirectory.
151152
paths::create_dir_all(dest.as_path_unlocked())?;
152153

153-
// For now we don't do any more finer-grained locking on the artifact
154-
// directory, so just lock the entire thing for the duration of this
155-
// compile.
156-
let artifact_dir_lock = if is_on_nfs_mount(root.as_path_unlocked()) {
157-
None
158-
} else {
159-
Some(dest.open_rw_exclusive_create(".cargo-lock", ws.gctx(), "artifact directory")?)
160-
};
161-
162154
let build_dir_lock = if root == build_root || is_on_nfs_mount(build_root.as_path_unlocked())
163155
{
164156
None
@@ -169,21 +161,38 @@ impl Layout {
169161
"build directory",
170162
)?)
171163
};
172-
let root = root.into_path_unlocked();
173164
let build_root = build_root.into_path_unlocked();
174-
let dest = dest.into_path_unlocked();
175165
let build_dest = build_dest.as_path_unlocked();
176166
let deps = build_dest.join("deps");
177167
let artifact = deps.join("artifact");
178168

179-
Ok(Layout {
180-
artifact_dir: Some(ArtifactDirLayout {
169+
let artifact_dir = if must_take_artifact_dir_lock {
170+
// For now we don't do any more finer-grained locking on the artifact
171+
// directory, so just lock the entire thing for the duration of this
172+
// compile.
173+
let artifact_dir_lock = if is_on_nfs_mount(root.as_path_unlocked()) {
174+
None
175+
} else {
176+
Some(dest.open_rw_exclusive_create(
177+
".cargo-lock",
178+
ws.gctx(),
179+
"artifact directory",
180+
)?)
181+
};
182+
let root = root.into_path_unlocked();
183+
let dest = dest.into_path_unlocked();
184+
Some(ArtifactDirLayout {
181185
dest: dest.clone(),
182186
examples: dest.join("examples"),
183187
doc: root.join("doc"),
184188
timings: root.join("cargo-timings"),
185189
_lock: artifact_dir_lock,
186-
}),
190+
})
191+
} else {
192+
None
193+
};
194+
Ok(Layout {
195+
artifact_dir,
187196
build_dir: BuildDirLayout {
188197
root: build_root.clone(),
189198
deps,

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)