Skip to content

Commit c4a6528

Browse files
committed
Make cargo metadata --no-deps print all path deps
Including those outside the current workspace. This allows for finding all local code without having to recursively call Cargo, which is useful for rustfmt and other tools. Fixes: #7483
1 parent 134f614 commit c4a6528

File tree

6 files changed

+125
-17
lines changed

6 files changed

+125
-17
lines changed

src/cargo/ops/cargo_output_metadata.rs

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ use crate::core::{Dependency, Package, PackageId, Workspace};
66
use crate::ops::{self, Packages};
77
use crate::util::interning::InternedString;
88
use crate::util::CargoResult;
9+
use crate::Config;
910
use cargo_platform::Platform;
1011
use serde::Serialize;
11-
use std::collections::BTreeMap;
12+
use std::collections::{BTreeMap, BTreeSet};
1213
use std::path::PathBuf;
1314

1415
const VERSION: u32 = 1;
@@ -35,7 +36,10 @@ pub fn output_metadata(ws: &Workspace<'_>, opt: &OutputMetadataOptions) -> Cargo
3536
}
3637
let config = ws.config();
3738
let (packages, resolve) = if opt.no_deps {
38-
let packages = ws.members().map(|pkg| pkg.serialized(config)).collect();
39+
let packages = path_packages(ws)?
40+
.into_iter()
41+
.map(|pkg| pkg.serialized(config))
42+
.collect();
3943
(packages, None)
4044
} else {
4145
let (packages, resolve) = build_resolve_graph(ws, opt)?;
@@ -103,6 +107,42 @@ impl From<&Dependency> for DepKindInfo {
103107
}
104108
}
105109

110+
fn path_packages_r(
111+
package: &Package,
112+
config: &Config,
113+
found: &mut BTreeSet<Package>,
114+
) -> CargoResult<()> {
115+
if found.contains(package) {
116+
return Ok(());
117+
}
118+
found.insert(package.clone());
119+
120+
for dependency in package.dependencies() {
121+
let source_id = dependency.source_id();
122+
123+
if !source_id.is_path() {
124+
continue;
125+
}
126+
127+
if let Ok(mut path) = source_id.url().to_file_path() {
128+
path.push("Cargo.toml");
129+
path_packages_r(Workspace::new(&path, &config)?.current()?, config, found)?;
130+
}
131+
}
132+
133+
Ok(())
134+
}
135+
136+
fn path_packages(ws: &Workspace<'_>) -> CargoResult<BTreeSet<Package>> {
137+
let mut found = BTreeSet::new();
138+
139+
for package in ws.members() {
140+
path_packages_r(package, ws.config(), &mut found)?;
141+
}
142+
143+
Ok(found)
144+
}
145+
106146
/// Builds the resolve graph as it will be displayed to the user.
107147
fn build_resolve_graph(
108148
ws: &Workspace<'_>,

src/doc/man/cargo-metadata.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ The output has the following format:
2626
```javascript
2727
{
2828
/* Array of all packages in the workspace.
29-
It also includes all feature-enabled dependencies unless --no-deps is used.
29+
With --no-deps, includes all packages in local workspaces.
30+
Otherwise, all packages in current workspace, and all feature-enabled dependencies.
3031
*/
3132
"packages": [
3233
{
@@ -198,7 +199,7 @@ The output has the following format:
198199
"links": null,
199200
}
200201
],
201-
/* Array of members of the workspace.
202+
/* Array of members of the current workspace.
202203
Each entry is the Package ID for the package.
203204
*/
204205
"workspace_members": [
@@ -292,7 +293,7 @@ The output has the following format:
292293
{{#options}}
293294

294295
{{#option "`--no-deps`" }}
295-
Output information only about the workspace members and don't fetch
296+
Output information only about the members of local workspaces and don't fetch
296297
dependencies.
297298
{{/option}}
298299

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ OUTPUT FORMAT
2121

2222
{
2323
/* Array of all packages in the workspace.
24-
It also includes all feature-enabled dependencies unless --no-deps is used.
24+
With --no-deps, includes all packages in local workspaces.
25+
Otherwise, all packages in current workspace, and all feature-enabled dependencies.
2526
*/
2627
"packages": [
2728
{
@@ -193,7 +194,7 @@ OUTPUT FORMAT
193194
"links": null,
194195
}
195196
],
196-
/* Array of members of the workspace.
197+
/* Array of members of the current workspace.
197198
Each entry is the Package ID for the package.
198199
*/
199200
"workspace_members": [
@@ -282,8 +283,8 @@ OUTPUT FORMAT
282283
OPTIONS
283284
Output Options
284285
--no-deps
285-
Output information only about the workspace members and don't fetch
286-
dependencies.
286+
Output information only about the members of local workspaces and
287+
don't fetch dependencies.
287288

288289
--format-version version
289290
Specify the version of the output format to use. Currently 1 is the

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ The output has the following format:
2626
```javascript
2727
{
2828
/* Array of all packages in the workspace.
29-
It also includes all feature-enabled dependencies unless --no-deps is used.
29+
With --no-deps, includes all packages in local workspaces.
30+
Otherwise, all packages in current workspace, and all feature-enabled dependencies.
3031
*/
3132
"packages": [
3233
{
@@ -198,7 +199,7 @@ The output has the following format:
198199
"links": null,
199200
}
200201
],
201-
/* Array of members of the workspace.
202+
/* Array of members of the current workspace.
202203
Each entry is the Package ID for the package.
203204
*/
204205
"workspace_members": [
@@ -292,7 +293,7 @@ The output has the following format:
292293
<dl>
293294

294295
<dt class="option-term" id="option-cargo-metadata---no-deps"><a class="option-anchor" href="#option-cargo-metadata---no-deps"></a><code>--no-deps</code></dt>
295-
<dd class="option-desc">Output information only about the workspace members and don't fetch
296+
<dd class="option-desc">Output information only about the members of local workspaces and don't fetch
296297
dependencies.</dd>
297298
298299

src/etc/man/cargo-metadata.1

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ The output has the following format:
2323
.nf
2424
{
2525
/* Array of all packages in the workspace.
26-
It also includes all feature\-enabled dependencies unless \-\-no\-deps is used.
26+
With \-\-no\-deps, includes all packages in local workspaces.
27+
Otherwise, all packages in current workspace, and all feature\-enabled dependencies.
2728
*/
2829
"packages": [
2930
{
@@ -195,7 +196,7 @@ The output has the following format:
195196
"links": null,
196197
}
197198
],
198-
/* Array of members of the workspace.
199+
/* Array of members of the current workspace.
199200
Each entry is the Package ID for the package.
200201
*/
201202
"workspace_members": [
@@ -287,7 +288,7 @@ The output has the following format:
287288
.sp
288289
\fB\-\-no\-deps\fR
289290
.RS 4
290-
Output information only about the workspace members and don't fetch
291+
Output information only about the members of local workspaces and don't fetch
291292
dependencies.
292293
.RE
293294
.sp

tests/testsuite/metadata.rs

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -909,12 +909,26 @@ fn workspace_metadata_no_deps() {
909909
r#"
910910
[workspace]
911911
members = ["bar", "baz"]
912+
exclude = ["qux"]
912913
"#,
913914
)
914915
.file("bar/Cargo.toml", &basic_lib_manifest("bar"))
915916
.file("bar/src/lib.rs", "")
916-
.file("baz/Cargo.toml", &basic_lib_manifest("baz"))
917+
.file(
918+
"baz/Cargo.toml",
919+
r#"
920+
[package]
921+
name = "baz"
922+
version = "0.5.0"
923+
authors = ["[email protected]"]
924+
925+
[dependencies]
926+
qux = { path = "../qux" }
927+
"#,
928+
)
917929
.file("baz/src/lib.rs", "")
930+
.file("qux/Cargo.toml", &basic_lib_manifest("qux"))
931+
.file("qux/src/lib.rs", "")
918932
.build();
919933

920934
p.cargo("metadata --no-deps")
@@ -973,7 +987,20 @@ fn workspace_metadata_no_deps() {
973987
"id": "baz[..]",
974988
"keywords": [],
975989
"source": null,
976-
"dependencies": [],
990+
"dependencies": [
991+
{
992+
"features": [],
993+
"kind": null,
994+
"name": "qux",
995+
"optional": false,
996+
"registry": null,
997+
"rename": null,
998+
"req": "*",
999+
"source": null,
1000+
"target": null,
1001+
"uses_default_features": true
1002+
}
1003+
],
9771004
"license": null,
9781005
"license_file": null,
9791006
"links": null,
@@ -995,6 +1022,43 @@ fn workspace_metadata_no_deps() {
9951022
"manifest_path": "[..]baz/Cargo.toml",
9961023
"metadata": null,
9971024
"publish": null
1025+
},
1026+
{
1027+
"authors": [
1028+
1029+
],
1030+
"categories": [],
1031+
"name": "qux",
1032+
"readme": null,
1033+
"repository": null,
1034+
"homepage": null,
1035+
"documentation": null,
1036+
"version": "0.5.0",
1037+
"id": "qux[..]",
1038+
"keywords": [],
1039+
"source": null,
1040+
"dependencies": [],
1041+
"license": null,
1042+
"license_file": null,
1043+
"links": null,
1044+
"description": null,
1045+
"edition": "2015",
1046+
"targets": [
1047+
{
1048+
"kind": [ "lib" ],
1049+
"crate_types": ["lib"],
1050+
"doc": true,
1051+
"doctest": true,
1052+
"test": true,
1053+
"edition": "2015",
1054+
"name": "qux",
1055+
"src_path": "[..]qux/src/lib.rs"
1056+
}
1057+
],
1058+
"features": {},
1059+
"manifest_path": "[..]qux/Cargo.toml",
1060+
"metadata": null,
1061+
"publish": null
9981062
}
9991063
],
10001064
"workspace_members": ["bar 0.5.0 (path+file:[..]bar)", "baz 0.5.0 (path+file:[..]baz)"],

0 commit comments

Comments
 (0)