Skip to content

Commit 8805d05

Browse files
committed
Auto merge of rust-lang#13296 - Strum355:package-information-package-name, r=Veykril
Fix PackageInformation having the crate name instead of package name The `PackageInformation` type from the LSIF PR used the _crate_ name instead of the _package_ name. This caused issues when looking up crates by this name on the Sourcegraph backend, where we sync crate contents, for crates such as actix-web and many of its components (actix-files, actix-http etc etc), see screenshot 1 for the observed symptom. This PR hasnt been tested on other entry points besides cargo (such as project json). See screenshot 2 for the change in behaviour via SCIP snapshot comparison. <details> <summary>Screenshot 1</summary> ![crates.io giving 404](https://user-images.githubusercontent.com/18282288/192286637-8bf7c333-4441-4e60-8cce-de7eaa11ee9f.png) </details> <details> <summary>Screenshot 2</summary> ![before and after from SCIP snapshot output](https://user-images.githubusercontent.com/18282288/192287733-d7e73ff0-abbc-4ae5-82d0-bf9dc45d755c.png) </details> Follow-up PR to my question over at the [rust-lang Zulip](https://rust-lang.zulipchat.com/#narrow/stream/185405-t-compiler.2Frust-analyzer/topic/canonical.20crate.20name.20confusion.20for.20monikers), excuse any incorrect usages of the term package vs crate.
2 parents b01cdd8 + 651c586 commit 8805d05

File tree

6 files changed

+85
-24
lines changed

6 files changed

+85
-24
lines changed

crates/base-db/src/fixture.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ impl ChangeFixture {
196196
Env::default(),
197197
Ok(Vec::new()),
198198
false,
199-
CrateOrigin::CratesIo { repo: None },
199+
CrateOrigin::CratesIo { repo: None, name: None },
200200
);
201201
} else {
202202
for (from, to, prelude) in crate_deps {
@@ -270,7 +270,7 @@ impl ChangeFixture {
270270
Env::default(),
271271
Ok(proc_macro),
272272
true,
273-
CrateOrigin::CratesIo { repo: None },
273+
CrateOrigin::CratesIo { repo: None, name: None },
274274
);
275275

276276
for krate in all_crates {
@@ -398,7 +398,7 @@ fn parse_crate(crate_str: String) -> (String, CrateOrigin, Option<String>) {
398398
let (version, origin) = match b.split_once(':') {
399399
Some(("CratesIo", data)) => match data.split_once(',') {
400400
Some((version, url)) => {
401-
(version, CrateOrigin::CratesIo { repo: Some(url.to_owned()) })
401+
(version, CrateOrigin::CratesIo { repo: Some(url.to_owned()), name: None })
402402
}
403403
_ => panic!("Bad crates.io parameter: {}", data),
404404
},
@@ -409,7 +409,7 @@ fn parse_crate(crate_str: String) -> (String, CrateOrigin, Option<String>) {
409409
let crate_origin = match &*crate_str {
410410
"std" => CrateOrigin::Lang(LangCrateOrigin::Std),
411411
"core" => CrateOrigin::Lang(LangCrateOrigin::Core),
412-
_ => CrateOrigin::CratesIo { repo: None },
412+
_ => CrateOrigin::CratesIo { repo: None, name: None },
413413
};
414414
(crate_str, crate_origin, None)
415415
}

crates/base-db/src/input.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ impl ops::Deref for CrateName {
136136
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
137137
pub enum CrateOrigin {
138138
/// Crates that are from crates.io official registry,
139-
CratesIo { repo: Option<String> },
139+
CratesIo { repo: Option<String>, name: Option<String> },
140140
/// Crates that are provided by the language, like std, core, proc-macro, ...
141141
Lang(LangCrateOrigin),
142142
}
@@ -648,7 +648,7 @@ mod tests {
648648
Env::default(),
649649
Ok(Vec::new()),
650650
false,
651-
CrateOrigin::CratesIo { repo: None },
651+
CrateOrigin::CratesIo { repo: None, name: None },
652652
);
653653
let crate2 = graph.add_crate_root(
654654
FileId(2u32),
@@ -660,7 +660,7 @@ mod tests {
660660
Env::default(),
661661
Ok(Vec::new()),
662662
false,
663-
CrateOrigin::CratesIo { repo: None },
663+
CrateOrigin::CratesIo { repo: None, name: None },
664664
);
665665
let crate3 = graph.add_crate_root(
666666
FileId(3u32),
@@ -672,7 +672,7 @@ mod tests {
672672
Env::default(),
673673
Ok(Vec::new()),
674674
false,
675-
CrateOrigin::CratesIo { repo: None },
675+
CrateOrigin::CratesIo { repo: None, name: None },
676676
);
677677
assert!(graph
678678
.add_dep(crate1, Dependency::new(CrateName::new("crate2").unwrap(), crate2))
@@ -698,7 +698,7 @@ mod tests {
698698
Env::default(),
699699
Ok(Vec::new()),
700700
false,
701-
CrateOrigin::CratesIo { repo: None },
701+
CrateOrigin::CratesIo { repo: None, name: None },
702702
);
703703
let crate2 = graph.add_crate_root(
704704
FileId(2u32),
@@ -710,7 +710,7 @@ mod tests {
710710
Env::default(),
711711
Ok(Vec::new()),
712712
false,
713-
CrateOrigin::CratesIo { repo: None },
713+
CrateOrigin::CratesIo { repo: None, name: None },
714714
);
715715
assert!(graph
716716
.add_dep(crate1, Dependency::new(CrateName::new("crate2").unwrap(), crate2))
@@ -733,7 +733,7 @@ mod tests {
733733
Env::default(),
734734
Ok(Vec::new()),
735735
false,
736-
CrateOrigin::CratesIo { repo: None },
736+
CrateOrigin::CratesIo { repo: None, name: None },
737737
);
738738
let crate2 = graph.add_crate_root(
739739
FileId(2u32),
@@ -745,7 +745,7 @@ mod tests {
745745
Env::default(),
746746
Ok(Vec::new()),
747747
false,
748-
CrateOrigin::CratesIo { repo: None },
748+
CrateOrigin::CratesIo { repo: None, name: None },
749749
);
750750
let crate3 = graph.add_crate_root(
751751
FileId(3u32),
@@ -757,7 +757,7 @@ mod tests {
757757
Env::default(),
758758
Ok(Vec::new()),
759759
false,
760-
CrateOrigin::CratesIo { repo: None },
760+
CrateOrigin::CratesIo { repo: None, name: None },
761761
);
762762
assert!(graph
763763
.add_dep(crate1, Dependency::new(CrateName::new("crate2").unwrap(), crate2))
@@ -780,7 +780,7 @@ mod tests {
780780
Env::default(),
781781
Ok(Vec::new()),
782782
false,
783-
CrateOrigin::CratesIo { repo: None },
783+
CrateOrigin::CratesIo { repo: None, name: None },
784784
);
785785
let crate2 = graph.add_crate_root(
786786
FileId(2u32),
@@ -792,7 +792,7 @@ mod tests {
792792
Env::default(),
793793
Ok(Vec::new()),
794794
false,
795-
CrateOrigin::CratesIo { repo: None },
795+
CrateOrigin::CratesIo { repo: None, name: None },
796796
);
797797
assert!(graph
798798
.add_dep(

crates/ide/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ impl Analysis {
236236
Env::default(),
237237
Ok(Vec::new()),
238238
false,
239-
CrateOrigin::CratesIo { repo: None },
239+
CrateOrigin::CratesIo { repo: None, name: None },
240240
);
241241
change.change_file(file_id, Some(Arc::new(text)));
242242
change.set_crate_graph(crate_graph);

crates/ide/src/moniker.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -253,10 +253,14 @@ pub(crate) fn def_to_moniker(
253253
},
254254
kind: if krate == from_crate { MonikerKind::Export } else { MonikerKind::Import },
255255
package_information: {
256-
let name = krate.display_name(db)?.to_string();
257-
let (repo, version) = match krate.origin(db) {
258-
CrateOrigin::CratesIo { repo } => (repo?, krate.version(db)?),
256+
let (name, repo, version) = match krate.origin(db) {
257+
CrateOrigin::CratesIo { repo, name } => (
258+
name.unwrap_or(krate.display_name(db)?.canonical_name().to_string()),
259+
repo?,
260+
krate.version(db)?,
261+
),
259262
CrateOrigin::Lang(lang) => (
263+
krate.display_name(db)?.canonical_name().to_string(),
260264
"https://github.com/rust-lang/rust/".to_string(),
261265
match lang {
262266
LangCrateOrigin::Other => {

crates/project-model/src/tests.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,9 @@ fn cargo_hello_world_project_model_with_wildcard_overrides() {
185185
),
186186
origin: CratesIo {
187187
repo: None,
188+
name: Some(
189+
"hello-world",
190+
),
188191
},
189192
is_proc_macro: false,
190193
},
@@ -260,6 +263,9 @@ fn cargo_hello_world_project_model_with_wildcard_overrides() {
260263
),
261264
origin: CratesIo {
262265
repo: None,
266+
name: Some(
267+
"hello-world",
268+
),
263269
},
264270
is_proc_macro: false,
265271
},
@@ -335,6 +341,9 @@ fn cargo_hello_world_project_model_with_wildcard_overrides() {
335341
),
336342
origin: CratesIo {
337343
repo: None,
344+
name: Some(
345+
"hello-world",
346+
),
338347
},
339348
is_proc_macro: false,
340349
},
@@ -410,6 +419,9 @@ fn cargo_hello_world_project_model_with_wildcard_overrides() {
410419
),
411420
origin: CratesIo {
412421
repo: None,
422+
name: Some(
423+
"hello-world",
424+
),
413425
},
414426
is_proc_macro: false,
415427
},
@@ -477,6 +489,9 @@ fn cargo_hello_world_project_model_with_wildcard_overrides() {
477489
repo: Some(
478490
"https://github.com/rust-lang/libc",
479491
),
492+
name: Some(
493+
"libc",
494+
),
480495
},
481496
is_proc_macro: false,
482497
},
@@ -567,6 +582,9 @@ fn cargo_hello_world_project_model_with_selective_overrides() {
567582
),
568583
origin: CratesIo {
569584
repo: None,
585+
name: Some(
586+
"hello-world",
587+
),
570588
},
571589
is_proc_macro: false,
572590
},
@@ -644,6 +662,9 @@ fn cargo_hello_world_project_model_with_selective_overrides() {
644662
),
645663
origin: CratesIo {
646664
repo: None,
665+
name: Some(
666+
"hello-world",
667+
),
647668
},
648669
is_proc_macro: false,
649670
},
@@ -721,6 +742,9 @@ fn cargo_hello_world_project_model_with_selective_overrides() {
721742
),
722743
origin: CratesIo {
723744
repo: None,
745+
name: Some(
746+
"hello-world",
747+
),
724748
},
725749
is_proc_macro: false,
726750
},
@@ -798,6 +822,9 @@ fn cargo_hello_world_project_model_with_selective_overrides() {
798822
),
799823
origin: CratesIo {
800824
repo: None,
825+
name: Some(
826+
"hello-world",
827+
),
801828
},
802829
is_proc_macro: false,
803830
},
@@ -865,6 +892,9 @@ fn cargo_hello_world_project_model_with_selective_overrides() {
865892
repo: Some(
866893
"https://github.com/rust-lang/libc",
867894
),
895+
name: Some(
896+
"libc",
897+
),
868898
},
869899
is_proc_macro: false,
870900
},
@@ -946,6 +976,9 @@ fn cargo_hello_world_project_model() {
946976
),
947977
origin: CratesIo {
948978
repo: None,
979+
name: Some(
980+
"hello-world",
981+
),
949982
},
950983
is_proc_macro: false,
951984
},
@@ -1023,6 +1056,9 @@ fn cargo_hello_world_project_model() {
10231056
),
10241057
origin: CratesIo {
10251058
repo: None,
1059+
name: Some(
1060+
"hello-world",
1061+
),
10261062
},
10271063
is_proc_macro: false,
10281064
},
@@ -1100,6 +1136,9 @@ fn cargo_hello_world_project_model() {
11001136
),
11011137
origin: CratesIo {
11021138
repo: None,
1139+
name: Some(
1140+
"hello-world",
1141+
),
11031142
},
11041143
is_proc_macro: false,
11051144
},
@@ -1177,6 +1216,9 @@ fn cargo_hello_world_project_model() {
11771216
),
11781217
origin: CratesIo {
11791218
repo: None,
1219+
name: Some(
1220+
"hello-world",
1221+
),
11801222
},
11811223
is_proc_macro: false,
11821224
},
@@ -1244,6 +1286,9 @@ fn cargo_hello_world_project_model() {
12441286
repo: Some(
12451287
"https://github.com/rust-lang/libc",
12461288
),
1289+
name: Some(
1290+
"libc",
1291+
),
12471292
},
12481293
is_proc_macro: false,
12491294
},
@@ -1804,6 +1849,9 @@ fn rust_project_hello_world_project_model() {
18041849
),
18051850
origin: CratesIo {
18061851
repo: None,
1852+
name: Some(
1853+
"hello_world",
1854+
),
18071855
},
18081856
is_proc_macro: false,
18091857
},

crates/project-model/src/workspace.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -518,9 +518,15 @@ fn project_json_to_crate_graph(
518518
proc_macro,
519519
krate.is_proc_macro,
520520
if krate.display_name.is_some() {
521-
CrateOrigin::CratesIo { repo: krate.repository.clone() }
521+
CrateOrigin::CratesIo {
522+
repo: krate.repository.clone(),
523+
name: krate
524+
.display_name
525+
.clone()
526+
.map(|n| n.canonical_name().to_string()),
527+
}
522528
} else {
523-
CrateOrigin::CratesIo { repo: None }
529+
CrateOrigin::CratesIo { repo: None, name: None }
524530
},
525531
),
526532
)
@@ -740,14 +746,17 @@ fn detached_files_to_crate_graph(
740746
let detached_file_crate = crate_graph.add_crate_root(
741747
file_id,
742748
Edition::CURRENT,
743-
display_name,
749+
display_name.clone(),
744750
None,
745751
cfg_options.clone(),
746752
cfg_options.clone(),
747753
Env::default(),
748754
Ok(Vec::new()),
749755
false,
750-
CrateOrigin::CratesIo { repo: None },
756+
CrateOrigin::CratesIo {
757+
repo: None,
758+
name: display_name.map(|n| n.canonical_name().to_string()),
759+
},
751760
);
752761

753762
public_deps.add(detached_file_crate, &mut crate_graph);
@@ -923,7 +932,7 @@ fn add_target_crate_root(
923932
env,
924933
proc_macro,
925934
is_proc_macro,
926-
CrateOrigin::CratesIo { repo: pkg.repository.clone() },
935+
CrateOrigin::CratesIo { repo: pkg.repository.clone(), name: Some(pkg.name.clone()) },
927936
)
928937
}
929938

0 commit comments

Comments
 (0)