Skip to content

Commit 3cac894

Browse files
committed
Auto merge of #3841 - matklad:encourage-explicit-version, r=alexcrichton
Encourage tools writers to explicitly pin metadata version We do support versioning of metadata, but let's encourage tool's writers to actually use it. They might not realize that this flag exists at all, or they can be too lazy (like myself :( ) to use it. We can also make this flag mandatory, but I think that's a little bit to far.
2 parents d3b8f9e + 624493c commit 3cac894

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

src/bin/metadata.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pub struct Options {
99
flag_color: Option<String>,
1010
flag_features: Vec<String>,
1111
flag_all_features: bool,
12-
flag_format_version: u32,
12+
flag_format_version: Option<u32>,
1313
flag_manifest_path: Option<String>,
1414
flag_no_default_features: bool,
1515
flag_no_deps: bool,
@@ -34,7 +34,7 @@ Options:
3434
--no-deps Output information only about the root package
3535
and don't fetch dependencies.
3636
--manifest-path PATH Path to the manifest
37-
--format-version VERSION Format version [default: 1]
37+
--format-version VERSION Format version
3838
Valid values: 1
3939
-v, --verbose ... Use verbose output (-vv very verbose/build.rs output)
4040
-q, --quiet No output printed to stdout
@@ -51,12 +51,17 @@ pub fn execute(options: Options, config: &Config) -> CliResult {
5151
options.flag_locked)?;
5252
let manifest = find_root_manifest_for_wd(options.flag_manifest_path, config.cwd())?;
5353

54+
if options.flag_format_version.is_none() {
55+
config.shell().warn("please specify `--format-version` flag explicitly to \
56+
avoid compatibility problems")?
57+
}
58+
5459
let options = OutputMetadataOptions {
5560
features: options.flag_features,
5661
all_features: options.flag_all_features,
5762
no_default_features: options.flag_no_default_features,
5863
no_deps: options.flag_no_deps,
59-
version: options.flag_format_version,
64+
version: options.flag_format_version.unwrap_or(1),
6065
};
6166

6267
let ws = Workspace::new(&manifest, config)?;

tests/metadata.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,21 @@ fn cargo_metadata_simple() {
5353
}"#));
5454
}
5555

56+
#[test]
57+
fn cargo_metadata_warns_on_implicit_version() {
58+
let p = project("foo")
59+
.file("src/foo.rs", "")
60+
.file("Cargo.toml", &basic_bin_manifest("foo"));
61+
p.build();
62+
63+
assert_that(p.cargo("metadata"),
64+
execs().with_stderr("\
65+
[WARNING] please specify `--format-version` flag explicitly to avoid compatibility problems"));
66+
67+
assert_that(p.cargo("metadata").arg("--format-version").arg("1"),
68+
execs().with_stderr(""));
69+
}
70+
5671
#[test]
5772
fn library_with_several_crate_types() {
5873
let p = project("foo")
@@ -520,8 +535,8 @@ fn cargo_metadata_with_invalid_manifest() {
520535
let p = project("foo")
521536
.file("Cargo.toml", "");
522537

523-
assert_that(p.cargo_process("metadata"), execs().with_status(101)
524-
.with_stderr("\
538+
assert_that(p.cargo_process("metadata").arg("--format-version").arg("1"),
539+
execs().with_status(101).with_stderr("\
525540
[ERROR] failed to parse manifest at `[..]`
526541
527542
Caused by:

0 commit comments

Comments
 (0)