Skip to content

Commit dce5a6b

Browse files
authored
Revert "feat: Do not lock the artifact-dir for check builds" (#16299)
### What does this PR try to resolve? Fixes #16297 ### How to test and review this PR? This reverts commit 9f5393a.
2 parents 2b48142 + eb54ffc commit dce5a6b

File tree

6 files changed

+29
-56
lines changed

6 files changed

+29
-56
lines changed

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

Lines changed: 3 additions & 24 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, UserIntent, artifact};
9+
use crate::core::compiler::{self, Unit, artifact};
1010
use crate::util::cache_lock::CacheLockMode;
1111
use crate::util::errors::CargoResult;
1212
use annotate_snippets::{Level, Message};
@@ -352,32 +352,11 @@ 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-
// 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)?;
355+
let host_layout = Layout::new(self.bcx.ws, None, &dest)?;
372356
let mut targets = HashMap::new();
373357
for kind in self.bcx.all_kinds.iter() {
374358
if let CompileKind::Target(target) = *kind {
375-
let layout = Layout::new(
376-
self.bcx.ws,
377-
Some(target),
378-
&dest,
379-
must_take_artifact_dir_lock,
380-
)?;
359+
let layout = Layout::new(self.bcx.ws, Some(target), &dest)?;
381360
targets.insert(target, layout);
382361
}
383362
}

src/cargo/core/compiler/layout.rs

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@ impl Layout {
127127
ws: &Workspace<'_>,
128128
target: Option<CompileTarget>,
129129
dest: &str,
130-
must_take_artifact_dir_lock: bool,
131130
) -> CargoResult<Layout> {
132131
let is_new_layout = ws.gctx().cli_unstable().build_dir_new_layout;
133132
let mut root = ws.target_dir();
@@ -151,6 +150,15 @@ impl Layout {
151150
// actual destination (sub)subdirectory.
152151
paths::create_dir_all(dest.as_path_unlocked())?;
153152

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+
154162
let build_dir_lock = if root == build_root || is_on_nfs_mount(build_root.as_path_unlocked())
155163
{
156164
None
@@ -161,38 +169,21 @@ impl Layout {
161169
"build directory",
162170
)?)
163171
};
172+
let root = root.into_path_unlocked();
164173
let build_root = build_root.into_path_unlocked();
174+
let dest = dest.into_path_unlocked();
165175
let build_dest = build_dest.as_path_unlocked();
166176
let deps = build_dest.join("deps");
167177
let artifact = deps.join("artifact");
168178

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 {
179+
Ok(Layout {
180+
artifact_dir: Some(ArtifactDirLayout {
185181
dest: dest.clone(),
186182
examples: dest.join("examples"),
187183
doc: root.join("doc"),
188184
timings: root.join("cargo-timings"),
189185
_lock: artifact_dir_lock,
190-
})
191-
} else {
192-
None
193-
};
194-
Ok(Layout {
195-
artifact_dir,
186+
}),
196187
build_dir: BuildDirLayout {
197188
root: build_root.clone(),
198189
deps,

src/cargo/ops/cargo_clean.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,17 +117,15 @@ 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, true)?;
120+
let host_layout = Layout::new(ws, None, &prof_dir_name)?;
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) => {
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-
}
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+
},
131129
CompileKind::Host => None,
132130
})
133131
.collect::<CargoResult<_>>()?;

tests/testsuite/build_dir.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,6 +1007,7 @@ 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
10101011
10111012
"#]]);
10121013

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

10461047
p.root().join("target").assert_build_dir_layout(str![[r#"
10471048
[ROOT]/foo/target/CACHEDIR.TAG
1049+
[ROOT]/foo/target/debug/.cargo-lock
10481050
10491051
"#]]);
10501052

tests/testsuite/build_dir_legacy.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -945,6 +945,7 @@ 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
948949
949950
"#]]);
950951

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

980981
p.root().join("target").assert_build_dir_layout(str![[r#"
981982
[ROOT]/foo/target/CACHEDIR.TAG
983+
[ROOT]/foo/target/debug/.cargo-lock
982984
983985
"#]]);
984986

tests/testsuite/check.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1705,16 +1705,17 @@ 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
17081709
17091710
"#]]);
17101711
}
17111712

17121713
#[cargo_test]
1713-
fn check_build_should_not_lock_artifact_dir() {
1714+
fn check_build_should_lock_artifact_dir() {
17141715
let p = project()
17151716
.file("src/main.rs", r#"fn main() { println!("Hello, World!") }"#)
17161717
.build();
17171718

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

0 commit comments

Comments
 (0)