Skip to content

Commit 8c4a25c

Browse files
authored
fix(lock): In error, differentiate between creating and updating lockfile (#16227)
### What does this PR try to resolve? Fixes #10503 ### How to test and review this PR?
2 parents a74cff7 + 2ba6f8c commit 8c4a25c

File tree

3 files changed

+16
-12
lines changed

3 files changed

+16
-12
lines changed

src/cargo/ops/lockfile.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,17 @@ pub fn write_pkg_lockfile(ws: &Workspace<'_>, resolve: &mut Resolve) -> CargoRes
5353
}
5454

5555
if let Some(locked_flag) = ws.gctx().locked_flag() {
56+
let lockfile_path = lock_root.as_path_unlocked().join(LOCKFILE_NAME);
57+
let action = if lockfile_path.exists() {
58+
"updated"
59+
} else {
60+
"created"
61+
};
62+
let lockfile_path = lockfile_path.display();
5663
anyhow::bail!(
57-
"the lock file {} needs to be updated but {} was passed to prevent this\n\
58-
If you want to try to generate the lock file without accessing the network, \
59-
remove the {} flag and use --offline instead.",
60-
lock_root.as_path_unlocked().join(LOCKFILE_NAME).display(),
61-
locked_flag,
62-
locked_flag
64+
"the lock file {lockfile_path} needs to be {action} but {locked_flag} was passed to prevent this\n\
65+
help: to generate the lock file without accessing the network, \
66+
remove the {locked_flag} flag and use --offline instead."
6367
);
6468
}
6569

tests/testsuite/lockfile_compat.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -517,8 +517,8 @@ fn locked_correct_error() {
517517
.with_status(101)
518518
.with_stderr_data(str![[r#"
519519
[UPDATING] `dummy-registry` index
520-
[ERROR] the lock file [ROOT]/foo/Cargo.lock needs to be updated but --locked was passed to prevent this
521-
If you want to try to generate the lock file without accessing the network, remove the --locked flag and use --offline instead.
520+
[ERROR] the lock file [ROOT]/foo/Cargo.lock needs to be created but --locked was passed to prevent this
521+
[HELP] to generate the lock file without accessing the network, remove the --locked flag and use --offline instead.
522522
523523
"#]])
524524
.run();

tests/testsuite/offline.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -767,8 +767,8 @@ fn offline_and_frozen_and_no_lock() {
767767
p.cargo("check --frozen --offline")
768768
.with_status(101)
769769
.with_stderr_data(str![[r#"
770-
[ERROR] the lock file [ROOT]/foo/Cargo.lock needs to be updated but --frozen was passed to prevent this
771-
If you want to try to generate the lock file without accessing the network, remove the --frozen flag and use --offline instead.
770+
[ERROR] the lock file [ROOT]/foo/Cargo.lock needs to be created but --frozen was passed to prevent this
771+
[HELP] to generate the lock file without accessing the network, remove the --frozen flag and use --offline instead.
772772
773773
"#]])
774774
.run();
@@ -780,8 +780,8 @@ fn offline_and_locked_and_no_frozen() {
780780
p.cargo("check --locked --offline")
781781
.with_status(101)
782782
.with_stderr_data(str![[r#"
783-
[ERROR] the lock file [ROOT]/foo/Cargo.lock needs to be updated but --locked was passed to prevent this
784-
If you want to try to generate the lock file without accessing the network, remove the --locked flag and use --offline instead.
783+
[ERROR] the lock file [ROOT]/foo/Cargo.lock needs to be created but --locked was passed to prevent this
784+
[HELP] to generate the lock file without accessing the network, remove the --locked flag and use --offline instead.
785785
786786
"#]])
787787
.run();

0 commit comments

Comments
 (0)