Skip to content

Commit fe7f479

Browse files
committed
Update test data for crate deduping
Make data reflect a case where dev deps are existent. base-db::CrateGraph::extend now adds dev dependencies for a crate in case of its upgrading from a CrateOrigin::Lib kind of a crate to a CrateOrigin::Local one.
1 parent 1a9b845 commit fe7f479

File tree

4 files changed

+162
-41
lines changed

4 files changed

+162
-41
lines changed

crates/base-db/src/input.rs

+40-11
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ impl CrateData {
376376
let other_deps = other.dependencies.iter();
377377

378378
if ignore_dev_deps {
379-
slf_deps
379+
return slf_deps
380380
.clone()
381381
.filter(|it| it.kind == DependencyKind::Normal)
382382
.eq(other_deps.clone().filter(|it| it.kind == DependencyKind::Normal));
@@ -524,7 +524,7 @@ impl CrateGraph {
524524

525525
self.check_cycle_after_dependency(from, dep.crate_id)?;
526526

527-
self.arena[from].add_dep(dep);
527+
self.arena[from].add_dep_unchecked(dep);
528528
Ok(())
529529
}
530530

@@ -665,16 +665,11 @@ impl CrateGraph {
665665
return Some((id, false));
666666
}
667667
}
668-
(CrateOrigin::Local { .. }, CrateOrigin::Library { .. }) => {
668+
(a @ CrateOrigin::Local { .. }, CrateOrigin::Library { .. })
669+
| (a @ CrateOrigin::Library { .. }, CrateOrigin::Local { .. }) => {
669670
// See #15656 for a relevant example.
670671
if data.eq_ignoring_origin_and_deps(&crate_data, true) {
671-
return Some((id, false));
672-
}
673-
}
674-
(CrateOrigin::Library { .. }, CrateOrigin::Local { .. }) => {
675-
// See #15656 for a relevant example.
676-
if data.eq_ignoring_origin_and_deps(&crate_data, true) {
677-
return Some((id, true));
672+
return Some((id, if a.is_local() { false } else { true }));
678673
}
679674
}
680675
(_, _) => return None,
@@ -692,6 +687,16 @@ impl CrateGraph {
692687
if let CrateOrigin::Library { repo, name } = origin_old {
693688
self.arena[res].origin = CrateOrigin::Local { repo, name: Some(name) };
694689
}
690+
691+
// Move local's dev dependencies into the newly-local-formerly-lib crate.
692+
let dev_deps = crate_data
693+
.dependencies
694+
.clone()
695+
.into_iter()
696+
.filter(|dep| dep.kind() == DependencyKind::Dev)
697+
.collect::<Vec<Dependency>>();
698+
699+
self.arena[res].add_dep(dev_deps).unwrap_or_default();
695700
}
696701
} else {
697702
let id = self.arena.alloc(crate_data.clone());
@@ -761,10 +766,34 @@ impl ops::Index<CrateId> for CrateGraph {
761766
}
762767
}
763768

769+
struct ExistingDepsError(Vec<Dependency>);
770+
764771
impl CrateData {
765-
fn add_dep(&mut self, dep: Dependency) {
772+
/// Add a dependency to `self` without checking if the dependency
773+
// is existent among `self.dependencies`.
774+
fn add_dep_unchecked(&mut self, dep: Dependency) {
766775
self.dependencies.push(dep)
767776
}
777+
778+
/// Add `deps` to `self` if the dependency is not already listed.
779+
/// Finally returning an `Err` propagating the dependencies it couldn't add.
780+
fn add_dep(&mut self, deps: Vec<Dependency>) -> Result<(), ExistingDepsError> {
781+
let mut existing_deps: Vec<Dependency> = vec![];
782+
783+
deps.into_iter().for_each(|dep| {
784+
if !self.dependencies.contains(&dep) {
785+
self.dependencies.push(dep);
786+
} else {
787+
existing_deps.push(dep);
788+
}
789+
});
790+
791+
if !existing_deps.is_empty() {
792+
return Err(ExistingDepsError(existing_deps));
793+
}
794+
795+
Ok(())
796+
}
768797
}
769798

770799
impl FromStr for Edition {

crates/project-model/src/tests.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::{
33
path::{Path, PathBuf},
44
};
55

6-
use base_db::{CrateGraph, FileId, ProcMacroPaths};
6+
use base_db::{CrateGraph, DependencyKind, FileId, ProcMacroPaths};
77
use cfg::{CfgAtom, CfgDiff};
88
use expect_test::{expect_file, ExpectFile};
99
use paths::{AbsPath, AbsPathBuf};
@@ -272,7 +272,9 @@ fn test_deduplicate_crate_differing_in_origin() {
272272
}
273273

274274
assert!(crates_named_p1.len() == 1);
275-
assert!(crates_named_p1[0].origin.is_local());
275+
let p1 = crates_named_p1[0];
276+
assert!(p1.dependencies.iter().filter(|dep| dep.kind() == DependencyKind::Dev).count() == 1);
277+
assert!(p1.origin.is_local());
276278
}
277279

278280
#[test]
@@ -297,5 +299,7 @@ fn test_deduplicate_crate_differing_in_origin_in_rev_resolution_order() {
297299
}
298300

299301
assert!(crates_named_p1.len() == 1);
300-
assert!(crates_named_p1[0].origin.is_local());
302+
let p1 = crates_named_p1[0];
303+
assert!(p1.dependencies.iter().filter(|dep| dep.kind() == DependencyKind::Dev).count() == 1);
304+
assert!(p1.origin.is_local());
301305
}

crates/project-model/test_data/deduplication_crate_graph_A.json

+84-10
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,26 @@
33
{
44
"name": "p1",
55
"version": "0.1.0",
6-
"id": "p1 0.1.0 (path+file:///path/to/project/projects/p1)",
6+
"id": "p1 0.1.0 (path+file:///home/alibektas/codebase/rust/ra/example_project/projects/p1)",
77
"license": null,
88
"license_file": null,
99
"description": null,
1010
"source": null,
11-
"dependencies": [],
11+
"dependencies": [
12+
{
13+
"name": "p3",
14+
"source": null,
15+
"req": "*",
16+
"kind": "dev",
17+
"rename": null,
18+
"optional": false,
19+
"uses_default_features": true,
20+
"features": [],
21+
"target": null,
22+
"registry": null,
23+
"path": "/home/alibektas/codebase/rust/ra/example_project/projects/p3"
24+
}
25+
],
1226
"targets": [
1327
{
1428
"kind": [
@@ -18,15 +32,56 @@
1832
"lib"
1933
],
2034
"name": "p1",
21-
"src_path": "/path/to/project/projects/p1/src/lib.rs",
35+
"src_path": "/home/alibektas/codebase/rust/ra/example_project/projects/p1/src/lib.rs",
2236
"edition": "2021",
2337
"doc": true,
2438
"doctest": true,
2539
"test": true
2640
}
2741
],
2842
"features": {},
29-
"manifest_path": "/path/to/project/projects/p1/Cargo.toml",
43+
"manifest_path": "/home/alibektas/codebase/rust/ra/example_project/projects/p1/Cargo.toml",
44+
"metadata": null,
45+
"publish": null,
46+
"authors": [],
47+
"categories": [],
48+
"keywords": [],
49+
"readme": null,
50+
"repository": null,
51+
"homepage": null,
52+
"documentation": null,
53+
"edition": "2021",
54+
"links": null,
55+
"default_run": null,
56+
"rust_version": null
57+
},
58+
{
59+
"name": "p3",
60+
"version": "0.1.0",
61+
"id": "p3 0.1.0 (path+file:///home/alibektas/codebase/rust/ra/example_project/projects/p3)",
62+
"license": null,
63+
"license_file": null,
64+
"description": null,
65+
"source": null,
66+
"dependencies": [],
67+
"targets": [
68+
{
69+
"kind": [
70+
"lib"
71+
],
72+
"crate_types": [
73+
"lib"
74+
],
75+
"name": "p3",
76+
"src_path": "/home/alibektas/codebase/rust/ra/example_project/projects/p3/src/lib.rs",
77+
"edition": "2021",
78+
"doc": true,
79+
"doctest": true,
80+
"test": true
81+
}
82+
],
83+
"features": {},
84+
"manifest_path": "/home/alibektas/codebase/rust/ra/example_project/projects/p3/Cargo.toml",
3085
"metadata": null,
3186
"publish": null,
3287
"authors": [],
@@ -43,24 +98,43 @@
4398
}
4499
],
45100
"workspace_members": [
46-
"p1 0.1.0 (path+file:///path/to/project/projects/p1)"
101+
"p1 0.1.0 (path+file:///home/alibektas/codebase/rust/ra/example_project/projects/p1)"
47102
],
48103
"workspace_default_members": [
49-
"p1 0.1.0 (path+file:///path/to/project/projects/p1)"
104+
"p1 0.1.0 (path+file:///home/alibektas/codebase/rust/ra/example_project/projects/p1)"
50105
],
51106
"resolve": {
52107
"nodes": [
53108
{
54-
"id": "p1 0.1.0 (path+file:///path/to/project/projects/p1)",
109+
"id": "p1 0.1.0 (path+file:///home/alibektas/codebase/rust/ra/example_project/projects/p1)",
110+
"dependencies": [
111+
"p3 0.1.0 (path+file:///home/alibektas/codebase/rust/ra/example_project/projects/p3)"
112+
],
113+
"deps": [
114+
{
115+
"name": "p3",
116+
"pkg": "p3 0.1.0 (path+file:///home/alibektas/codebase/rust/ra/example_project/projects/p3)",
117+
"dep_kinds": [
118+
{
119+
"kind": "dev",
120+
"target": null
121+
}
122+
]
123+
}
124+
],
125+
"features": []
126+
},
127+
{
128+
"id": "p3 0.1.0 (path+file:///home/alibektas/codebase/rust/ra/example_project/projects/p3)",
55129
"dependencies": [],
56130
"deps": [],
57131
"features": []
58132
}
59133
],
60-
"root": "p1 0.1.0 (path+file:///path/to/project/projects/p1)"
134+
"root": "p1 0.1.0 (path+file:///home/alibektas/codebase/rust/ra/example_project/projects/p1)"
61135
},
62-
"target_directory": "/path/to/project/projects/p1/target",
136+
"target_directory": "/home/alibektas/codebase/rust/ra/example_project/projects/p1/target",
63137
"version": 1,
64-
"workspace_root": "/path/to/project/projects/p1",
138+
"workspace_root": "/home/alibektas/codebase/rust/ra/example_project/projects/p1",
65139
"metadata": null
66140
}

crates/project-model/test_data/deduplication_crate_graph_B.json

+31-17
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,26 @@
33
{
44
"name": "p1",
55
"version": "0.1.0",
6-
"id": "p1 0.1.0 (path+file:///path/to/project/projects/p1)",
6+
"id": "p1 0.1.0 (path+file:///home/alibektas/codebase/rust/ra/example_project/projects/p1)",
77
"license": null,
88
"license_file": null,
99
"description": null,
1010
"source": null,
11-
"dependencies": [],
11+
"dependencies": [
12+
{
13+
"name": "p3",
14+
"source": null,
15+
"req": "*",
16+
"kind": "dev",
17+
"rename": null,
18+
"optional": false,
19+
"uses_default_features": true,
20+
"features": [],
21+
"target": null,
22+
"registry": null,
23+
"path": "/home/alibektas/codebase/rust/ra/example_project/projects/p3"
24+
}
25+
],
1226
"targets": [
1327
{
1428
"kind": [
@@ -18,15 +32,15 @@
1832
"lib"
1933
],
2034
"name": "p1",
21-
"src_path": "/path/to/project/projects/p1/src/lib.rs",
35+
"src_path": "/home/alibektas/codebase/rust/ra/example_project/projects/p1/src/lib.rs",
2236
"edition": "2021",
2337
"doc": true,
2438
"doctest": true,
2539
"test": true
2640
}
2741
],
2842
"features": {},
29-
"manifest_path": "/path/to/project/projects/p1/Cargo.toml",
43+
"manifest_path": "/home/alibektas/codebase/rust/ra/example_project/projects/p1/Cargo.toml",
3044
"metadata": null,
3145
"publish": null,
3246
"authors": [],
@@ -44,7 +58,7 @@
4458
{
4559
"name": "p2",
4660
"version": "0.1.0",
47-
"id": "p2 0.1.0 (path+file:///path/to/project/projects/p2)",
61+
"id": "p2 0.1.0 (path+file:///home/alibektas/codebase/rust/ra/example_project/projects/p2)",
4862
"license": null,
4963
"license_file": null,
5064
"description": null,
@@ -61,7 +75,7 @@
6175
"features": [],
6276
"target": null,
6377
"registry": null,
64-
"path": "/path/to/project/projects/p1"
78+
"path": "/home/alibektas/codebase/rust/ra/example_project/projects/p1"
6579
}
6680
],
6781
"targets": [
@@ -73,15 +87,15 @@
7387
"lib"
7488
],
7589
"name": "p2",
76-
"src_path": "/path/to/project/projects/p2/src/lib.rs",
90+
"src_path": "/home/alibektas/codebase/rust/ra/example_project/projects/p2/src/lib.rs",
7791
"edition": "2021",
7892
"doc": true,
7993
"doctest": true,
8094
"test": true
8195
}
8296
],
8397
"features": {},
84-
"manifest_path": "/path/to/project/projects/p2/Cargo.toml",
98+
"manifest_path": "/home/alibektas/codebase/rust/ra/example_project/projects/p2/Cargo.toml",
8599
"metadata": null,
86100
"publish": null,
87101
"authors": [],
@@ -98,28 +112,28 @@
98112
}
99113
],
100114
"workspace_members": [
101-
"p2 0.1.0 (path+file:///path/to/project/projects/p2)"
115+
"p2 0.1.0 (path+file:///home/alibektas/codebase/rust/ra/example_project/projects/p2)"
102116
],
103117
"workspace_default_members": [
104-
"p2 0.1.0 (path+file:///path/to/project/projects/p2)"
118+
"p2 0.1.0 (path+file:///home/alibektas/codebase/rust/ra/example_project/projects/p2)"
105119
],
106120
"resolve": {
107121
"nodes": [
108122
{
109-
"id": "p1 0.1.0 (path+file:///path/to/project/projects/p1)",
123+
"id": "p1 0.1.0 (path+file:///home/alibektas/codebase/rust/ra/example_project/projects/p1)",
110124
"dependencies": [],
111125
"deps": [],
112126
"features": []
113127
},
114128
{
115-
"id": "p2 0.1.0 (path+file:///path/to/project/projects/p2)",
129+
"id": "p2 0.1.0 (path+file:///home/alibektas/codebase/rust/ra/example_project/projects/p2)",
116130
"dependencies": [
117-
"p1 0.1.0 (path+file:///path/to/project/projects/p1)"
131+
"p1 0.1.0 (path+file:///home/alibektas/codebase/rust/ra/example_project/projects/p1)"
118132
],
119133
"deps": [
120134
{
121135
"name": "p1",
122-
"pkg": "p1 0.1.0 (path+file:///path/to/project/projects/p1)",
136+
"pkg": "p1 0.1.0 (path+file:///home/alibektas/codebase/rust/ra/example_project/projects/p1)",
123137
"dep_kinds": [
124138
{
125139
"kind": null,
@@ -131,10 +145,10 @@
131145
"features": []
132146
}
133147
],
134-
"root": "p2 0.1.0 (path+file:///path/to/project/projects/p2)"
148+
"root": "p2 0.1.0 (path+file:///home/alibektas/codebase/rust/ra/example_project/projects/p2)"
135149
},
136-
"target_directory": "/path/to/project/projects/p2/target",
150+
"target_directory": "/home/alibektas/codebase/rust/ra/example_project/projects/p2/target",
137151
"version": 1,
138-
"workspace_root": "/path/to/project/projects/p2",
152+
"workspace_root": "/home/alibektas/codebase/rust/ra/example_project/projects/p2",
139153
"metadata": null
140154
}

0 commit comments

Comments
 (0)