Skip to content

Commit

Permalink
Auto merge of #8994 - jonhoo:manifest-in-local-deps-meta, r=ehuss
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bors committed Jan 6, 2021
2 parents 3bb0968 + fd25f93 commit 329895f
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/cargo/core/dependency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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<PathBuf>,
}

impl ser::Serialize for Dependency {
Expand All @@ -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)
}
Expand Down
11 changes: 10 additions & 1 deletion src/cargo/core/source/source_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<PathBuf> {
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!(
Expand Down
4 changes: 4 additions & 0 deletions src/doc/man/cargo-metadata.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
4 changes: 4 additions & 0 deletions src/doc/man/generated_txt/cargo-metadata.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
4 changes: 4 additions & 0 deletions src/doc/src/commands/cargo-metadata.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
4 changes: 4 additions & 0 deletions src/etc/man/cargo-metadata.1
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
1 change: 1 addition & 0 deletions tests/testsuite/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 329895f

Please sign in to comment.