Skip to content

Commit a1ea74a

Browse files
author
Jon Gjengset
committed
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: rust-lang#7483 (comment) 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 rust-lang#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 rust-lang#7483
1 parent fde8ee3 commit a1ea74a

File tree

7 files changed

+47
-29
lines changed

7 files changed

+47
-29
lines changed

src/cargo/core/source/source_id.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::util::{CanonicalUrl, CargoResult, Config, IntoUrl};
55
use log::trace;
66
use serde::de;
77
use serde::ser;
8+
use std::cell::Cell;
89
use std::cmp::{self, Ordering};
910
use std::collections::HashSet;
1011
use std::fmt::{self, Formatter};
@@ -437,12 +438,29 @@ impl Ord for SourceId {
437438
}
438439
}
439440

441+
thread_local! {
442+
static SERIALIZE_STRIP_PATHS: Cell<bool> = Cell::new(false);
443+
}
444+
445+
#[must_use]
446+
pub fn disable_path_serialization() -> impl Drop {
447+
// Ensure that we reset the setting eventually.
448+
struct DropGuard;
449+
impl Drop for DropGuard {
450+
fn drop(&mut self) {
451+
SERIALIZE_STRIP_PATHS.with(|ssp| ssp.set(false));
452+
}
453+
}
454+
SERIALIZE_STRIP_PATHS.with(|ssp| ssp.set(true));
455+
DropGuard
456+
}
457+
440458
impl ser::Serialize for SourceId {
441459
fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
442460
where
443461
S: ser::Serializer,
444462
{
445-
if self.is_path() {
463+
if self.is_path() && SERIALIZE_STRIP_PATHS.with(|ssp| ssp.get()) {
446464
None::<String>.serialize(s)
447465
} else {
448466
s.collect_str(&self.as_url())

tests/testsuite/alt_registry.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,7 @@ fn alt_reg_metadata() {
783783
"license": null,
784784
"license_file": null,
785785
"description": null,
786-
"source": null,
786+
"source": "path+file://[..]/foo",
787787
"dependencies": [
788788
{
789789
"name": "altdep",
@@ -936,7 +936,7 @@ fn alt_reg_metadata() {
936936
"license": null,
937937
"license_file": null,
938938
"description": null,
939-
"source": null,
939+
"source": "path+file://[..]/foo",
940940
"dependencies": [
941941
{
942942
"name": "altdep",
@@ -1141,7 +1141,7 @@ fn unknown_registry() {
11411141
"license": null,
11421142
"license_file": null,
11431143
"description": null,
1144-
"source": null,
1144+
"source": "path+file://[..]/foo",
11451145
"dependencies": [
11461146
{
11471147
"name": "bar",

tests/testsuite/features_namespaced.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -741,7 +741,7 @@ fn json_exposed() {
741741
"description": null,
742742
"homepage": null,
743743
"documentation": null,
744-
"source": null,
744+
"source": "path+file://[..]/foo",
745745
"dependencies": "{...}",
746746
"targets": "{...}",
747747
"features": {

tests/testsuite/git.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3106,7 +3106,7 @@ fn metadata_master_consistency() {
31063106
"license": null,
31073107
"license_file": null,
31083108
"description": null,
3109-
"source": null,
3109+
"source": "path+file://[..]/foo",
31103110
"dependencies": [
31113111
{
31123112
"name": "bar",

tests/testsuite/metadata.rs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ fn cargo_metadata_simple() {
2424
"version": "0.5.0",
2525
"id": "foo[..]",
2626
"keywords": [],
27-
"source": null,
27+
"source": "path+file://[..]/foo",
2828
"dependencies": [],
2929
"edition": "2015",
3030
"license": null,
@@ -125,7 +125,7 @@ crate-type = ["lib", "staticlib"]
125125
"version": "0.5.0",
126126
"id": "foo[..]",
127127
"keywords": [],
128-
"source": null,
128+
"source": "path+file://[..]/foo",
129129
"dependencies": [],
130130
"edition": "2015",
131131
"license": null,
@@ -212,7 +212,7 @@ optional_feat = []
212212
"version": "0.5.0",
213213
"id": "foo[..]",
214214
"keywords": [],
215-
"source": null,
215+
"source": "path+file://[..]/foo",
216216
"dependencies": [],
217217
"edition": "2015",
218218
"license": null,
@@ -440,7 +440,7 @@ fn cargo_metadata_with_deps_and_version() {
440440
"repository": null,
441441
"homepage": null,
442442
"documentation": null,
443-
"source": null,
443+
"source": "path+file://[..]/foo",
444444
"targets": [
445445
{
446446
"crate_types": [
@@ -616,7 +616,7 @@ name = "ex"
616616
"links": null,
617617
"description": null,
618618
"edition": "2015",
619-
"source": null,
619+
"source": "path+file://[..]/foo",
620620
"dependencies": [],
621621
"targets": [
622622
{
@@ -709,7 +709,7 @@ crate-type = ["rlib", "dylib"]
709709
"links": null,
710710
"description": null,
711711
"edition": "2015",
712-
"source": null,
712+
"source": "path+file://[..]/foo",
713713
"dependencies": [],
714714
"targets": [
715715
{
@@ -804,7 +804,7 @@ fn workspace_metadata() {
804804
"homepage": null,
805805
"documentation": null,
806806
"keywords": [],
807-
"source": null,
807+
"source": "path+file://[..]/bar",
808808
"dependencies": [],
809809
"license": null,
810810
"license_file": null,
@@ -841,7 +841,7 @@ fn workspace_metadata() {
841841
"version": "0.5.0",
842842
"id": "baz[..]",
843843
"keywords": [],
844-
"source": null,
844+
"source": "path+file://[..]/baz",
845845
"dependencies": [],
846846
"license": null,
847847
"license_file": null,
@@ -933,7 +933,7 @@ fn workspace_metadata_no_deps() {
933933
"version": "0.5.0",
934934
"id": "bar[..]",
935935
"keywords": [],
936-
"source": null,
936+
"source": "path+file://[..]/bar",
937937
"dependencies": [],
938938
"license": null,
939939
"license_file": null,
@@ -970,7 +970,7 @@ fn workspace_metadata_no_deps() {
970970
"version": "0.5.0",
971971
"id": "baz[..]",
972972
"keywords": [],
973-
"source": null,
973+
"source": "path+file://[..]/baz",
974974
"dependencies": [],
975975
"license": null,
976976
"license_file": null,
@@ -1032,7 +1032,7 @@ const MANIFEST_OUTPUT: &str = r#"
10321032
"name":"foo",
10331033
"version":"0.5.0",
10341034
"id":"foo[..]0.5.0[..](path+file://[..]/foo)",
1035-
"source":null,
1035+
"source":"path+file://[..]/foo",
10361036
"dependencies":[],
10371037
"keywords": [],
10381038
"license": null,
@@ -1222,7 +1222,7 @@ fn package_metadata() {
12221222
"version": "0.1.0",
12231223
"id": "foo[..]",
12241224
"keywords": ["database"],
1225-
"source": null,
1225+
"source": "path+file://[..]/foo",
12261226
"dependencies": [],
12271227
"edition": "2015",
12281228
"license": null,
@@ -1299,7 +1299,7 @@ fn package_publish() {
12991299
"version": "0.1.0",
13001300
"id": "foo[..]",
13011301
"keywords": ["database"],
1302-
"source": null,
1302+
"source": "path+file://[..]/foo",
13031303
"dependencies": [],
13041304
"edition": "2015",
13051305
"license": null,
@@ -1382,7 +1382,7 @@ fn cargo_metadata_path_to_cargo_toml_project() {
13821382
"repository": null,
13831383
"homepage": null,
13841384
"documentation": null,
1385-
"source": null,
1385+
"source": "path+file://[..]/bar-0.5.0",
13861386
"targets": [
13871387
{
13881388
"crate_types": [
@@ -1468,7 +1468,7 @@ fn package_edition_2018() {
14681468
"repository": null,
14691469
"homepage": null,
14701470
"documentation": null,
1471-
"source": null,
1471+
"source": "path+file://[..]/foo",
14721472
"targets": [
14731473
{
14741474
"crate_types": [
@@ -1558,7 +1558,7 @@ fn target_edition_2018() {
15581558
"repository": null,
15591559
"homepage": null,
15601560
"documentation": null,
1561-
"source": null,
1561+
"source": "path+file://[..]/foo",
15621562
"targets": [
15631563
{
15641564
"crate_types": [
@@ -1766,7 +1766,7 @@ fn rename_dependency() {
17661766
"repository": null,
17671767
"homepage": null,
17681768
"documentation": null,
1769-
"source": null,
1769+
"source": "path+file://[..]/foo",
17701770
"targets": [
17711771
{
17721772
"crate_types": [
@@ -1886,7 +1886,7 @@ fn metadata_links() {
18861886
"repository": null,
18871887
"homepage": null,
18881888
"documentation": null,
1889-
"source": null,
1889+
"source": "path+file://[..]/foo",
18901890
"targets": [
18911891
{
18921892
"crate_types": [
@@ -1974,11 +1974,11 @@ fn deps_with_bin_only() {
19741974
"license": null,
19751975
"license_file": null,
19761976
"description": null,
1977-
"source": null,
1977+
"source": "path+file://[..]/foo",
19781978
"dependencies": [
19791979
{
19801980
"name": "bdep",
1981-
"source": null,
1981+
"source": "path+file://[..]/bdep",
19821982
"req": "*",
19831983
"kind": null,
19841984
"rename": null,
@@ -2258,7 +2258,7 @@ fn filter_platform() {
22582258
"license": null,
22592259
"license_file": null,
22602260
"description": null,
2261-
"source": null,
2261+
"source": "path+file://[..]/foo",
22622262
"dependencies": [
22632263
{
22642264
"name": "normal-dep",

tests/testsuite/read_manifest.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ fn manifest_output(readme_value: &str) -> String {
2323
"links": null,
2424
"description": null,
2525
"edition": "2015",
26-
"source":null,
26+
"source": "path+file://[..]/foo",
2727
"dependencies":[],
2828
"targets":[{{
2929
"kind":["bin"],

tests/testsuite/update.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ fn update_precise_first_run() {
463463
"name": "bar",
464464
"readme": null,
465465
"repository": null,
466-
"source": null,
466+
"source": "path+file://[..]/foo",
467467
"targets": [
468468
{
469469
"crate_types": [

0 commit comments

Comments
 (0)