feat: lockfile path move create logic into write_pkg_lockfile

This commit is contained in:
Ifropc 2024-08-13 23:07:54 -07:00
parent c1ace2ad81
commit d1b1b65a54
No known key found for this signature in database
GPG Key ID: D2704D4776B957A8
3 changed files with 25 additions and 15 deletions

View File

@ -84,6 +84,10 @@ pub fn write_pkg_lockfile(ws: &Workspace<'_>, resolve: &mut Resolve) -> CargoRes
anyhow::bail!("lock file version `{current_version:?}` requires `-Znext-lockfile-bump`")
}
if !lock_root.as_path_unlocked().exists() {
lock_root.create_dir()?;
}
// Ok, if that didn't work just write it out
lock_root
.open_rw_exclusive_create(LOCKFILE_NAME, ws.gctx(), "Cargo.lock file")

View File

@ -1015,7 +1015,7 @@ pub fn lockfile_path(
let path = gctx.cwd().join(lockfile_path);
if !path.ends_with(LOCKFILE_NAME) {
bail!("the lockfile-path must be a path to a {LOCKFILE_NAME} file")
bail!("the lockfile-path must be a path to a {LOCKFILE_NAME} file (please rename your lock file to {LOCKFILE_NAME})")
}
if path.is_dir() {
bail!(
@ -1023,12 +1023,6 @@ pub fn lockfile_path(
lockfile_path.display()
)
}
if !path.exists() {
// Root case should already be covered above
let parent_path = lockfile_path.parent().expect("lockfile path can't be root");
paths::create_dir_all(parent_path)?;
}
return Ok(Some(path));
}

View File

@ -135,19 +135,25 @@ fn broken_symlink() {
assert!(!p.root().join(src).is_dir());
let err_msg = if !cfg!(windows) {
str![[r#"[ERROR] failed to create directory `somedir/link`
str![[
r#"[WARNING] please specify `--format-version` flag explicitly to avoid compatibility problems
[ERROR] failed to create directory `[ROOT]/foo/somedir/link`
Caused by:
File exists (os error 17)
"#]]
"#
]]
} else {
str![[r#"[ERROR] failed to create directory `somedir/link`
str![[
r#"[WARNING] please specify `--format-version` flag explicitly to avoid compatibility problems
[ERROR] failed to create directory `[ROOT]/foo/somedir/link`
Caused by:
Cannot create a file when that file already exists. (os error 183)
"#]]
"#
]]
};
make_execs(&mut p.cargo("metadata"), lockfile_path_argument.to_string())
@ -173,19 +179,25 @@ fn loop_symlink() {
assert!(!p.root().join(src).is_dir());
let err_msg = if !cfg!(windows) {
str![[r#"[ERROR] failed to create directory `somedir/link`
str![[
r#"[WARNING] please specify `--format-version` flag explicitly to avoid compatibility problems
[ERROR] failed to create directory `[ROOT]/foo/somedir/link`
Caused by:
File exists (os error 17)
"#]]
"#
]]
} else {
str![[r#"[ERROR] failed to create directory `somedir/link`
str![[
r#"[WARNING] please specify `--format-version` flag explicitly to avoid compatibility problems
[ERROR] failed to create directory `[ROOT]/foo/somedir/link`
Caused by:
Cannot create a file when that file already exists. (os error 183)
"#]]
"#
]]
};
make_execs(&mut p.cargo("metadata"), lockfile_path_argument.to_string())