Skip to content

Commit a261f5a

Browse files
committed
Revert "Revert "feat: Do not lock the artifact-dir for check builds""
1 parent 3a4485d commit a261f5a

File tree

6 files changed

+56
-29
lines changed

6 files changed

+56
-29
lines changed

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

Lines changed: 24 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,32 @@ 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 is
361+
// one exception: If check has `--timings` we still need to lock artifact-dir since
362+
// we will output the report files.
363+
!self.bcx.build_config.timing_outputs.is_empty()
364+
}
365+
UserIntent::Build
366+
| UserIntent::Test
367+
| UserIntent::Doc { .. }
368+
| UserIntent::Doctest
369+
| UserIntent::Bench => true,
370+
};
371+
let host_layout = Layout::new(self.bcx.ws, None, &dest, must_take_artifact_dir_lock)?;
356372
let mut targets = HashMap::new();
357373
for kind in self.bcx.all_kinds.iter() {
358374
if let CompileKind::Target(target) = *kind {
359-
let layout = Layout::new(self.bcx.ws, Some(target), &dest)?;
375+
let layout = Layout::new(
376+
self.bcx.ws,
377+
Some(target),
378+
&dest,
379+
must_take_artifact_dir_lock,
380+
)?;
360381
targets.insert(target, layout);
361382
}
362383
}

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
@@ -117,15 +117,17 @@ fn clean_specs(
117117
let target_data = RustcTargetData::new(ws, &requested_kinds)?;
118118
let (pkg_set, resolve) = ops::resolve_ws(ws, dry_run)?;
119119
let prof_dir_name = profiles.get_dir_name();
120-
let host_layout = Layout::new(ws, None, &prof_dir_name)?;
120+
let host_layout = Layout::new(ws, None, &prof_dir_name, true)?;
121121
// Convert requested kinds to a Vec of layouts.
122122
let target_layouts: Vec<(CompileKind, Layout)> = requested_kinds
123123
.into_iter()
124124
.filter_map(|kind| match kind {
125-
CompileKind::Target(target) => match Layout::new(ws, Some(target), &prof_dir_name) {
126-
Ok(layout) => Some(Ok((kind, layout))),
127-
Err(e) => Some(Err(e)),
128-
},
125+
CompileKind::Target(target) => {
126+
match Layout::new(ws, Some(target), &prof_dir_name, true) {
127+
Ok(layout) => Some(Ok((kind, layout))),
128+
Err(e) => Some(Err(e)),
129+
}
130+
}
129131
CompileKind::Host => None,
130132
})
131133
.collect::<CargoResult<_>>()?;

tests/testsuite/build_dir.rs

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

10081008
p.root().join("target").assert_build_dir_layout(str![[r#"
10091009
[ROOT]/foo/target/CACHEDIR.TAG
1010-
[ROOT]/foo/target/debug/.cargo-lock
10111010
10121011
"#]]);
10131012

@@ -1046,7 +1045,6 @@ fn template_workspace_path_hash_should_handle_symlink() {
10461045

10471046
p.root().join("target").assert_build_dir_layout(str![[r#"
10481047
[ROOT]/foo/target/CACHEDIR.TAG
1049-
[ROOT]/foo/target/debug/.cargo-lock
10501048
10511049
"#]]);
10521050

tests/testsuite/build_dir_legacy.rs

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

946946
p.root().join("target").assert_build_dir_layout(str![[r#"
947947
[ROOT]/foo/target/CACHEDIR.TAG
948-
[ROOT]/foo/target/debug/.cargo-lock
949948
950949
"#]]);
951950

@@ -980,7 +979,6 @@ fn template_workspace_path_hash_should_handle_symlink() {
980979

981980
p.root().join("target").assert_build_dir_layout(str![[r#"
982981
[ROOT]/foo/target/CACHEDIR.TAG
983-
[ROOT]/foo/target/debug/.cargo-lock
984982
985983
"#]]);
986984

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)