Skip to content

Commit 4ed0537

Browse files
committed
Add sync-lockfile command
1 parent d5556ae commit 4ed0537

File tree

6 files changed

+185
-0
lines changed

6 files changed

+185
-0
lines changed

src/bin/cargo/commands/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ pub fn builtin() -> Vec<App> {
88
clean::cli(),
99
doc::cli(),
1010
fetch::cli(),
11+
sync_lockfile::cli(),
1112
fix::cli(),
1213
generate_lockfile::cli(),
1314
git_checkout::cli(),
@@ -45,6 +46,7 @@ pub fn builtin_exec(cmd: &str) -> Option<fn(&mut Config, &ArgMatches<'_>) -> Cli
4546
"clean" => clean::exec,
4647
"doc" => doc::exec,
4748
"fetch" => fetch::exec,
49+
"sync-lockfile" => sync_lockfile::exec,
4850
"fix" => fix::exec,
4951
"generate-lockfile" => generate_lockfile::exec,
5052
"git-checkout" => git_checkout::exec,
@@ -82,6 +84,7 @@ pub mod check;
8284
pub mod clean;
8385
pub mod doc;
8486
pub mod fetch;
87+
pub mod sync_lockfile;
8588
pub mod fix;
8689
pub mod generate_lockfile;
8790
pub mod git_checkout;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use crate::command_prelude::*;
2+
3+
use cargo::ops;
4+
use cargo::ops::SyncLockfileOptions;
5+
6+
pub fn cli() -> App {
7+
subcommand("sync-lockfile")
8+
.about("Synchronize the lockfile to the package")
9+
.arg(opt("quiet", "No output printed to stdout").short("q"))
10+
.arg_manifest_path()
11+
.arg_target_triple("Sync the lockfile for the target triple")
12+
.after_help("Run `cargo help sync-lockfile` for more detailed information.\n")
13+
}
14+
15+
pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
16+
let ws = args.workspace(config)?;
17+
18+
let opts = SyncLockfileOptions {
19+
config,
20+
targets: args.targets(),
21+
};
22+
let _ = ops::sync_lockfile(&ws, &opts)?;
23+
Ok(())
24+
}

src/cargo/ops/cargo_sync_lockfile.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use crate::core::{PackageSet, Resolve, Workspace};
2+
use crate::ops;
3+
use crate::util::CargoResult;
4+
use crate::util::Config;
5+
6+
pub struct SyncLockfileOptions<'a> {
7+
pub config: &'a Config,
8+
/// The target arch triple to sync lockfile dependencies for
9+
pub targets: Vec<String>,
10+
}
11+
12+
/// Executes `cargo sync_lockfile`.
13+
pub fn sync_lockfile<'a>(
14+
ws: &Workspace<'a>,
15+
_options: &SyncLockfileOptions<'a>,
16+
) -> CargoResult<(Resolve, PackageSet<'a>)> {
17+
ws.emit_warnings()?;
18+
let (packages, resolve) = ops::resolve_ws(ws)?;
19+
Ok((resolve, packages))
20+
}

src/cargo/ops/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ pub use self::cargo_compile::{
55
pub use self::cargo_compile::{CompileFilter, FilterRule, LibRule, Packages};
66
pub use self::cargo_doc::{doc, DocOptions};
77
pub use self::cargo_fetch::{fetch, FetchOptions};
8+
pub use self::cargo_sync_lockfile::{sync_lockfile, SyncLockfileOptions};
89
pub use self::cargo_generate_lockfile::generate_lockfile;
910
pub use self::cargo_generate_lockfile::update_lockfile;
1011
pub use self::cargo_generate_lockfile::UpdateOptions;
@@ -33,6 +34,7 @@ mod cargo_clean;
3334
mod cargo_compile;
3435
mod cargo_doc;
3536
mod cargo_fetch;
37+
mod cargo_sync_lockfile;
3638
mod cargo_generate_lockfile;
3739
mod cargo_install;
3840
mod cargo_new;

tests/testsuite/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ mod rustflags;
109109
mod search;
110110
mod shell_quoting;
111111
mod standard_lib;
112+
mod sync_lockfile;
112113
mod test;
113114
mod timings;
114115
mod tool_paths;

tests/testsuite/sync_lockfile.rs

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
//! Tests for the `cargo sync-lockfile` command.
2+
3+
use cargo_test_support::registry::Package;
4+
use cargo_test_support::rustc_host;
5+
use cargo_test_support::{basic_manifest, cross_compile, project};
6+
7+
#[cargo_test]
8+
fn no_deps() {
9+
let p = project()
10+
.file("src/main.rs", "mod a; fn main() {}")
11+
.file("src/a.rs", "")
12+
.build();
13+
14+
p.cargo("sync-lockfile").with_stdout("").run();
15+
}
16+
17+
#[cargo_test]
18+
fn sync_all_platform_dependencies_when_no_target_is_given() {
19+
if cross_compile::disabled() {
20+
return;
21+
}
22+
23+
Package::new("d1", "1.2.3")
24+
.file("Cargo.toml", &basic_manifest("d1", "1.2.3"))
25+
.file("src/lib.rs", "")
26+
.publish();
27+
28+
Package::new("d2", "0.1.2")
29+
.file("Cargo.toml", &basic_manifest("d2", "0.1.2"))
30+
.file("src/lib.rs", "")
31+
.publish();
32+
33+
let target = cross_compile::alternate();
34+
let host = rustc_host();
35+
let p = project()
36+
.file(
37+
"Cargo.toml",
38+
&format!(
39+
r#"
40+
[package]
41+
name = "foo"
42+
version = "0.0.1"
43+
authors = []
44+
45+
[target.{host}.dependencies]
46+
d1 = "1.2.3"
47+
48+
[target.{target}.dependencies]
49+
d2 = "0.1.2"
50+
"#,
51+
host = host,
52+
target = target
53+
),
54+
)
55+
.file("src/lib.rs", "")
56+
.build();
57+
58+
p.cargo("sync-lockfile")
59+
.with_stderr_contains("[DOWNLOADED] d1 v1.2.3 [..]")
60+
.with_stderr_contains("[DOWNLOADED] d2 v0.1.2 [..]")
61+
.run();
62+
}
63+
64+
#[cargo_test]
65+
fn sync_platform_specific_dependencies() {
66+
if cross_compile::disabled() {
67+
return;
68+
}
69+
70+
Package::new("d1", "1.2.3")
71+
.file("Cargo.toml", &basic_manifest("d1", "1.2.3"))
72+
.file("src/lib.rs", "")
73+
.publish();
74+
75+
Package::new("d2", "0.1.2")
76+
.file("Cargo.toml", &basic_manifest("d2", "0.1.2"))
77+
.file("src/lib.rs", "")
78+
.publish();
79+
80+
let target = cross_compile::alternate();
81+
let host = rustc_host();
82+
let p = project()
83+
.file(
84+
"Cargo.toml",
85+
&format!(
86+
r#"
87+
[package]
88+
name = "foo"
89+
version = "0.0.1"
90+
authors = []
91+
92+
[target.{host}.dependencies]
93+
d1 = "1.2.3"
94+
95+
[target.{target}.dependencies]
96+
d2 = "0.1.2"
97+
"#,
98+
host = host,
99+
target = target
100+
),
101+
)
102+
.file("src/lib.rs", "")
103+
.build();
104+
105+
p.cargo("sync-lockfile --target")
106+
.arg(&host)
107+
.with_stderr_contains("[DOWNLOADED] d1 v1.2.3 [..]")
108+
.with_stderr_does_not_contain("[DOWNLOADED] d2 v0.1.2 [..]")
109+
.run();
110+
111+
p.cargo("sync-lockfile --target")
112+
.arg(&target)
113+
.with_stderr_contains("[DOWNLOADED] d2 v0.1.2[..]")
114+
.with_stderr_does_not_contain("[DOWNLOADED] d1 v1.2.3 [..]")
115+
.run();
116+
}
117+
118+
#[cargo_test]
119+
fn sync_warning() {
120+
let p = project()
121+
.file(
122+
"Cargo.toml",
123+
r#"
124+
[package]
125+
name = "foo"
126+
version = "1.0.0"
127+
misspelled = "wut"
128+
"#,
129+
)
130+
.file("src/lib.rs", "")
131+
.build();
132+
p.cargo("sync-lockfile")
133+
.with_stderr("[WARNING] unused manifest key: package.misspelled")
134+
.run();
135+
}

0 commit comments

Comments
 (0)