From a1ea74a84dd02f2d59d00b0577dd08e82080d5fc Mon Sep 17 00:00:00 2001 From: Jon Gjengset Date: Wed, 16 Dec 2020 16:27:40 -0800 Subject: [PATCH 1/2] Also emit source information for paths Previously, the location for path-based (i.e., local) packages was redacted from metadata output. This was done way back when so that `Cargo.lock` would not include path sources: https://github.com/rust-lang/cargo/issues/7483#issuecomment-539258083 This PR makes this redaction optional so that `cargo metadata` and friends _will_ output the source location, which is useful for tools like `rustfmt` which need to know the paths to local packages (see #7483 and rust-lang/rustfmt#4599). Interestingly enough, no tests fail even though `disable_path_serialization` is never currently called. I don't know if that's because there's no test that cover where redaction is needed, or because redaction is simply no longer needed (presumably because lockfile generation now uses custom logic). If it is the latter, this PR can be simplified to not support opting into redaction. Fixes #7483 --- src/cargo/core/source/source_id.rs | 20 +++++++++++- tests/testsuite/alt_registry.rs | 6 ++-- tests/testsuite/features_namespaced.rs | 2 +- tests/testsuite/git.rs | 2 +- tests/testsuite/metadata.rs | 42 +++++++++++++------------- tests/testsuite/read_manifest.rs | 2 +- tests/testsuite/update.rs | 2 +- 7 files changed, 47 insertions(+), 29 deletions(-) diff --git a/src/cargo/core/source/source_id.rs b/src/cargo/core/source/source_id.rs index 5e6322d9e9c..3e446272458 100644 --- a/src/cargo/core/source/source_id.rs +++ b/src/cargo/core/source/source_id.rs @@ -5,6 +5,7 @@ use crate::util::{CanonicalUrl, CargoResult, Config, IntoUrl}; use log::trace; use serde::de; use serde::ser; +use std::cell::Cell; use std::cmp::{self, Ordering}; use std::collections::HashSet; use std::fmt::{self, Formatter}; @@ -437,12 +438,29 @@ impl Ord for SourceId { } } +thread_local! { + static SERIALIZE_STRIP_PATHS: Cell = Cell::new(false); +} + +#[must_use] +pub fn disable_path_serialization() -> impl Drop { + // Ensure that we reset the setting eventually. + struct DropGuard; + impl Drop for DropGuard { + fn drop(&mut self) { + SERIALIZE_STRIP_PATHS.with(|ssp| ssp.set(false)); + } + } + SERIALIZE_STRIP_PATHS.with(|ssp| ssp.set(true)); + DropGuard +} + impl ser::Serialize for SourceId { fn serialize(&self, s: S) -> Result where S: ser::Serializer, { - if self.is_path() { + if self.is_path() && SERIALIZE_STRIP_PATHS.with(|ssp| ssp.get()) { None::.serialize(s) } else { s.collect_str(&self.as_url()) diff --git a/tests/testsuite/alt_registry.rs b/tests/testsuite/alt_registry.rs index 748d5dcaaa5..5fd7958a7d2 100644 --- a/tests/testsuite/alt_registry.rs +++ b/tests/testsuite/alt_registry.rs @@ -783,7 +783,7 @@ fn alt_reg_metadata() { "license": null, "license_file": null, "description": null, - "source": null, + "source": "path+file://[..]/foo", "dependencies": [ { "name": "altdep", @@ -936,7 +936,7 @@ fn alt_reg_metadata() { "license": null, "license_file": null, "description": null, - "source": null, + "source": "path+file://[..]/foo", "dependencies": [ { "name": "altdep", @@ -1141,7 +1141,7 @@ fn unknown_registry() { "license": null, "license_file": null, "description": null, - "source": null, + "source": "path+file://[..]/foo", "dependencies": [ { "name": "bar", diff --git a/tests/testsuite/features_namespaced.rs b/tests/testsuite/features_namespaced.rs index 55078f1c28e..4d7397515e3 100644 --- a/tests/testsuite/features_namespaced.rs +++ b/tests/testsuite/features_namespaced.rs @@ -741,7 +741,7 @@ fn json_exposed() { "description": null, "homepage": null, "documentation": null, - "source": null, + "source": "path+file://[..]/foo", "dependencies": "{...}", "targets": "{...}", "features": { diff --git a/tests/testsuite/git.rs b/tests/testsuite/git.rs index 548d7264c2a..aadc5e93327 100644 --- a/tests/testsuite/git.rs +++ b/tests/testsuite/git.rs @@ -3106,7 +3106,7 @@ fn metadata_master_consistency() { "license": null, "license_file": null, "description": null, - "source": null, + "source": "path+file://[..]/foo", "dependencies": [ { "name": "bar", diff --git a/tests/testsuite/metadata.rs b/tests/testsuite/metadata.rs index 3c4ba6bf59b..c65ae5e80f5 100644 --- a/tests/testsuite/metadata.rs +++ b/tests/testsuite/metadata.rs @@ -24,7 +24,7 @@ fn cargo_metadata_simple() { "version": "0.5.0", "id": "foo[..]", "keywords": [], - "source": null, + "source": "path+file://[..]/foo", "dependencies": [], "edition": "2015", "license": null, @@ -125,7 +125,7 @@ crate-type = ["lib", "staticlib"] "version": "0.5.0", "id": "foo[..]", "keywords": [], - "source": null, + "source": "path+file://[..]/foo", "dependencies": [], "edition": "2015", "license": null, @@ -212,7 +212,7 @@ optional_feat = [] "version": "0.5.0", "id": "foo[..]", "keywords": [], - "source": null, + "source": "path+file://[..]/foo", "dependencies": [], "edition": "2015", "license": null, @@ -440,7 +440,7 @@ fn cargo_metadata_with_deps_and_version() { "repository": null, "homepage": null, "documentation": null, - "source": null, + "source": "path+file://[..]/foo", "targets": [ { "crate_types": [ @@ -616,7 +616,7 @@ name = "ex" "links": null, "description": null, "edition": "2015", - "source": null, + "source": "path+file://[..]/foo", "dependencies": [], "targets": [ { @@ -709,7 +709,7 @@ crate-type = ["rlib", "dylib"] "links": null, "description": null, "edition": "2015", - "source": null, + "source": "path+file://[..]/foo", "dependencies": [], "targets": [ { @@ -804,7 +804,7 @@ fn workspace_metadata() { "homepage": null, "documentation": null, "keywords": [], - "source": null, + "source": "path+file://[..]/bar", "dependencies": [], "license": null, "license_file": null, @@ -841,7 +841,7 @@ fn workspace_metadata() { "version": "0.5.0", "id": "baz[..]", "keywords": [], - "source": null, + "source": "path+file://[..]/baz", "dependencies": [], "license": null, "license_file": null, @@ -933,7 +933,7 @@ fn workspace_metadata_no_deps() { "version": "0.5.0", "id": "bar[..]", "keywords": [], - "source": null, + "source": "path+file://[..]/bar", "dependencies": [], "license": null, "license_file": null, @@ -970,7 +970,7 @@ fn workspace_metadata_no_deps() { "version": "0.5.0", "id": "baz[..]", "keywords": [], - "source": null, + "source": "path+file://[..]/baz", "dependencies": [], "license": null, "license_file": null, @@ -1032,7 +1032,7 @@ const MANIFEST_OUTPUT: &str = r#" "name":"foo", "version":"0.5.0", "id":"foo[..]0.5.0[..](path+file://[..]/foo)", - "source":null, + "source":"path+file://[..]/foo", "dependencies":[], "keywords": [], "license": null, @@ -1222,7 +1222,7 @@ fn package_metadata() { "version": "0.1.0", "id": "foo[..]", "keywords": ["database"], - "source": null, + "source": "path+file://[..]/foo", "dependencies": [], "edition": "2015", "license": null, @@ -1299,7 +1299,7 @@ fn package_publish() { "version": "0.1.0", "id": "foo[..]", "keywords": ["database"], - "source": null, + "source": "path+file://[..]/foo", "dependencies": [], "edition": "2015", "license": null, @@ -1382,7 +1382,7 @@ fn cargo_metadata_path_to_cargo_toml_project() { "repository": null, "homepage": null, "documentation": null, - "source": null, + "source": "path+file://[..]/bar-0.5.0", "targets": [ { "crate_types": [ @@ -1468,7 +1468,7 @@ fn package_edition_2018() { "repository": null, "homepage": null, "documentation": null, - "source": null, + "source": "path+file://[..]/foo", "targets": [ { "crate_types": [ @@ -1558,7 +1558,7 @@ fn target_edition_2018() { "repository": null, "homepage": null, "documentation": null, - "source": null, + "source": "path+file://[..]/foo", "targets": [ { "crate_types": [ @@ -1766,7 +1766,7 @@ fn rename_dependency() { "repository": null, "homepage": null, "documentation": null, - "source": null, + "source": "path+file://[..]/foo", "targets": [ { "crate_types": [ @@ -1886,7 +1886,7 @@ fn metadata_links() { "repository": null, "homepage": null, "documentation": null, - "source": null, + "source": "path+file://[..]/foo", "targets": [ { "crate_types": [ @@ -1974,11 +1974,11 @@ fn deps_with_bin_only() { "license": null, "license_file": null, "description": null, - "source": null, + "source": "path+file://[..]/foo", "dependencies": [ { "name": "bdep", - "source": null, + "source": "path+file://[..]/bdep", "req": "*", "kind": null, "rename": null, @@ -2258,7 +2258,7 @@ fn filter_platform() { "license": null, "license_file": null, "description": null, - "source": null, + "source": "path+file://[..]/foo", "dependencies": [ { "name": "normal-dep", diff --git a/tests/testsuite/read_manifest.rs b/tests/testsuite/read_manifest.rs index 24d855eb029..5686d513703 100644 --- a/tests/testsuite/read_manifest.rs +++ b/tests/testsuite/read_manifest.rs @@ -23,7 +23,7 @@ fn manifest_output(readme_value: &str) -> String { "links": null, "description": null, "edition": "2015", - "source":null, + "source": "path+file://[..]/foo", "dependencies":[], "targets":[{{ "kind":["bin"], diff --git a/tests/testsuite/update.rs b/tests/testsuite/update.rs index 93f6eb6846c..e1c1d0891fb 100644 --- a/tests/testsuite/update.rs +++ b/tests/testsuite/update.rs @@ -463,7 +463,7 @@ fn update_precise_first_run() { "name": "bar", "readme": null, "repository": null, - "source": null, + "source": "path+file://[..]/foo", "targets": [ { "crate_types": [ From ad3cd3ac2522341fa4863eeba801f2c7c1ebbe8a Mon Sep 17 00:00:00 2001 From: Jon Gjengset Date: Wed, 16 Dec 2020 16:43:02 -0800 Subject: [PATCH 2/2] Don't warn for disable_path_serialization --- src/cargo/core/source/source_id.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/cargo/core/source/source_id.rs b/src/cargo/core/source/source_id.rs index 3e446272458..dae09cb6ee7 100644 --- a/src/cargo/core/source/source_id.rs +++ b/src/cargo/core/source/source_id.rs @@ -443,6 +443,7 @@ thread_local! { } #[must_use] +#[allow(dead_code)] pub fn disable_path_serialization() -> impl Drop { // Ensure that we reset the setting eventually. struct DropGuard;