Skip to content

Commit d1b1b65

Browse files
committed
feat: lockfile path move create logic into write_pkg_lockfile
1 parent c1ace2a commit d1b1b65

File tree

3 files changed

+25
-15
lines changed

3 files changed

+25
-15
lines changed

src/cargo/ops/lockfile.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ pub fn write_pkg_lockfile(ws: &Workspace<'_>, resolve: &mut Resolve) -> CargoRes
8484
anyhow::bail!("lock file version `{current_version:?}` requires `-Znext-lockfile-bump`")
8585
}
8686

87+
if !lock_root.as_path_unlocked().exists() {
88+
lock_root.create_dir()?;
89+
}
90+
8791
// Ok, if that didn't work just write it out
8892
lock_root
8993
.open_rw_exclusive_create(LOCKFILE_NAME, ws.gctx(), "Cargo.lock file")

src/cargo/util/command_prelude.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,20 +1015,14 @@ pub fn lockfile_path(
10151015
let path = gctx.cwd().join(lockfile_path);
10161016

10171017
if !path.ends_with(LOCKFILE_NAME) {
1018-
bail!("the lockfile-path must be a path to a {LOCKFILE_NAME} file")
1018+
bail!("the lockfile-path must be a path to a {LOCKFILE_NAME} file (please rename your lock file to {LOCKFILE_NAME})")
10191019
}
10201020
if path.is_dir() {
10211021
bail!(
10221022
"lockfile path `{}` is a directory but expected a file",
10231023
lockfile_path.display()
10241024
)
10251025
}
1026-
if !path.exists() {
1027-
// Root case should already be covered above
1028-
let parent_path = lockfile_path.parent().expect("lockfile path can't be root");
1029-
1030-
paths::create_dir_all(parent_path)?;
1031-
}
10321026

10331027
return Ok(Some(path));
10341028
}

tests/testsuite/lockfile_path.rs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -135,19 +135,25 @@ fn broken_symlink() {
135135
assert!(!p.root().join(src).is_dir());
136136

137137
let err_msg = if !cfg!(windows) {
138-
str![[r#"[ERROR] failed to create directory `somedir/link`
138+
str![[
139+
r#"[WARNING] please specify `--format-version` flag explicitly to avoid compatibility problems
140+
[ERROR] failed to create directory `[ROOT]/foo/somedir/link`
139141
140142
Caused by:
141143
File exists (os error 17)
142144
143-
"#]]
145+
"#
146+
]]
144147
} else {
145-
str![[r#"[ERROR] failed to create directory `somedir/link`
148+
str![[
149+
r#"[WARNING] please specify `--format-version` flag explicitly to avoid compatibility problems
150+
[ERROR] failed to create directory `[ROOT]/foo/somedir/link`
146151
147152
Caused by:
148153
Cannot create a file when that file already exists. (os error 183)
149154
150-
"#]]
155+
"#
156+
]]
151157
};
152158

153159
make_execs(&mut p.cargo("metadata"), lockfile_path_argument.to_string())
@@ -173,19 +179,25 @@ fn loop_symlink() {
173179
assert!(!p.root().join(src).is_dir());
174180

175181
let err_msg = if !cfg!(windows) {
176-
str![[r#"[ERROR] failed to create directory `somedir/link`
182+
str![[
183+
r#"[WARNING] please specify `--format-version` flag explicitly to avoid compatibility problems
184+
[ERROR] failed to create directory `[ROOT]/foo/somedir/link`
177185
178186
Caused by:
179187
File exists (os error 17)
180188
181-
"#]]
189+
"#
190+
]]
182191
} else {
183-
str![[r#"[ERROR] failed to create directory `somedir/link`
192+
str![[
193+
r#"[WARNING] please specify `--format-version` flag explicitly to avoid compatibility problems
194+
[ERROR] failed to create directory `[ROOT]/foo/somedir/link`
184195
185196
Caused by:
186197
Cannot create a file when that file already exists. (os error 183)
187198
188-
"#]]
199+
"#
200+
]]
189201
};
190202

191203
make_execs(&mut p.cargo("metadata"), lockfile_path_argument.to_string())

0 commit comments

Comments
 (0)