Skip to content

Commit 132ba16

Browse files
committed
refactoring: move lock_root into worspace
1 parent bf7e2dc commit 132ba16

File tree

5 files changed

+30
-28
lines changed

5 files changed

+30
-28
lines changed

src/bin/cargo/commands/fix.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {
7979
ws.set_resolve_honors_rust_version(args.honor_rust_version());
8080
let lockfile_path =
8181
lockfile_path(args.get_one::<String>("lockfile-path").map(Path::new), gctx)?;
82-
ws.set_requested_lockfile_path(lockfile_path);
82+
ws.set_requested_lockfile_path(lockfile_path.clone());
8383

8484
let mut opts = args.compile_options(gctx, mode, Some(&ws), ProfileChecking::LegacyTestOnly)?;
8585

@@ -92,6 +92,7 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {
9292
gctx,
9393
&ws,
9494
&root_manifest,
95+
lockfile_path,
9596
&mut ops::FixOptions {
9697
edition: args.flag("edition"),
9798
idioms: args.flag("edition-idioms"),

src/cargo/core/workspace.rs

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

654-
pub fn requested_lockfile_path(&self) -> Option<&PathBuf> {
655-
self.requested_lockfile_path.as_ref()
654+
pub fn lock_root(&self) -> Filesystem {
655+
if let Some(requested) = self.requested_lockfile_path.as_ref() {
656+
return Filesystem::new(
657+
requested
658+
.parent()
659+
.unwrap_or_else(|| unreachable!("Lockfile path can't be root"))
660+
.to_owned(),
661+
);
662+
}
663+
return self.default_lock_root();
664+
}
665+
666+
fn default_lock_root(&self) -> Filesystem {
667+
if self.root_maybe().is_embedded() {
668+
self.target_dir()
669+
} else {
670+
Filesystem::new(self.root().to_owned())
671+
}
656672
}
657673

658674
pub fn set_requested_lockfile_path(&mut self, path: Option<PathBuf>) {

src/cargo/ops/cargo_package.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ use std::path::{Path, PathBuf};
66
use std::sync::Arc;
77
use std::task::Poll;
88

9+
use super::RegistryOrIndex;
910
use crate::core::compiler::{BuildConfig, CompileMode, DefaultExecutor, Executor};
1011
use crate::core::dependency::DepKind;
1112
use crate::core::manifest::Target;
1213
use crate::core::resolver::CliFeatures;
1314
use crate::core::resolver::HasDevUnits;
1415
use crate::core::{Feature, PackageIdSpecQuery, Shell, Verbosity, Workspace};
1516
use crate::core::{Package, PackageId, PackageSet, Resolve, SourceId};
17+
use crate::ops::lockfile::LOCKFILE_NAME;
1618
use crate::sources::registry::index::{IndexPackage, RegistryDependency};
1719
use crate::sources::{PathSource, SourceConfigMap, CRATES_IO_REGISTRY};
1820
use crate::util::cache_lock::CacheLockMode;
@@ -32,8 +34,6 @@ use tar::{Archive, Builder, EntryType, Header, HeaderMode};
3234
use tracing::debug;
3335
use unicase::Ascii as UncasedAscii;
3436

35-
use super::RegistryOrIndex;
36-
3737
#[derive(Clone)]
3838
pub struct PackageOpts<'gctx> {
3939
pub gctx: &'gctx GlobalContext,
@@ -248,10 +248,10 @@ pub fn package(ws: &Workspace<'_>, opts: &PackageOpts<'_>) -> CargoResult<Vec<Fi
248248
}
249249
let pkgs = ws.members_with_features(specs, &opts.cli_features)?;
250250

251-
let default_lockfile = ws.root().join("Cargo.lock");
252251
if ws
253-
.requested_lockfile_path()
254-
.unwrap_or_else(|| &default_lockfile)
252+
.lock_root()
253+
.as_path_unlocked()
254+
.join(LOCKFILE_NAME)
255255
.exists()
256256
{
257257
// Make sure the Cargo.lock is up-to-date and valid.

src/cargo/ops/fix.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ pub fn fix(
103103
gctx: &GlobalContext,
104104
original_ws: &Workspace<'_>,
105105
root_manifest: &Path,
106+
requested_lockfile_path: Option<PathBuf>,
106107
opts: &mut FixOptions,
107108
) -> CargoResult<()> {
108109
check_version_control(gctx, opts)?;
@@ -121,8 +122,8 @@ pub fn fix(
121122
}
122123
let mut ws = Workspace::new(&root_manifest, gctx)?;
123124
ws.set_resolve_honors_rust_version(Some(original_ws.resolve_honors_rust_version()));
124-
if let Some(p) = original_ws.requested_lockfile_path() {
125-
ws.set_requested_lockfile_path(Some(p.clone()))
125+
if let Some(p) = requested_lockfile_path {
126+
ws.set_requested_lockfile_path(Some(p))
126127
}
127128

128129
// Spin up our lock server, which our subprocesses will use to synchronize fixes.

src/cargo/ops/lockfile.rs

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pub const LOCKFILE_NAME: &str = "Cargo.lock";
1010

1111
#[tracing::instrument(skip_all)]
1212
pub fn load_pkg_lockfile(ws: &Workspace<'_>) -> CargoResult<Option<Resolve>> {
13-
let lock_root = lock_root(ws);
13+
let lock_root = ws.lock_root();
1414
if !lock_root.as_path_unlocked().join(LOCKFILE_NAME).exists() {
1515
return Ok(None);
1616
}
@@ -106,7 +106,7 @@ fn resolve_to_string_orig(
106106
resolve: &Resolve,
107107
) -> (Option<String>, String, Filesystem) {
108108
// Load the original lock file if it exists.
109-
let lock_root = lock_root(ws);
109+
let lock_root = ws.lock_root();
110110
let orig = lock_root.open_ro_shared(LOCKFILE_NAME, ws.gctx(), "Cargo.lock file");
111111
let orig = orig.and_then(|mut f| {
112112
let mut s = String::new();
@@ -247,19 +247,3 @@ fn emit_package(dep: &toml::Table, out: &mut String) {
247247
out.push_str(&format!("replace = {}\n\n", &dep["replace"]));
248248
}
249249
}
250-
251-
fn lock_root(ws: &Workspace<'_>) -> Filesystem {
252-
if let Some(requested) = ws.requested_lockfile_path() {
253-
return Filesystem::new(
254-
requested
255-
.parent()
256-
.unwrap_or_else(|| unreachable!("Lockfile path can't be root"))
257-
.to_owned(),
258-
);
259-
}
260-
if ws.root_maybe().is_embedded() {
261-
ws.target_dir()
262-
} else {
263-
Filesystem::new(ws.root().to_owned())
264-
}
265-
}

0 commit comments

Comments
 (0)