diff --git a/src/cargo/core/dependency.rs b/src/cargo/core/dependency.rs index 61795936dc2..483ecef75bf 100644 --- a/src/cargo/core/dependency.rs +++ b/src/cargo/core/dependency.rs @@ -4,6 +4,7 @@ use semver::ReqParseError; use semver::VersionReq; use serde::ser; use serde::Serialize; +use std::path::PathBuf; use std::rc::Rc; use crate::core::{PackageId, SourceId, Summary}; @@ -61,6 +62,10 @@ struct SerializedDependency<'a> { /// The registry URL this dependency is from. /// If None, then it comes from the default registry (crates.io). registry: Option<&'a str>, + + /// The file system path for a local path dependency. + #[serde(skip_serializing_if = "Option::is_none")] + path: Option, } impl ser::Serialize for Dependency { @@ -80,6 +85,7 @@ impl ser::Serialize for Dependency { target: self.platform(), rename: self.explicit_name_in_toml().map(|s| s.as_str()), registry: registry_id.as_ref().map(|sid| sid.url().as_str()), + path: self.source_id().local_path(), } .serialize(s) } diff --git a/src/cargo/core/source/source_id.rs b/src/cargo/core/source/source_id.rs index 5e6322d9e9c..a1183f39670 100644 --- a/src/cargo/core/source/source_id.rs +++ b/src/cargo/core/source/source_id.rs @@ -9,7 +9,7 @@ use std::cmp::{self, Ordering}; use std::collections::HashSet; use std::fmt::{self, Formatter}; use std::hash::{self, Hash}; -use std::path::Path; +use std::path::{Path, PathBuf}; use std::ptr; use std::sync::Mutex; use url::Url; @@ -237,6 +237,15 @@ impl SourceId { self.inner.kind == SourceKind::Path } + /// Returns the local path if this is a path dependency. + pub fn local_path(self) -> Option { + if self.inner.kind != SourceKind::Path { + return None; + } + + Some(self.inner.url.to_file_path().unwrap()) + } + /// Returns `true` if this source is from a registry (either local or not). pub fn is_registry(self) -> bool { matches!( diff --git a/src/doc/man/cargo-metadata.md b/src/doc/man/cargo-metadata.md index 811aa094a59..33951fddd69 100644 --- a/src/doc/man/cargo-metadata.md +++ b/src/doc/man/cargo-metadata.md @@ -83,6 +83,10 @@ The output has the following format: null if not a target dependency. */ "target": "cfg(windows)", + /* The file system path for a local path dependency. + not present if not a path dependency. + */ + "path": "/path/to/dep", /* A string of the URL of the registry this dependency is from. If not specified or null, the dependency is from the default registry (crates.io). diff --git a/src/doc/man/generated_txt/cargo-metadata.txt b/src/doc/man/generated_txt/cargo-metadata.txt index 8a600cfca65..8dcd2227e81 100644 --- a/src/doc/man/generated_txt/cargo-metadata.txt +++ b/src/doc/man/generated_txt/cargo-metadata.txt @@ -78,6 +78,10 @@ OUTPUT FORMAT null if not a target dependency. */ "target": "cfg(windows)", + /* The file system path for a local path dependency. + not present if not a path dependency. + */ + "path": "/path/to/dep", /* A string of the URL of the registry this dependency is from. If not specified or null, the dependency is from the default registry (crates.io). diff --git a/src/doc/src/commands/cargo-metadata.md b/src/doc/src/commands/cargo-metadata.md index a95a27b9f34..b59e2638715 100644 --- a/src/doc/src/commands/cargo-metadata.md +++ b/src/doc/src/commands/cargo-metadata.md @@ -83,6 +83,10 @@ The output has the following format: null if not a target dependency. */ "target": "cfg(windows)", + /* The file system path for a local path dependency. + not present if not a path dependency. + */ + "path": "/path/to/dep", /* A string of the URL of the registry this dependency is from. If not specified or null, the dependency is from the default registry (crates.io). diff --git a/src/etc/man/cargo-metadata.1 b/src/etc/man/cargo-metadata.1 index 08655d3b5bf..2c20cc7880b 100644 --- a/src/etc/man/cargo-metadata.1 +++ b/src/etc/man/cargo-metadata.1 @@ -80,6 +80,10 @@ The output has the following format: null if not a target dependency. */ "target": "cfg(windows)", + /* The file system path for a local path dependency. + not present if not a path dependency. + */ + "path": "/path/to/dep", /* A string of the URL of the registry this dependency is from. If not specified or null, the dependency is from the default registry (crates.io). diff --git a/tests/testsuite/metadata.rs b/tests/testsuite/metadata.rs index 9d633f309a4..7896c2549c5 100644 --- a/tests/testsuite/metadata.rs +++ b/tests/testsuite/metadata.rs @@ -1986,6 +1986,7 @@ fn deps_with_bin_only() { "rename": null, "optional": false, "uses_default_features": true, + "path": "[..]/foo/bdep", "features": [], "target": null, "registry": null