Skip to content

Commit d18c60d

Browse files
committed
feat lockfile-path and related tests refactoring
1 parent 132ba16 commit d18c60d

File tree

5 files changed

+121
-81
lines changed

5 files changed

+121
-81
lines changed

src/bin/cargo/commands/fix.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::command_prelude::*;
22
use cargo::core::Workspace;
3-
use std::path::Path;
43

54
use cargo::ops;
65

@@ -77,8 +76,7 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {
7776
// Can't use workspace() to avoid using -Zavoid-dev-deps (if passed)
7877
let mut ws = Workspace::new(&root_manifest, gctx)?;
7978
ws.set_resolve_honors_rust_version(args.honor_rust_version());
80-
let lockfile_path =
81-
lockfile_path(args.get_one::<String>("lockfile-path").map(Path::new), gctx)?;
79+
let lockfile_path = args.lockfile_path(gctx)?;
8280
ws.set_requested_lockfile_path(lockfile_path.clone());
8381

8482
let mut opts = args.compile_options(gctx, mode, Some(&ws), ProfileChecking::LegacyTestOnly)?;
@@ -92,7 +90,6 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {
9290
gctx,
9391
&ws,
9492
&root_manifest,
95-
lockfile_path,
9693
&mut ops::FixOptions {
9794
edition: args.flag("edition"),
9895
idioms: args.flag("edition-idioms"),
@@ -101,6 +98,7 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {
10198
allow_no_vcs: args.flag("allow-no-vcs"),
10299
allow_staged: args.flag("allow-staged"),
103100
broken_code: args.flag("broken-code"),
101+
requested_lockfile_path: lockfile_path,
104102
},
105103
)?;
106104
Ok(())

src/cargo/core/workspace.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -651,16 +651,17 @@ impl<'gctx> Workspace<'gctx> {
651651
self
652652
}
653653

654+
/// Returns the directory where the lockfile is in.
654655
pub fn lock_root(&self) -> Filesystem {
655656
if let Some(requested) = self.requested_lockfile_path.as_ref() {
656657
return Filesystem::new(
657658
requested
658659
.parent()
659-
.unwrap_or_else(|| unreachable!("Lockfile path can't be root"))
660+
.expect("Lockfile path can't be root")
660661
.to_owned(),
661662
);
662663
}
663-
return self.default_lock_root();
664+
self.default_lock_root()
664665
}
665666

666667
fn default_lock_root(&self) -> Filesystem {

src/cargo/ops/fix.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,13 @@ pub struct FixOptions {
9797
pub allow_no_vcs: bool,
9898
pub allow_staged: bool,
9999
pub broken_code: bool,
100+
pub requested_lockfile_path: Option<PathBuf>,
100101
}
101102

102103
pub fn fix(
103104
gctx: &GlobalContext,
104105
original_ws: &Workspace<'_>,
105106
root_manifest: &Path,
106-
requested_lockfile_path: Option<PathBuf>,
107107
opts: &mut FixOptions,
108108
) -> CargoResult<()> {
109109
check_version_control(gctx, opts)?;
@@ -122,9 +122,7 @@ pub fn fix(
122122
}
123123
let mut ws = Workspace::new(&root_manifest, gctx)?;
124124
ws.set_resolve_honors_rust_version(Some(original_ws.resolve_honors_rust_version()));
125-
if let Some(p) = requested_lockfile_path {
126-
ws.set_requested_lockfile_path(Some(p))
127-
}
125+
ws.set_requested_lockfile_path(opts.requested_lockfile_path.clone());
128126

129127
// Spin up our lock server, which our subprocesses will use to synchronize fixes.
130128
let lock_server = LockServer::new()?;

src/cargo/util/command_prelude.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1014,7 +1014,7 @@ pub fn lockfile_path(
10141014

10151015
let path = gctx.cwd().join(lockfile_path);
10161016

1017-
if !path.ends_with(LOCKFILE_NAME) && !crate::util::toml::is_embedded(&path) {
1017+
if !path.ends_with(LOCKFILE_NAME) {
10181018
bail!("the lockfile-path must be a path to a {LOCKFILE_NAME} file")
10191019
}
10201020
if path.is_dir() {

0 commit comments

Comments
 (0)