Skip to content

Commit 329895f

Browse files
committed
Auto merge of #8994 - jonhoo:manifest-in-local-deps-meta, r=ehuss
metadata: Supply local path for path dependencies This is potentially a simpler way to address #7483 than #8988. /cc rust-lang/rustfmt#4247 Fixes #7483.
2 parents 3bb0968 + fd25f93 commit 329895f

File tree

7 files changed

+33
-1
lines changed

7 files changed

+33
-1
lines changed

src/cargo/core/dependency.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use semver::ReqParseError;
44
use semver::VersionReq;
55
use serde::ser;
66
use serde::Serialize;
7+
use std::path::PathBuf;
78
use std::rc::Rc;
89

910
use crate::core::{PackageId, SourceId, Summary};
@@ -61,6 +62,10 @@ struct SerializedDependency<'a> {
6162
/// The registry URL this dependency is from.
6263
/// If None, then it comes from the default registry (crates.io).
6364
registry: Option<&'a str>,
65+
66+
/// The file system path for a local path dependency.
67+
#[serde(skip_serializing_if = "Option::is_none")]
68+
path: Option<PathBuf>,
6469
}
6570

6671
impl ser::Serialize for Dependency {
@@ -80,6 +85,7 @@ impl ser::Serialize for Dependency {
8085
target: self.platform(),
8186
rename: self.explicit_name_in_toml().map(|s| s.as_str()),
8287
registry: registry_id.as_ref().map(|sid| sid.url().as_str()),
88+
path: self.source_id().local_path(),
8389
}
8490
.serialize(s)
8591
}

src/cargo/core/source/source_id.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::cmp::{self, Ordering};
99
use std::collections::HashSet;
1010
use std::fmt::{self, Formatter};
1111
use std::hash::{self, Hash};
12-
use std::path::Path;
12+
use std::path::{Path, PathBuf};
1313
use std::ptr;
1414
use std::sync::Mutex;
1515
use url::Url;
@@ -237,6 +237,15 @@ impl SourceId {
237237
self.inner.kind == SourceKind::Path
238238
}
239239

240+
/// Returns the local path if this is a path dependency.
241+
pub fn local_path(self) -> Option<PathBuf> {
242+
if self.inner.kind != SourceKind::Path {
243+
return None;
244+
}
245+
246+
Some(self.inner.url.to_file_path().unwrap())
247+
}
248+
240249
/// Returns `true` if this source is from a registry (either local or not).
241250
pub fn is_registry(self) -> bool {
242251
matches!(

src/doc/man/cargo-metadata.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ The output has the following format:
8383
null if not a target dependency.
8484
*/
8585
"target": "cfg(windows)",
86+
/* The file system path for a local path dependency.
87+
not present if not a path dependency.
88+
*/
89+
"path": "/path/to/dep",
8690
/* A string of the URL of the registry this dependency is from.
8791
If not specified or null, the dependency is from the default
8892
registry (crates.io).

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ OUTPUT FORMAT
7878
null if not a target dependency.
7979
*/
8080
"target": "cfg(windows)",
81+
/* The file system path for a local path dependency.
82+
not present if not a path dependency.
83+
*/
84+
"path": "/path/to/dep",
8185
/* A string of the URL of the registry this dependency is from.
8286
If not specified or null, the dependency is from the default
8387
registry (crates.io).

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ The output has the following format:
8383
null if not a target dependency.
8484
*/
8585
"target": "cfg(windows)",
86+
/* The file system path for a local path dependency.
87+
not present if not a path dependency.
88+
*/
89+
"path": "/path/to/dep",
8690
/* A string of the URL of the registry this dependency is from.
8791
If not specified or null, the dependency is from the default
8892
registry (crates.io).

src/etc/man/cargo-metadata.1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ The output has the following format:
8080
null if not a target dependency.
8181
*/
8282
"target": "cfg(windows)",
83+
/* The file system path for a local path dependency.
84+
not present if not a path dependency.
85+
*/
86+
"path": "/path/to/dep",
8387
/* A string of the URL of the registry this dependency is from.
8488
If not specified or null, the dependency is from the default
8589
registry (crates.io).

tests/testsuite/metadata.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1986,6 +1986,7 @@ fn deps_with_bin_only() {
19861986
"rename": null,
19871987
"optional": false,
19881988
"uses_default_features": true,
1989+
"path": "[..]/foo/bdep",
19891990
"features": [],
19901991
"target": null,
19911992
"registry": null

0 commit comments

Comments
 (0)