Skip to content

Commit 63d0fe4

Browse files
committed
Auto merge of #8725 - chaaz:master, r=ehuss
Add "--workspace" to update command My `--bin` project has CI which updates the version number in `Cargo.toml`, which it then commits. However, this means that any further cargo command (`build`, `test`, etc) will update the existing `Cargo.lock` file (updating the root version), causing some frustration for users. Furthermore, it breaks the `publish` command, which requires the repo to be current. I've added a `sync-lockfile` command to simply update the root version in the `Cargo.lock` file to match the `Cargo.toml` in the same way that simple commands like `fetch` do. If no `Cargo.lock` file is present, and new one is generated based on the index. This is a demo PR for Pre-RFC conversation at https://internals.rust-lang.org/t/pre-rfc-cargo-command-to-just-sync-lockfile/13119, but may become a real PR if it gets approval.
2 parents cee17c2 + 42f3469 commit 63d0fe4

File tree

7 files changed

+62
-2
lines changed

7 files changed

+62
-2
lines changed

src/bin/cargo/commands/update.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ pub fn cli() -> App {
77
subcommand("update")
88
.about("Update dependencies as recorded in the local lock file")
99
.arg(opt("quiet", "No output printed to stdout").short("q"))
10+
.arg(opt("workspace", "Only update the workspace packages").short("w"))
1011
.arg_package_spec_simple("Package to update")
1112
.arg(opt(
1213
"aggressive",
@@ -30,6 +31,7 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
3031
precise: args.value_of("precise"),
3132
to_update: values(args, "package"),
3233
dry_run: args.is_present("dry-run"),
34+
workspace: args.is_present("workspace"),
3335
config,
3436
};
3537
ops::update_lockfile(&ws, &update_opts)?;

src/cargo/ops/cargo_generate_lockfile.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub struct UpdateOptions<'a> {
1717
pub precise: Option<&'a str>,
1818
pub aggressive: bool,
1919
pub dry_run: bool,
20+
pub workspace: bool,
2021
}
2122

2223
pub fn generate_lockfile(ws: &Workspace<'_>) -> CargoResult<()> {
@@ -78,8 +79,10 @@ pub fn update_lockfile(ws: &Workspace<'_>, opts: &UpdateOptions<'_>) -> CargoRes
7879
let mut to_avoid = HashSet::new();
7980

8081
if opts.to_update.is_empty() {
81-
to_avoid.extend(previous_resolve.iter());
82-
to_avoid.extend(previous_resolve.unused_patches());
82+
if !opts.workspace {
83+
to_avoid.extend(previous_resolve.iter());
84+
to_avoid.extend(previous_resolve.unused_patches());
85+
}
8386
} else {
8487
let mut sources = Vec::new();
8588
for name in opts.to_update.iter() {

src/doc/man/cargo-update.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ the package to. If the package comes from a git repository, this can be a git
4444
revision (such as a SHA hash or tag).
4545
{{/option}}
4646

47+
{{#option "`-w`" "`--workspace`" }}
48+
Attempt to update only packages defined in the workspace. Other packages
49+
are updated only if they don't already exist in the lockfile. This
50+
option is useful for updating `Cargo.lock` after you've changed version
51+
numbers in `Cargo.toml`.
52+
{{/option}}
53+
4754
{{#option "`--dry-run`" }}
4855
Displays what would be updated, but doesn't actually write the lockfile.
4956
{{/option}}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ OPTIONS
3535
to set the package to. If the package comes from a git repository,
3636
this can be a git revision (such as a SHA hash or tag).
3737

38+
-w, --workspace
39+
Attempt to update only packages defined in the workspace. Other
40+
packages are updated only if they don't already exist in the
41+
lockfile. This option is useful for updating Cargo.lock after you've
42+
changed version numbers in Cargo.toml.
43+
3844
--dry-run
3945
Displays what would be updated, but doesn't actually write the
4046
lockfile.

src/doc/src/commands/cargo-update.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ the package to. If the package comes from a git repository, this can be a git
4343
revision (such as a SHA hash or tag).</dd>
4444

4545

46+
<dt class="option-term" id="option-cargo-update--w"><a class="option-anchor" href="#option-cargo-update--w"></a><code>-w</code></dt>
47+
<dt class="option-term" id="option-cargo-update---workspace"><a class="option-anchor" href="#option-cargo-update---workspace"></a><code>--workspace</code></dt>
48+
<dd class="option-desc">Attempt to update only packages defined in the workspace. Other packages
49+
are updated only if they don't already exist in the lockfile. This
50+
option is useful for updating <code>Cargo.lock</code> after you've changed version
51+
numbers in <code>Cargo.toml</code>.</dd>
52+
53+
4654
<dt class="option-term" id="option-cargo-update---dry-run"><a class="option-anchor" href="#option-cargo-update---dry-run"></a><code>--dry-run</code></dt>
4755
<dd class="option-desc">Displays what would be updated, but doesn't actually write the lockfile.</dd>
4856

src/etc/man/cargo-update.1

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ the package to. If the package comes from a git repository, this can be a git
4242
revision (such as a SHA hash or tag).
4343
.RE
4444
.sp
45+
\fB\-w\fR,
46+
\fB\-\-workspace\fR
47+
.RS 4
48+
Attempt to update only packages defined in the workspace. Other packages
49+
are updated only if they don't already exist in the lockfile. This
50+
option is useful for updating \fBCargo.lock\fR after you've changed version
51+
numbers in \fBCargo.toml\fR\&.
52+
.RE
53+
.sp
4554
\fB\-\-dry\-run\fR
4655
.RS 4
4756
Displays what would be updated, but doesn't actually write the lockfile.

tests/testsuite/update.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,3 +651,28 @@ fn dry_run_update() {
651651
let new_lockfile = p.read_lockfile();
652652
assert_eq!(old_lockfile, new_lockfile)
653653
}
654+
655+
#[cargo_test]
656+
fn workspace_only() {
657+
let p = project().file("src/main.rs", "fn main() {}").build();
658+
p.cargo("generate-lockfile").run();
659+
let lock1 = p.read_lockfile();
660+
661+
p.change_file(
662+
"Cargo.toml",
663+
r#"
664+
[package]
665+
name = "foo"
666+
authors = []
667+
version = "0.0.2"
668+
"#,
669+
);
670+
p.cargo("update --workspace").run();
671+
let lock2 = p.read_lockfile();
672+
673+
assert_ne!(lock1, lock2);
674+
assert!(lock1.contains("0.0.1"));
675+
assert!(lock2.contains("0.0.2"));
676+
assert!(!lock1.contains("0.0.2"));
677+
assert!(!lock2.contains("0.0.1"));
678+
}

0 commit comments

Comments
 (0)