diff --git a/src/cargo/core/package.rs b/src/cargo/core/package.rs index 6f160db5334..a1fe7823787 100644 --- a/src/cargo/core/package.rs +++ b/src/cargo/core/package.rs @@ -102,6 +102,7 @@ pub struct SerializedPackage { links: Option, #[serde(skip_serializing_if = "Option::is_none")] metabuild: Option>, + default_run: Option, } impl Package { @@ -267,6 +268,7 @@ impl Package { links: self.manifest().links().map(|s| s.to_owned()), metabuild: self.manifest().metabuild().cloned(), publish: self.publish().as_ref().cloned(), + default_run: self.manifest().default_run().map(|s| s.to_owned()), } } } diff --git a/src/doc/man/cargo-metadata.md b/src/doc/man/cargo-metadata.md index 33951fddd69..3ef6e8d6884 100644 --- a/src/doc/man/cargo-metadata.md +++ b/src/doc/man/cargo-metadata.md @@ -180,6 +180,8 @@ The output has the following format: "categories": [ "command-line-utilities" ], + /* Optional string that is the default binary picked by cargo run. */ + "default_run": null, /* Array of keywords from the manifest. */ "keywords": [ "cli" diff --git a/src/doc/man/generated_txt/cargo-metadata.txt b/src/doc/man/generated_txt/cargo-metadata.txt index 8dcd2227e81..97a1940f660 100644 --- a/src/doc/man/generated_txt/cargo-metadata.txt +++ b/src/doc/man/generated_txt/cargo-metadata.txt @@ -175,6 +175,8 @@ OUTPUT FORMAT "categories": [ "command-line-utilities" ], + /* Optional string that is the default binary picked by cargo run. */ + "default_run": null, /* Array of keywords from the manifest. */ "keywords": [ "cli" diff --git a/src/doc/src/commands/cargo-metadata.md b/src/doc/src/commands/cargo-metadata.md index b59e2638715..7d856a49aa7 100644 --- a/src/doc/src/commands/cargo-metadata.md +++ b/src/doc/src/commands/cargo-metadata.md @@ -180,6 +180,8 @@ The output has the following format: "categories": [ "command-line-utilities" ], + /* Optional string that is the default binary picked by cargo run. */ + "default_run": null, /* Array of keywords from the manifest. */ "keywords": [ "cli" diff --git a/src/etc/man/cargo-metadata.1 b/src/etc/man/cargo-metadata.1 index 2c20cc7880b..dfe79583c45 100644 --- a/src/etc/man/cargo-metadata.1 +++ b/src/etc/man/cargo-metadata.1 @@ -177,6 +177,8 @@ The output has the following format: "categories": [ "command\-line\-utilities" ], + /* Optional string that is the default binary picked by cargo run. */ + "default_run": null, /* Array of keywords from the manifest. */ "keywords": [ "cli" diff --git a/tests/testsuite/alt_registry.rs b/tests/testsuite/alt_registry.rs index 336e978ffd8..dd08e3a517c 100644 --- a/tests/testsuite/alt_registry.rs +++ b/tests/testsuite/alt_registry.rs @@ -829,6 +829,7 @@ fn alt_reg_metadata() { "publish": null, "authors": [], "categories": [], + "default_run": null, "keywords": [], "readme": null, "repository": null, @@ -885,6 +886,7 @@ fn alt_reg_metadata() { "publish": null, "authors": [], "categories": [], + "default_run": null, "keywords": [], "readme": null, "repository": null, @@ -909,6 +911,7 @@ fn alt_reg_metadata() { "publish": null, "authors": [], "categories": [], + "default_run": null, "keywords": [], "readme": null, "repository": null, @@ -933,6 +936,7 @@ fn alt_reg_metadata() { "publish": null, "authors": [], "categories": [], + "default_run": null, "keywords": [], "readme": null, "repository": null, @@ -982,6 +986,7 @@ fn alt_reg_metadata() { "publish": null, "authors": [], "categories": [], + "default_run": null, "keywords": [], "readme": null, "repository": null, @@ -1019,6 +1024,7 @@ fn alt_reg_metadata() { "publish": null, "authors": [], "categories": [], + "default_run": null, "keywords": [], "readme": null, "repository": null, @@ -1115,6 +1121,7 @@ fn unknown_registry() { "publish": null, "authors": [], "categories": [], + "default_run": null, "keywords": [], "readme": null, "repository": null, @@ -1139,6 +1146,7 @@ fn unknown_registry() { "publish": null, "authors": [], "categories": [], + "default_run": null, "keywords": [], "readme": null, "repository": null, @@ -1176,6 +1184,7 @@ fn unknown_registry() { "publish": null, "authors": [], "categories": [], + "default_run": null, "keywords": [], "readme": null, "repository": null, diff --git a/tests/testsuite/features_namespaced.rs b/tests/testsuite/features_namespaced.rs index 9922674b3e7..0c3022c8dd3 100644 --- a/tests/testsuite/features_namespaced.rs +++ b/tests/testsuite/features_namespaced.rs @@ -766,6 +766,7 @@ fn json_exposed() { "publish": null, "authors": [], "categories": [], + "default_run": null, "keywords": [], "readme": null, "repository": null, diff --git a/tests/testsuite/git.rs b/tests/testsuite/git.rs index bd3718e7acd..46830457fc0 100644 --- a/tests/testsuite/git.rs +++ b/tests/testsuite/git.rs @@ -3081,6 +3081,7 @@ fn metadata_master_consistency() { "publish": null, "authors": [], "categories": [], + "default_run": null, "keywords": [], "readme": null, "repository": null, @@ -3118,6 +3119,7 @@ fn metadata_master_consistency() { "publish": null, "authors": [], "categories": [], + "default_run": null, "keywords": [], "readme": null, "repository": null, diff --git a/tests/testsuite/metadata.rs b/tests/testsuite/metadata.rs index 87efa67fa96..a6545582e58 100644 --- a/tests/testsuite/metadata.rs +++ b/tests/testsuite/metadata.rs @@ -22,6 +22,7 @@ fn cargo_metadata_simple() { "wycats@example.com" ], "categories": [], + "default_run": null, "name": "foo", "version": "0.5.0", "id": "foo[..]", @@ -119,6 +120,7 @@ crate-type = ["lib", "staticlib"] { "authors": [], "categories": [], + "default_run": null, "name": "foo", "readme": null, "repository": null, @@ -206,6 +208,7 @@ optional_feat = [] { "authors": [], "categories": [], + "default_run": null, "name": "foo", "readme": null, "repository": null, @@ -309,6 +312,7 @@ fn cargo_metadata_with_deps_and_version() { { "authors": [], "categories": [], + "default_run": null, "dependencies": [ { "features": [], @@ -361,6 +365,7 @@ fn cargo_metadata_with_deps_and_version() { { "authors": [], "categories": [], + "default_run": null, "dependencies": [], "description": null, "edition": "2015", @@ -400,6 +405,7 @@ fn cargo_metadata_with_deps_and_version() { { "authors": [], "categories": [], + "default_run": null, "dependencies": [ { "features": [], @@ -464,6 +470,7 @@ fn cargo_metadata_with_deps_and_version() { { "authors": [], "categories": [], + "default_run": null, "dependencies": [], "description": null, "edition": "2015", @@ -605,6 +612,7 @@ name = "ex" { "authors": [], "categories": [], + "default_run": null, "name": "foo", "readme": null, "repository": null, @@ -698,6 +706,7 @@ crate-type = ["rlib", "dylib"] { "authors": [], "categories": [], + "default_run": null, "name": "foo", "readme": null, "repository": null, @@ -798,6 +807,7 @@ fn workspace_metadata() { "wycats@example.com" ], "categories": [], + "default_run": null, "name": "bar", "version": "0.5.0", "id": "bar[..]", @@ -835,6 +845,7 @@ fn workspace_metadata() { "wycats@example.com" ], "categories": [], + "default_run": null, "name": "baz", "readme": null, "repository": null, @@ -927,6 +938,7 @@ fn workspace_metadata_no_deps() { "wycats@example.com" ], "categories": [], + "default_run": null, "name": "bar", "readme": null, "repository": null, @@ -964,6 +976,7 @@ fn workspace_metadata_no_deps() { "wycats@example.com" ], "categories": [], + "default_run": null, "name": "baz", "readme": null, "repository": null, @@ -1031,6 +1044,7 @@ const MANIFEST_OUTPUT: &str = r#" "wycats@example.com" ], "categories": [], + "default_run": null, "name":"foo", "version":"0.5.0", "id":"foo[..]0.5.0[..](path+file://[..]/foo)", @@ -1216,6 +1230,7 @@ fn package_metadata() { { "authors": ["wycats@example.com"], "categories": ["database"], + "default_run": null, "name": "foo", "readme": "README.md", "repository": "https://github.com/rust-lang/cargo", @@ -1293,6 +1308,7 @@ fn package_publish() { { "authors": ["wycats@example.com"], "categories": ["database"], + "default_run": null, "name": "foo", "readme": "README.md", "repository": "https://github.com/rust-lang/cargo", @@ -1367,6 +1383,7 @@ fn cargo_metadata_path_to_cargo_toml_project() { "wycats@example.com" ], "categories": [], + "default_run": null, "dependencies": [], "description": null, "edition": "2015", @@ -1453,6 +1470,7 @@ fn package_edition_2018() { "wycats@example.com" ], "categories": [], + "default_run": null, "dependencies": [], "description": null, "edition": "2018", @@ -1514,6 +1532,126 @@ fn package_edition_2018() { .run(); } +#[cargo_test] +fn package_default_run() { + let p = project() + .file("src/lib.rs", "") + .file("src/bin/a.rs", r#"fn main() { println!("hello A"); }"#) + .file("src/bin/b.rs", r#"fn main() { println!("hello B"); }"#) + .file( + "Cargo.toml", + r#" + [project] + name = "foo" + version = "0.1.0" + authors = ["wycats@example.com"] + edition = "2018" + default-run = "a" + "#, + ) + .build(); + p.cargo("metadata") + .with_json( + r#" + { + "packages": [ + { + "authors": [ + "wycats@example.com" + ], + "categories": [], + "default_run": "a", + "dependencies": [], + "description": null, + "edition": "2018", + "features": {}, + "id": "foo 0.1.0 (path+file:[..])", + "keywords": [], + "license": null, + "license_file": null, + "links": null, + "manifest_path": "[..]Cargo.toml", + "metadata": null, + "publish": null, + "name": "foo", + "readme": null, + "repository": null, + "homepage": null, + "documentation": null, + "source": null, + "targets": [ + { + "crate_types": [ + "lib" + ], + "doc": true, + "doctest": true, + "test": true, + "edition": "2018", + "kind": [ + "lib" + ], + "name": "foo", + "src_path": "[..]src/lib.rs" + }, + { + "crate_types": [ + "bin" + ], + "doc": true, + "doctest": false, + "test": true, + "edition": "2018", + "kind": [ + "bin" + ], + "name": "a", + "src_path": "[..]src/bin/a.rs", + "test": true + }, + { + "crate_types": [ + "bin" + ], + "doc": true, + "doctest": false, + "test": true, + "edition": "2018", + "kind": [ + "bin" + ], + "name": "b", + "src_path": "[..]src/bin/b.rs", + "test": true + } + ], + "version": "0.1.0" + } + ], + "resolve": { + "nodes": [ + { + "dependencies": [], + "deps": [], + "features": [], + "id": "foo 0.1.0 (path+file:[..])" + } + ], + "root": "foo 0.1.0 (path+file:[..])" + }, + "target_directory": "[..]", + "version": 1, + "workspace_members": [ + "foo 0.1.0 (path+file:[..])" + ], + "workspace_root": "[..]", + "metadata": null + } + "#, + ) + .run(); +} + #[cargo_test] fn target_edition_2018() { let p = project() @@ -1543,6 +1681,7 @@ fn target_edition_2018() { "wycats@example.com" ], "categories": [], + "default_run": null, "dependencies": [], "description": null, "edition": "2015", @@ -1648,6 +1787,7 @@ fn rename_dependency() { { "authors": [], "categories": [], + "default_run": null, "dependencies": [], "description": null, "edition": "2015", @@ -1687,6 +1827,7 @@ fn rename_dependency() { { "authors": [], "categories": [], + "default_run": null, "dependencies": [], "description": null, "edition": "2015", @@ -1726,6 +1867,7 @@ fn rename_dependency() { { "authors": [], "categories": [], + "default_run": null, "dependencies": [ { "features": [], @@ -1871,6 +2013,7 @@ fn metadata_links() { { "authors": [], "categories": [], + "default_run": null, "dependencies": [], "description": null, "edition": "2015", @@ -2014,6 +2157,7 @@ fn deps_with_bin_only() { "publish": null, "authors": [], "categories": [], + "default_run": null, "keywords": [], "readme": null, "repository": null, @@ -2117,6 +2261,7 @@ fn filter_platform() { "publish": null, "authors": [], "categories": [], + "default_run": null, "keywords": [], "readme": null, "repository": null, @@ -2159,6 +2304,7 @@ fn filter_platform() { "publish": null, "authors": [], "categories": [], + "default_run": null, "keywords": [], "readme": null, "repository": null, @@ -2201,6 +2347,7 @@ fn filter_platform() { "publish": null, "authors": [], "categories": [], + "default_run": null, "keywords": [], "readme": null, "repository": null, @@ -2243,6 +2390,7 @@ fn filter_platform() { "publish": null, "authors": [], "categories": [], + "default_run": null, "keywords": [], "readme": null, "repository": null, @@ -2348,6 +2496,7 @@ fn filter_platform() { "publish": null, "authors": [], "categories": [], + "default_run": null, "keywords": [], "readme": null, "repository": null, diff --git a/tests/testsuite/read_manifest.rs b/tests/testsuite/read_manifest.rs index 24d855eb029..28bf6eed2a7 100644 --- a/tests/testsuite/read_manifest.rs +++ b/tests/testsuite/read_manifest.rs @@ -10,6 +10,7 @@ fn manifest_output(readme_value: &str) -> String { "wycats@example.com" ], "categories": [], + "default_run": null, "name":"foo", "readme": {}, "homepage": null, diff --git a/tests/testsuite/update.rs b/tests/testsuite/update.rs index 8a4d589989f..33ca18c38b1 100644 --- a/tests/testsuite/update.rs +++ b/tests/testsuite/update.rs @@ -433,6 +433,7 @@ fn update_precise_first_run() { { "authors": [], "categories": [], + "default_run": null, "dependencies": [ { "features": [], @@ -485,6 +486,7 @@ fn update_precise_first_run() { { "authors": [], "categories": [], + "default_run": null, "dependencies": [], "description": null, "documentation": null,