Skip to content

Commit 778ff68

Browse files
committed
Factor out workspace reloading
This factors out the code to reload a workspace since I will be needing to reuse this elsewhere. There is some risk with this method not getting updated correctly in the future as new fields are added to Workspace (and it may be missing some things now), but that is an issue with the existing code.
1 parent 8d17219 commit 778ff68

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

src/bin/cargo/commands/fix.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {
9494
ops::fix(
9595
gctx,
9696
&ws,
97-
&root_manifest,
9897
&mut ops::FixOptions {
9998
edition: args.flag("edition"),
10099
idioms: args.flag("edition-idioms"),

src/cargo/core/workspace.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,18 @@ impl<'gctx> Workspace<'gctx> {
301301
Ok(ws)
302302
}
303303

304+
/// Reloads the workspace.
305+
///
306+
/// This is useful if the workspace has been updated, such as with `cargo
307+
/// fix` modifying the `Cargo.toml` file.
308+
pub fn reload(&self, gctx: &'gctx GlobalContext) -> CargoResult<Workspace<'gctx>> {
309+
let mut ws = Workspace::new(self.root_manifest(), gctx)?;
310+
ws.set_resolve_honors_rust_version(Some(self.resolve_honors_rust_version));
311+
ws.set_resolve_feature_unification(self.resolve_feature_unification);
312+
ws.set_requested_lockfile_path(self.requested_lockfile_path.clone());
313+
Ok(ws)
314+
}
315+
304316
fn set_resolve_behavior(&mut self) -> CargoResult<()> {
305317
// - If resolver is specified in the workspace definition, use that.
306318
// - If the root package specifies the resolver, use that.

src/cargo/ops/fix.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ pub struct FixOptions {
103103
pub fn fix(
104104
gctx: &GlobalContext,
105105
original_ws: &Workspace<'_>,
106-
root_manifest: &Path,
107106
opts: &mut FixOptions,
108107
) -> CargoResult<()> {
109108
check_version_control(gctx, opts)?;
@@ -120,10 +119,7 @@ pub fn fix(
120119

121120
check_resolver_change(&original_ws, &mut target_data, opts)?;
122121
}
123-
let mut ws = Workspace::new(&root_manifest, gctx)?;
124-
ws.set_resolve_honors_rust_version(Some(original_ws.resolve_honors_rust_version()));
125-
ws.set_resolve_feature_unification(original_ws.resolve_feature_unification());
126-
ws.set_requested_lockfile_path(opts.requested_lockfile_path.clone());
122+
let ws = original_ws.reload(gctx)?;
127123

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

0 commit comments

Comments
 (0)