Skip to content

Commit 3da5667

Browse files
committed
refactoring: lockfile-path documentation and implementation cleanup
1 parent 066b291 commit 3da5667

File tree

87 files changed

+310
-334
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+310
-334
lines changed

src/cargo/util/command_prelude.rs

Lines changed: 25 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,13 @@ use crate::util::{
1313
print_available_packages, print_available_tests,
1414
};
1515
use crate::CargoResult;
16-
use anyhow::{bail, Context};
16+
use anyhow::bail;
1717
use cargo_util::paths;
1818
use cargo_util_schemas::manifest::ProfileName;
1919
use cargo_util_schemas::manifest::RegistryName;
2020
use cargo_util_schemas::manifest::StringOrVec;
2121
use clap::builder::UnknownArgumentValueParser;
2222
use std::ffi::{OsStr, OsString};
23-
use std::fs;
2423
use std::path::Path;
2524
use std::path::PathBuf;
2625

@@ -299,9 +298,8 @@ pub trait CommandExt: Sized {
299298

300299
fn arg_lockfile_path(self) -> Self {
301300
self._arg(
302-
opt("lockfile-path", "Path to the Cargo.lock file (unstable)")
301+
opt("lockfile-path", "Path to Cargo.lock (unstable)")
303302
.value_name("FILE")
304-
// TODO: seemed to be the best option, but maybe should be something else?
305303
.help_heading(heading::MANIFEST_OPTIONS),
306304
)
307305
}
@@ -1007,58 +1005,35 @@ pub fn lockfile_path(
10071005
lockfile_path: Option<&Path>,
10081006
gctx: &GlobalContext,
10091007
) -> CargoResult<Option<PathBuf>> {
1010-
let path;
1008+
let Some(lockfile_path) = lockfile_path else {
1009+
return Ok(None);
1010+
};
10111011

1012-
if let Some(lockfile_path) = lockfile_path {
1013-
if !gctx.cli_unstable().unstable_options {
1014-
bail!("`--lockfile-path` option requires `-Zunstable-options`")
1015-
}
1012+
gctx.cli_unstable()
1013+
.fail_if_stable_opt("--lockfile-path", 5707)?;
10161014

1017-
if lockfile_path.is_absolute() {
1018-
path = lockfile_path.to_path_buf();
1019-
} else {
1020-
path = gctx.cwd().join(lockfile_path);
1021-
}
1015+
let path = gctx.cwd().join(lockfile_path);
10221016

1023-
if !path.ends_with(LOCKFILE_NAME) && !crate::util::toml::is_embedded(&path) {
1024-
bail!(
1025-
"the lockfile-path must be a path to a {} file",
1026-
LOCKFILE_NAME
1027-
)
1028-
}
1029-
if path.is_dir() {
1030-
bail!(
1031-
"lockfile path `{}` is a directory but expected a file",
1032-
lockfile_path.display()
1033-
)
1034-
}
1035-
if !path.exists() {
1036-
// Root case should already be covered above
1037-
let parent_path = lockfile_path
1038-
.parent()
1039-
.unwrap_or_else(|| unreachable!("Lockfile path can't be root"));
1040-
1041-
let exists = parent_path.try_exists().with_context(|| {
1042-
format!(
1043-
"Failed to fetch lock file's parent path metadata {}",
1044-
parent_path.display()
1045-
)
1046-
})?;
1047-
1048-
if !exists {
1049-
fs::create_dir_all(parent_path).with_context(|| {
1050-
format!(
1051-
"Failed to create lockfile-path parent directory {}",
1052-
parent_path.display()
1053-
)
1054-
})?
1055-
}
1056-
}
1017+
if !path.ends_with(LOCKFILE_NAME) && !crate::util::toml::is_embedded(&path) {
1018+
bail!(
1019+
"the lockfile-path must be a path to a {} file",
1020+
LOCKFILE_NAME
1021+
)
1022+
}
1023+
if path.is_dir() {
1024+
bail!(
1025+
"lockfile path `{}` is a directory but expected a file",
1026+
lockfile_path.display()
1027+
)
1028+
}
1029+
if !path.exists() {
1030+
// Root case should already be covered above
1031+
let parent_path = lockfile_path.parent().expect("lockfile path can't be root");
10571032

1058-
return Ok(Some(path));
1033+
paths::create_dir_all(parent_path)?;
10591034
}
10601035

1061-
Ok(None)
1036+
return Ok(Some(path));
10621037
}
10631038

10641039
#[track_caller]

src/doc/man/generated_txt/cargo-add.txt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -198,14 +198,14 @@ OPTIONS
198198
Equivalent to specifying both --locked and --offline.
199199

200200
--lockfile-path PATH
201-
Changes the path of the lockfile from the default (./Cargo.lock) to
202-
PATH. PATH must end with Cargo.lock (e.g. --lockfile-path
203-
/tmp/temporary-lockfile/Cargo.lock). Note that providing
204-
--lockfile-path will ignore existing default lockfile
205-
(./Cargo.lock), if exists, and instead will either use PATH lockfile
206-
(or write a new lockfile into the provided path if it doesn’t
207-
exist). This flag can be used to run most commands in read-only
208-
directories, writing lockfile into the provided PATH.
201+
Changes the path of the lockfile from the default
202+
(<workspace_root>/Cargo.lock) to PATH. PATH must end with Cargo.lock
203+
(e.g. --lockfile-path /tmp/temporary-lockfile/Cargo.lock). Note that
204+
providing --lockfile-path will ignore existing default lockfile
205+
(<workspace_root>/Cargo.lock), if exists, and instead will either
206+
use PATH lockfile (or write a new lockfile into the provided path if
207+
it doesn’t exist). This flag can be used to run most commands in
208+
read-only directories, writing lockfile into the provided PATH.
209209

210210
This option is only available on the nightly channel
211211
<https://doc.rust-lang.org/book/appendix-07-nightly-rust.html> and

src/doc/man/generated_txt/cargo-bench.txt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -369,14 +369,14 @@ OPTIONS
369369
Equivalent to specifying both --locked and --offline.
370370

371371
--lockfile-path PATH
372-
Changes the path of the lockfile from the default (./Cargo.lock) to
373-
PATH. PATH must end with Cargo.lock (e.g. --lockfile-path
374-
/tmp/temporary-lockfile/Cargo.lock). Note that providing
375-
--lockfile-path will ignore existing default lockfile
376-
(./Cargo.lock), if exists, and instead will either use PATH lockfile
377-
(or write a new lockfile into the provided path if it doesn’t
378-
exist). This flag can be used to run most commands in read-only
379-
directories, writing lockfile into the provided PATH.
372+
Changes the path of the lockfile from the default
373+
(<workspace_root>/Cargo.lock) to PATH. PATH must end with Cargo.lock
374+
(e.g. --lockfile-path /tmp/temporary-lockfile/Cargo.lock). Note that
375+
providing --lockfile-path will ignore existing default lockfile
376+
(<workspace_root>/Cargo.lock), if exists, and instead will either
377+
use PATH lockfile (or write a new lockfile into the provided path if
378+
it doesn’t exist). This flag can be used to run most commands in
379+
read-only directories, writing lockfile into the provided PATH.
380380

381381
This option is only available on the nightly channel
382382
<https://doc.rust-lang.org/book/appendix-07-nightly-rust.html> and

src/doc/man/generated_txt/cargo-build.txt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -303,14 +303,14 @@ OPTIONS
303303
Equivalent to specifying both --locked and --offline.
304304

305305
--lockfile-path PATH
306-
Changes the path of the lockfile from the default (./Cargo.lock) to
307-
PATH. PATH must end with Cargo.lock (e.g. --lockfile-path
308-
/tmp/temporary-lockfile/Cargo.lock). Note that providing
309-
--lockfile-path will ignore existing default lockfile
310-
(./Cargo.lock), if exists, and instead will either use PATH lockfile
311-
(or write a new lockfile into the provided path if it doesn’t
312-
exist). This flag can be used to run most commands in read-only
313-
directories, writing lockfile into the provided PATH.
306+
Changes the path of the lockfile from the default
307+
(<workspace_root>/Cargo.lock) to PATH. PATH must end with Cargo.lock
308+
(e.g. --lockfile-path /tmp/temporary-lockfile/Cargo.lock). Note that
309+
providing --lockfile-path will ignore existing default lockfile
310+
(<workspace_root>/Cargo.lock), if exists, and instead will either
311+
use PATH lockfile (or write a new lockfile into the provided path if
312+
it doesn’t exist). This flag can be used to run most commands in
313+
read-only directories, writing lockfile into the provided PATH.
314314

315315
This option is only available on the nightly channel
316316
<https://doc.rust-lang.org/book/appendix-07-nightly-rust.html> and

src/doc/man/generated_txt/cargo-check.txt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -288,14 +288,14 @@ OPTIONS
288288
Equivalent to specifying both --locked and --offline.
289289

290290
--lockfile-path PATH
291-
Changes the path of the lockfile from the default (./Cargo.lock) to
292-
PATH. PATH must end with Cargo.lock (e.g. --lockfile-path
293-
/tmp/temporary-lockfile/Cargo.lock). Note that providing
294-
--lockfile-path will ignore existing default lockfile
295-
(./Cargo.lock), if exists, and instead will either use PATH lockfile
296-
(or write a new lockfile into the provided path if it doesn’t
297-
exist). This flag can be used to run most commands in read-only
298-
directories, writing lockfile into the provided PATH.
291+
Changes the path of the lockfile from the default
292+
(<workspace_root>/Cargo.lock) to PATH. PATH must end with Cargo.lock
293+
(e.g. --lockfile-path /tmp/temporary-lockfile/Cargo.lock). Note that
294+
providing --lockfile-path will ignore existing default lockfile
295+
(<workspace_root>/Cargo.lock), if exists, and instead will either
296+
use PATH lockfile (or write a new lockfile into the provided path if
297+
it doesn’t exist). This flag can be used to run most commands in
298+
read-only directories, writing lockfile into the provided PATH.
299299

300300
This option is only available on the nightly channel
301301
<https://doc.rust-lang.org/book/appendix-07-nightly-rust.html> and

src/doc/man/generated_txt/cargo-clean.txt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,14 +124,14 @@ OPTIONS
124124
Equivalent to specifying both --locked and --offline.
125125

126126
--lockfile-path PATH
127-
Changes the path of the lockfile from the default (./Cargo.lock) to
128-
PATH. PATH must end with Cargo.lock (e.g. --lockfile-path
129-
/tmp/temporary-lockfile/Cargo.lock). Note that providing
130-
--lockfile-path will ignore existing default lockfile
131-
(./Cargo.lock), if exists, and instead will either use PATH lockfile
132-
(or write a new lockfile into the provided path if it doesn’t
133-
exist). This flag can be used to run most commands in read-only
134-
directories, writing lockfile into the provided PATH.
127+
Changes the path of the lockfile from the default
128+
(<workspace_root>/Cargo.lock) to PATH. PATH must end with Cargo.lock
129+
(e.g. --lockfile-path /tmp/temporary-lockfile/Cargo.lock). Note that
130+
providing --lockfile-path will ignore existing default lockfile
131+
(<workspace_root>/Cargo.lock), if exists, and instead will either
132+
use PATH lockfile (or write a new lockfile into the provided path if
133+
it doesn’t exist). This flag can be used to run most commands in
134+
read-only directories, writing lockfile into the provided PATH.
135135

136136
This option is only available on the nightly channel
137137
<https://doc.rust-lang.org/book/appendix-07-nightly-rust.html> and

src/doc/man/generated_txt/cargo-doc.txt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -259,14 +259,14 @@ OPTIONS
259259
Equivalent to specifying both --locked and --offline.
260260

261261
--lockfile-path PATH
262-
Changes the path of the lockfile from the default (./Cargo.lock) to
263-
PATH. PATH must end with Cargo.lock (e.g. --lockfile-path
264-
/tmp/temporary-lockfile/Cargo.lock). Note that providing
265-
--lockfile-path will ignore existing default lockfile
266-
(./Cargo.lock), if exists, and instead will either use PATH lockfile
267-
(or write a new lockfile into the provided path if it doesn’t
268-
exist). This flag can be used to run most commands in read-only
269-
directories, writing lockfile into the provided PATH.
262+
Changes the path of the lockfile from the default
263+
(<workspace_root>/Cargo.lock) to PATH. PATH must end with Cargo.lock
264+
(e.g. --lockfile-path /tmp/temporary-lockfile/Cargo.lock). Note that
265+
providing --lockfile-path will ignore existing default lockfile
266+
(<workspace_root>/Cargo.lock), if exists, and instead will either
267+
use PATH lockfile (or write a new lockfile into the provided path if
268+
it doesn’t exist). This flag can be used to run most commands in
269+
read-only directories, writing lockfile into the provided PATH.
270270

271271
This option is only available on the nightly channel
272272
<https://doc.rust-lang.org/book/appendix-07-nightly-rust.html> and

src/doc/man/generated_txt/cargo-fetch.txt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,14 @@ OPTIONS
103103
Equivalent to specifying both --locked and --offline.
104104

105105
--lockfile-path PATH
106-
Changes the path of the lockfile from the default (./Cargo.lock) to
107-
PATH. PATH must end with Cargo.lock (e.g. --lockfile-path
108-
/tmp/temporary-lockfile/Cargo.lock). Note that providing
109-
--lockfile-path will ignore existing default lockfile
110-
(./Cargo.lock), if exists, and instead will either use PATH lockfile
111-
(or write a new lockfile into the provided path if it doesn’t
112-
exist). This flag can be used to run most commands in read-only
113-
directories, writing lockfile into the provided PATH.
106+
Changes the path of the lockfile from the default
107+
(<workspace_root>/Cargo.lock) to PATH. PATH must end with Cargo.lock
108+
(e.g. --lockfile-path /tmp/temporary-lockfile/Cargo.lock). Note that
109+
providing --lockfile-path will ignore existing default lockfile
110+
(<workspace_root>/Cargo.lock), if exists, and instead will either
111+
use PATH lockfile (or write a new lockfile into the provided path if
112+
it doesn’t exist). This flag can be used to run most commands in
113+
read-only directories, writing lockfile into the provided PATH.
114114

115115
This option is only available on the nightly channel
116116
<https://doc.rust-lang.org/book/appendix-07-nightly-rust.html> and

src/doc/man/generated_txt/cargo-fix.txt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -361,14 +361,14 @@ OPTIONS
361361
Equivalent to specifying both --locked and --offline.
362362

363363
--lockfile-path PATH
364-
Changes the path of the lockfile from the default (./Cargo.lock) to
365-
PATH. PATH must end with Cargo.lock (e.g. --lockfile-path
366-
/tmp/temporary-lockfile/Cargo.lock). Note that providing
367-
--lockfile-path will ignore existing default lockfile
368-
(./Cargo.lock), if exists, and instead will either use PATH lockfile
369-
(or write a new lockfile into the provided path if it doesn’t
370-
exist). This flag can be used to run most commands in read-only
371-
directories, writing lockfile into the provided PATH.
364+
Changes the path of the lockfile from the default
365+
(<workspace_root>/Cargo.lock) to PATH. PATH must end with Cargo.lock
366+
(e.g. --lockfile-path /tmp/temporary-lockfile/Cargo.lock). Note that
367+
providing --lockfile-path will ignore existing default lockfile
368+
(<workspace_root>/Cargo.lock), if exists, and instead will either
369+
use PATH lockfile (or write a new lockfile into the provided path if
370+
it doesn’t exist). This flag can be used to run most commands in
371+
read-only directories, writing lockfile into the provided PATH.
372372

373373
This option is only available on the nightly channel
374374
<https://doc.rust-lang.org/book/appendix-07-nightly-rust.html> and

src/doc/man/generated_txt/cargo-generate-lockfile.txt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,14 @@ OPTIONS
8282
Equivalent to specifying both --locked and --offline.
8383

8484
--lockfile-path PATH
85-
Changes the path of the lockfile from the default (./Cargo.lock) to
86-
PATH. PATH must end with Cargo.lock (e.g. --lockfile-path
87-
/tmp/temporary-lockfile/Cargo.lock). Note that providing
88-
--lockfile-path will ignore existing default lockfile
89-
(./Cargo.lock), if exists, and instead will either use PATH lockfile
90-
(or write a new lockfile into the provided path if it doesn’t
91-
exist). This flag can be used to run most commands in read-only
92-
directories, writing lockfile into the provided PATH.
85+
Changes the path of the lockfile from the default
86+
(<workspace_root>/Cargo.lock) to PATH. PATH must end with Cargo.lock
87+
(e.g. --lockfile-path /tmp/temporary-lockfile/Cargo.lock). Note that
88+
providing --lockfile-path will ignore existing default lockfile
89+
(<workspace_root>/Cargo.lock), if exists, and instead will either
90+
use PATH lockfile (or write a new lockfile into the provided path if
91+
it doesn’t exist). This flag can be used to run most commands in
92+
read-only directories, writing lockfile into the provided PATH.
9393

9494
This option is only available on the nightly channel
9595
<https://doc.rust-lang.org/book/appendix-07-nightly-rust.html> and

0 commit comments

Comments
 (0)