Skip to content

Commit a504f48

Browse files
committed
Add flags to assert lock/cache behavior to Cargo
If a lock file is generated and some equivalent of `cargo fetch` is run then Cargo shouldn't ever touch the network or modify `Cargo.lock` until any `Cargo.toml` later changes, but this often wants to be asserted in some build environments where it's a programmer error if Cargo attempts to access the network. The `--locked` flag added here will assert that `Cargo.lock` does not need to change to proceed. That is, if `Cargo.lock` would be modified (as it automatically is by default) this is turned into a hard error instead. This `--frozen` will not only assert that `Cargo.lock` doesn't change (the same behavior as `--locked`), but it will also will manually prevent Cargo from touching the network by ensuring that all network requests return an error. These flags can be used in environments where it is *expected* that no network access happens (or no lockfile changes happen) because it has been pre-arranged for Cargo to not happen. Examples of this include: * CI for projects want to pass `--locked` to ensure that `Cargo.lock` is up to date before changes are checked in. * Environments with vendored dependencies want to pass `--frozen` as touching the network indicates a programmer error that something wasn't vendored correctly. A crucial property of these two flags is that **they do not change the behavior of Cargo**. They are simply assertions at a few locations in Cargo to ensure that actions expected to not happen indeed don't happen. Some documentation has also been added to this effect. Closes #2111
1 parent f2cf284 commit a504f48

38 files changed

+379
-95
lines changed

src/bin/bench.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ pub struct Options {
2020
flag_example: Vec<String>,
2121
flag_test: Vec<String>,
2222
flag_bench: Vec<String>,
23+
flag_frozen: bool,
24+
flag_locked: bool,
2325
arg_args: Vec<String>,
2426
}
2527

@@ -46,6 +48,8 @@ Options:
4648
-v, --verbose ... Use verbose output
4749
-q, --quiet No output printed to stdout
4850
--color WHEN Coloring: auto, always, never
51+
--frozen Require Cargo.lock and cache are up to date
52+
--locked Require Cargo.lock is up to date
4953
5054
All of the trailing arguments are passed to the benchmark binaries generated
5155
for filtering benchmarks and generally providing options configuring how they
@@ -64,9 +68,11 @@ Compilation can be customized with the `bench` profile in the manifest.
6468

6569
pub fn execute(options: Options, config: &Config) -> CliResult<Option<()>> {
6670
let root = try!(find_root_manifest_for_wd(options.flag_manifest_path, config.cwd()));
67-
try!(config.configure_shell(options.flag_verbose,
68-
options.flag_quiet,
69-
&options.flag_color));
71+
try!(config.configure(options.flag_verbose,
72+
options.flag_quiet,
73+
&options.flag_color,
74+
options.flag_frozen,
75+
options.flag_locked));
7076

7177
let ops = ops::TestOptions {
7278
no_run: options.flag_no_run,

src/bin/build.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ pub struct Options {
2323
flag_example: Vec<String>,
2424
flag_test: Vec<String>,
2525
flag_bench: Vec<String>,
26+
flag_locked: bool,
27+
flag_frozen: bool,
2628
}
2729

2830
pub const USAGE: &'static str = "
@@ -48,6 +50,8 @@ Options:
4850
-v, --verbose ... Use verbose output
4951
-q, --quiet No output printed to stdout
5052
--color WHEN Coloring: auto, always, never
53+
--frozen Require Cargo.lock and cache are up to date
54+
--locked Require Cargo.lock is up to date
5155
5256
If the --package argument is given, then SPEC is a package id specification
5357
which indicates which package should be built. If it is not given, then the
@@ -62,9 +66,11 @@ the --release flag will use the `release` profile instead.
6266
pub fn execute(options: Options, config: &Config) -> CliResult<Option<()>> {
6367
debug!("executing; cmd=cargo-build; args={:?}",
6468
env::args().collect::<Vec<_>>());
65-
try!(config.configure_shell(options.flag_verbose,
66-
options.flag_quiet,
67-
&options.flag_color));
69+
try!(config.configure(options.flag_verbose,
70+
options.flag_quiet,
71+
&options.flag_color,
72+
options.flag_frozen,
73+
options.flag_locked));
6874

6975
let root = try!(find_root_manifest_for_wd(options.flag_manifest_path, config.cwd()));
7076

src/bin/cargo.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ pub struct Flags {
2828
flag_explain: Option<String>,
2929
arg_command: String,
3030
arg_args: Vec<String>,
31+
flag_locked: bool,
32+
flag_frozen: bool,
3133
}
3234

3335
const USAGE: &'static str = "
@@ -45,6 +47,8 @@ Options:
4547
-v, --verbose ... Use verbose output
4648
-q, --quiet No output printed to stdout
4749
--color WHEN Coloring: auto, always, never
50+
--frozen Require Cargo.lock and cache are up to date
51+
--locked Require Cargo.lock is up to date
4852
4953
Some common cargo commands are (see all commands with --list):
5054
build Compile the current project
@@ -68,6 +72,16 @@ fn main() {
6872
execute_main_without_stdin(execute, true, USAGE)
6973
}
7074

75+
macro_rules! configure_shell {
76+
($config:expr, $options:expr) => (
77+
try!($config.configure($options.flag_verbose,
78+
$options.flag_quiet,
79+
&$options.flag_color,
80+
$options.flag_frozen,
81+
$options.flag_locked));
82+
)
83+
}
84+
7185
macro_rules! each_subcommand{
7286
($mac:ident) => {
7387
$mac!(bench);
@@ -113,9 +127,11 @@ each_subcommand!(declare_mod);
113127
on this top-level information.
114128
*/
115129
fn execute(flags: Flags, config: &Config) -> CliResult<Option<()>> {
116-
try!(config.configure_shell(flags.flag_verbose,
117-
flags.flag_quiet,
118-
&flags.flag_color));
130+
try!(config.configure(flags.flag_verbose,
131+
flags.flag_quiet,
132+
&flags.flag_color,
133+
flags.flag_frozen,
134+
flags.flag_locked));
119135

120136
init_git_transports(config);
121137
cargo::util::job::setup();

src/bin/clean.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ pub struct Options {
1414
flag_quiet: Option<bool>,
1515
flag_color: Option<String>,
1616
flag_release: bool,
17+
flag_frozen: bool,
18+
flag_locked: bool,
1719
}
1820

1921
pub const USAGE: &'static str = "
@@ -31,6 +33,8 @@ Options:
3133
-v, --verbose ... Use verbose output
3234
-q, --quiet No output printed to stdout
3335
--color WHEN Coloring: auto, always, never
36+
--frozen Require Cargo.lock and cache are up to date
37+
--locked Require Cargo.lock is up to date
3438
3539
If the --package argument is given, then SPEC is a package id specification
3640
which indicates which package's artifacts should be cleaned out. If it is not
@@ -40,9 +44,11 @@ and its format, see the `cargo help pkgid` command.
4044

4145
pub fn execute(options: Options, config: &Config) -> CliResult<Option<()>> {
4246
debug!("executing; cmd=cargo-clean; args={:?}", env::args().collect::<Vec<_>>());
43-
try!(config.configure_shell(options.flag_verbose,
44-
options.flag_quiet,
45-
&options.flag_color));
47+
try!(config.configure(options.flag_verbose,
48+
options.flag_quiet,
49+
&options.flag_color,
50+
options.flag_frozen,
51+
options.flag_locked));
4652

4753
let root = try!(find_root_manifest_for_wd(options.flag_manifest_path, config.cwd()));
4854
let opts = ops::CleanOptions {

src/bin/doc.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ pub struct Options {
1919
flag_package: Vec<String>,
2020
flag_lib: bool,
2121
flag_bin: Vec<String>,
22+
flag_frozen: bool,
23+
flag_locked: bool,
2224
}
2325

2426
pub const USAGE: &'static str = "
@@ -43,6 +45,8 @@ Options:
4345
-v, --verbose ... Use verbose output
4446
-q, --quiet No output printed to stdout
4547
--color WHEN Coloring: auto, always, never
48+
--frozen Require Cargo.lock and cache are up to date
49+
--locked Require Cargo.lock is up to date
4650
4751
By default the documentation for the local package and all dependencies is
4852
built. The output is all placed in `target/doc` in rustdoc's usual format.
@@ -54,9 +58,11 @@ the `cargo help pkgid` command.
5458
";
5559

5660
pub fn execute(options: Options, config: &Config) -> CliResult<Option<()>> {
57-
try!(config.configure_shell(options.flag_verbose,
58-
options.flag_quiet,
59-
&options.flag_color));
61+
try!(config.configure(options.flag_verbose,
62+
options.flag_quiet,
63+
&options.flag_color,
64+
options.flag_frozen,
65+
options.flag_locked));
6066

6167
let root = try!(find_root_manifest_for_wd(options.flag_manifest_path, config.cwd()));
6268

src/bin/fetch.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ pub struct Options {
99
flag_verbose: u32,
1010
flag_quiet: Option<bool>,
1111
flag_color: Option<String>,
12+
flag_frozen: bool,
13+
flag_locked: bool,
1214
}
1315

1416
pub const USAGE: &'static str = "
@@ -23,6 +25,8 @@ Options:
2325
-v, --verbose ... Use verbose output
2426
-q, --quiet No output printed to stdout
2527
--color WHEN Coloring: auto, always, never
28+
--frozen Require Cargo.lock and cache are up to date
29+
--locked Require Cargo.lock is up to date
2630
2731
If a lockfile is available, this command will ensure that all of the git
2832
dependencies and/or registries dependencies are downloaded and locally
@@ -35,9 +39,11 @@ all updated.
3539
";
3640

3741
pub fn execute(options: Options, config: &Config) -> CliResult<Option<()>> {
38-
try!(config.configure_shell(options.flag_verbose,
39-
options.flag_quiet,
40-
&options.flag_color));
42+
try!(config.configure(options.flag_verbose,
43+
options.flag_quiet,
44+
&options.flag_color,
45+
options.flag_frozen,
46+
options.flag_locked));
4147
let root = try!(find_root_manifest_for_wd(options.flag_manifest_path, config.cwd()));
4248
let ws = try!(Workspace::new(&root, config));
4349
try!(ops::fetch(&ws));

src/bin/generate_lockfile.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ pub struct Options {
1111
flag_verbose: u32,
1212
flag_quiet: Option<bool>,
1313
flag_color: Option<String>,
14+
flag_frozen: bool,
15+
flag_locked: bool,
1416
}
1517

1618
pub const USAGE: &'static str = "
@@ -25,13 +27,17 @@ Options:
2527
-v, --verbose ... Use verbose output
2628
-q, --quiet No output printed to stdout
2729
--color WHEN Coloring: auto, always, never
30+
--frozen Require Cargo.lock and cache are up to date
31+
--locked Require Cargo.lock is up to date
2832
";
2933

3034
pub fn execute(options: Options, config: &Config) -> CliResult<Option<()>> {
3135
debug!("executing; cmd=cargo-generate-lockfile; args={:?}", env::args().collect::<Vec<_>>());
32-
try!(config.configure_shell(options.flag_verbose,
33-
options.flag_quiet,
34-
&options.flag_color));
36+
try!(config.configure(options.flag_verbose,
37+
options.flag_quiet,
38+
&options.flag_color,
39+
options.flag_frozen,
40+
options.flag_locked));
3541
let root = try!(find_root_manifest_for_wd(options.flag_manifest_path, config.cwd()));
3642

3743
let ws = try!(Workspace::new(&root, config));

src/bin/git_checkout.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ pub struct Options {
99
flag_verbose: u32,
1010
flag_quiet: Option<bool>,
1111
flag_color: Option<String>,
12+
flag_frozen: bool,
13+
flag_locked: bool,
1214
}
1315

1416
pub const USAGE: &'static str = "
@@ -23,12 +25,16 @@ Options:
2325
-v, --verbose ... Use verbose output
2426
-q, --quiet No output printed to stdout
2527
--color WHEN Coloring: auto, always, never
28+
--frozen Require Cargo.lock and cache are up to date
29+
--locked Require Cargo.lock is up to date
2630
";
2731

2832
pub fn execute(options: Options, config: &Config) -> CliResult<Option<()>> {
29-
try!(config.configure_shell(options.flag_verbose,
30-
options.flag_quiet,
31-
&options.flag_color));
33+
try!(config.configure(options.flag_verbose,
34+
options.flag_quiet,
35+
&options.flag_color,
36+
options.flag_frozen,
37+
options.flag_locked));
3238
let Options { flag_url: url, flag_reference: reference, .. } = options;
3339

3440
let url = try!(url.to_url().map_err(|e| {

src/bin/init.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ pub struct Options {
1212
arg_path: Option<String>,
1313
flag_name: Option<String>,
1414
flag_vcs: Option<ops::VersionControl>,
15+
flag_frozen: bool,
16+
flag_locked: bool,
1517
}
1618

1719
pub const USAGE: &'static str = "
@@ -31,13 +33,17 @@ Options:
3133
-v, --verbose ... Use verbose output
3234
-q, --quiet No output printed to stdout
3335
--color WHEN Coloring: auto, always, never
36+
--frozen Require Cargo.lock and cache are up to date
37+
--locked Require Cargo.lock is up to date
3438
";
3539

3640
pub fn execute(options: Options, config: &Config) -> CliResult<Option<()>> {
3741
debug!("executing; cmd=cargo-init; args={:?}", env::args().collect::<Vec<_>>());
38-
try!(config.configure_shell(options.flag_verbose,
39-
options.flag_quiet,
40-
&options.flag_color));
42+
try!(config.configure(options.flag_verbose,
43+
options.flag_quiet,
44+
&options.flag_color,
45+
options.flag_frozen,
46+
options.flag_locked));
4147

4248
let Options { flag_bin, arg_path, flag_name, flag_vcs, .. } = options;
4349

src/bin/install.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ pub struct Options {
1616
flag_root: Option<String>,
1717
flag_list: bool,
1818
flag_force: bool,
19+
flag_frozen: bool,
20+
flag_locked: bool,
1921

2022
arg_crate: Option<String>,
2123
flag_vers: Option<String>,
@@ -56,6 +58,8 @@ Build and install options:
5658
-v, --verbose ... Use verbose output
5759
-q, --quiet Less output printed to stdout
5860
--color WHEN Coloring: auto, always, never
61+
--frozen Require Cargo.lock and cache are up to date
62+
--locked Require Cargo.lock is up to date
5963
6064
This command manages Cargo's local set of installed binary crates. Only packages
6165
which have [[bin]] targets can be installed, and all binaries are installed into
@@ -89,9 +93,11 @@ The `--list` option will list all installed packages (and their versions).
8993
";
9094

9195
pub fn execute(options: Options, config: &Config) -> CliResult<Option<()>> {
92-
try!(config.configure_shell(options.flag_verbose,
93-
options.flag_quiet,
94-
&options.flag_color));
96+
try!(config.configure(options.flag_verbose,
97+
options.flag_quiet,
98+
&options.flag_color,
99+
options.flag_frozen,
100+
options.flag_locked));
95101

96102
let compile_opts = ops::CompileOptions {
97103
config: config,

src/bin/login.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ pub struct Options {
1313
flag_verbose: u32,
1414
flag_quiet: Option<bool>,
1515
flag_color: Option<String>,
16+
flag_frozen: bool,
17+
flag_locked: bool,
1618
}
1719

1820
pub const USAGE: &'static str = "
@@ -27,13 +29,17 @@ Options:
2729
-v, --verbose ... Use verbose output
2830
-q, --quiet No output printed to stdout
2931
--color WHEN Coloring: auto, always, never
32+
--frozen Require Cargo.lock and cache are up to date
33+
--locked Require Cargo.lock is up to date
3034
3135
";
3236

3337
pub fn execute(options: Options, config: &Config) -> CliResult<Option<()>> {
34-
try!(config.configure_shell(options.flag_verbose,
35-
options.flag_quiet,
36-
&options.flag_color));
38+
try!(config.configure(options.flag_verbose,
39+
options.flag_quiet,
40+
&options.flag_color,
41+
options.flag_frozen,
42+
options.flag_locked));
3743
let token = match options.arg_token.clone() {
3844
Some(token) => token,
3945
None => {

src/bin/metadata.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ pub struct Options {
1313
flag_no_deps: bool,
1414
flag_quiet: Option<bool>,
1515
flag_verbose: u32,
16+
flag_frozen: bool,
17+
flag_locked: bool,
1618
}
1719

1820
pub const USAGE: &'static str = "
@@ -34,12 +36,16 @@ Options:
3436
-v, --verbose ... Use verbose output
3537
-q, --quiet No output printed to stdout
3638
--color WHEN Coloring: auto, always, never
39+
--frozen Require Cargo.lock and cache are up to date
40+
--locked Require Cargo.lock is up to date
3741
";
3842

3943
pub fn execute(options: Options, config: &Config) -> CliResult<Option<ExportInfo>> {
40-
try!(config.configure_shell(options.flag_verbose,
41-
options.flag_quiet,
42-
&options.flag_color));
44+
try!(config.configure(options.flag_verbose,
45+
options.flag_quiet,
46+
&options.flag_color,
47+
options.flag_frozen,
48+
options.flag_locked));
4349
let manifest = try!(find_root_manifest_for_wd(options.flag_manifest_path, config.cwd()));
4450

4551
let options = OutputMetadataOptions {

0 commit comments

Comments
 (0)