Skip to content

Commit 5de25c8

Browse files
author
Bogdan Mircea
committed
add is_test to crate spec
1 parent ac1dbe0 commit 5de25c8

3 files changed

Lines changed: 43 additions & 15 deletions

File tree

rust/private/rust_analyzer.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ def _create_single_crate(ctx, attrs, info):
236236
crate["edition"] = info.crate.edition
237237
crate["env"] = {}
238238
crate["crate_type"] = info.crate.type
239+
crate["is_test"] = info.crate.is_test
239240

240241
# Switch on external/ to determine if crates are in the workspace or remote.
241242
# TODO: Some folks may want to override this for vendored dependencies.

tools/rust_analyzer/aquery.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ pub struct CrateSpec {
5151
pub env: BTreeMap<String, String>,
5252
pub target: String,
5353
pub crate_type: CrateType,
54+
pub is_test: bool,
5455
pub build: Option<CrateSpecBuild>,
5556
}
5657

@@ -226,6 +227,7 @@ fn consolidate_crate_specs(crate_specs: Vec<CrateSpec>) -> anyhow::Result<BTreeS
226227
existing.display_name = spec.display_name;
227228
existing.crate_type = CrateType::Rlib;
228229
existing.build = spec.build;
230+
existing.is_test = spec.is_test;
229231
}
230232

231233
// For proc-macro crates that exist within the workspace, there will be a
@@ -267,6 +269,7 @@ mod test {
267269
env: BTreeMap::new(),
268270
target: "x86_64-unknown-linux-gnu".into(),
269271
crate_type: CrateType::Rlib,
272+
is_test: false,
270273
build: Some(CrateSpecBuild {
271274
label: "//:test".to_owned(),
272275
build_file: "BUILD.bazel".to_owned().into(),
@@ -286,6 +289,7 @@ mod test {
286289
env: BTreeMap::new(),
287290
target: "x86_64-unknown-linux-gnu".into(),
288291
crate_type: CrateType::Rlib,
292+
is_test: false,
289293
build: None,
290294
},
291295
CrateSpec {
@@ -302,6 +306,7 @@ mod test {
302306
env: BTreeMap::new(),
303307
target: "x86_64-unknown-linux-gnu".into(),
304308
crate_type: CrateType::Rlib,
309+
is_test: false,
305310
build: None,
306311
},
307312
CrateSpec {
@@ -318,6 +323,7 @@ mod test {
318323
env: BTreeMap::new(),
319324
target: "x86_64-unknown-linux-gnu".into(),
320325
crate_type: CrateType::Bin,
326+
is_test: true,
321327
build: None,
322328
},
323329
];
@@ -339,6 +345,7 @@ mod test {
339345
env: BTreeMap::new(),
340346
target: "x86_64-unknown-linux-gnu".into(),
341347
crate_type: CrateType::Rlib,
348+
is_test: false,
342349
build: Some(CrateSpecBuild {
343350
label: "//:test".to_owned(),
344351
build_file: "BUILD.bazel".to_owned().into(),
@@ -358,6 +365,7 @@ mod test {
358365
env: BTreeMap::new(),
359366
target: "x86_64-unknown-linux-gnu".into(),
360367
crate_type: CrateType::Rlib,
368+
is_test: false,
361369
build: None
362370
},
363371
CrateSpec {
@@ -374,6 +382,7 @@ mod test {
374382
env: BTreeMap::new(),
375383
target: "x86_64-unknown-linux-gnu".into(),
376384
crate_type: CrateType::Rlib,
385+
is_test: false,
377386
build: None
378387
},
379388
])
@@ -397,6 +406,7 @@ mod test {
397406
env: BTreeMap::new(),
398407
target: "x86_64-unknown-linux-gnu".into(),
399408
crate_type: CrateType::Bin,
409+
is_test: true,
400410
build: None,
401411
},
402412
CrateSpec {
@@ -413,6 +423,7 @@ mod test {
413423
env: BTreeMap::new(),
414424
target: "x86_64-unknown-linux-gnu".into(),
415425
crate_type: CrateType::Rlib,
426+
is_test: false,
416427
build: None,
417428
},
418429
CrateSpec {
@@ -429,6 +440,7 @@ mod test {
429440
env: BTreeMap::new(),
430441
target: "x86_64-unknown-linux-gnu".into(),
431442
crate_type: CrateType::Rlib,
443+
is_test: false,
432444
build: None,
433445
},
434446
CrateSpec {
@@ -445,6 +457,7 @@ mod test {
445457
env: BTreeMap::new(),
446458
target: "x86_64-unknown-linux-gnu".into(),
447459
crate_type: CrateType::Rlib,
460+
is_test: false,
448461
build: None,
449462
},
450463
];
@@ -466,6 +479,7 @@ mod test {
466479
env: BTreeMap::new(),
467480
target: "x86_64-unknown-linux-gnu".into(),
468481
crate_type: CrateType::Rlib,
482+
is_test: false,
469483
build: None
470484
},
471485
CrateSpec {
@@ -482,6 +496,7 @@ mod test {
482496
env: BTreeMap::new(),
483497
target: "x86_64-unknown-linux-gnu".into(),
484498
crate_type: CrateType::Rlib,
499+
is_test: false,
485500
build: None
486501
},
487502
CrateSpec {
@@ -498,6 +513,7 @@ mod test {
498513
env: BTreeMap::new(),
499514
target: "x86_64-unknown-linux-gnu".into(),
500515
crate_type: CrateType::Rlib,
516+
is_test: false,
501517
build: None
502518
},
503519
])
@@ -526,6 +542,7 @@ mod test {
526542
env: BTreeMap::new(),
527543
target: "x86_64-unknown-linux-gnu".into(),
528544
crate_type: CrateType::Rlib,
545+
is_test: false,
529546
build: None,
530547
},
531548
CrateSpec {
@@ -542,6 +559,7 @@ mod test {
542559
env: BTreeMap::new(),
543560
target: "x86_64-unknown-linux-gnu".into(),
544561
crate_type: CrateType::Bin,
562+
is_test: true,
545563
build: None,
546564
},
547565
CrateSpec {
@@ -558,6 +576,7 @@ mod test {
558576
env: BTreeMap::new(),
559577
target: "x86_64-unknown-linux-gnu".into(),
560578
crate_type: CrateType::Bin,
579+
is_test: false,
561580
build: None,
562581
},
563582
CrateSpec {
@@ -574,6 +593,7 @@ mod test {
574593
env: BTreeMap::new(),
575594
target: "x86_64-unknown-linux-gnu".into(),
576595
crate_type: CrateType::Rlib,
596+
is_test: false,
577597
build: None,
578598
},
579599
];
@@ -596,6 +616,7 @@ mod test {
596616
env: BTreeMap::new(),
597617
target: "x86_64-unknown-linux-gnu".into(),
598618
crate_type: CrateType::Rlib,
619+
is_test: false,
599620
build: None,
600621
},
601622
CrateSpec {
@@ -612,6 +633,7 @@ mod test {
612633
env: BTreeMap::new(),
613634
target: "x86_64-unknown-linux-gnu".into(),
614635
crate_type: CrateType::Rlib,
636+
is_test: false,
615637
build: None
616638
},
617639
])
@@ -642,6 +664,7 @@ mod test {
642664
env: BTreeMap::new(),
643665
target: "x86_64-unknown-linux-gnu".into(),
644666
crate_type: CrateType::ProcMacro,
667+
is_test: false,
645668
build: None,
646669
},
647670
CrateSpec {
@@ -660,6 +683,7 @@ mod test {
660683
env: BTreeMap::new(),
661684
target: "x86_64-unknown-linux-gnu".into(),
662685
crate_type: CrateType::ProcMacro,
686+
is_test: false,
663687
build: None,
664688
},
665689
];
@@ -684,6 +708,7 @@ mod test {
684708
env: BTreeMap::new(),
685709
target: "x86_64-unknown-linux-gnu".into(),
686710
crate_type: CrateType::ProcMacro,
711+
is_test: false,
687712
build: None,
688713
},])
689714
);

tools/rust_analyzer/rust_project.rs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -159,20 +159,6 @@ pub enum TargetKind {
159159
Test,
160160
}
161161

162-
impl From<CrateType> for TargetKind {
163-
fn from(value: CrateType) -> Self {
164-
match value {
165-
CrateType::Bin => Self::Bin,
166-
CrateType::Rlib
167-
| CrateType::Lib
168-
| CrateType::Dylib
169-
| CrateType::Cdylib
170-
| CrateType::Staticlib
171-
| CrateType::ProcMacro => Self::Lib,
172-
}
173-
}
174-
}
175-
176162
/// A template-like structure for describing runnables.
177163
///
178164
/// These are used for running and debugging binaries and tests without encoding
@@ -286,6 +272,18 @@ pub fn generate_rust_project(
286272
} else {
287273
log::trace!("Merging crate {}", &c.crate_id);
288274
merged_crates_index.insert(c.crate_id.clone(), project.crates.len());
275+
276+
let target_kind = match c.crate_type {
277+
CrateType::Bin if c.is_test => TargetKind::Test,
278+
CrateType::Bin => TargetKind::Bin,
279+
CrateType::Rlib
280+
| CrateType::Lib
281+
| CrateType::Dylib
282+
| CrateType::Cdylib
283+
| CrateType::Staticlib
284+
| CrateType::ProcMacro => TargetKind::Lib,
285+
};
286+
289287
project.crates.push(Crate {
290288
display_name: Some(c.display_name.clone()),
291289
root_module: c.root_module.clone(),
@@ -326,7 +324,7 @@ pub fn generate_rust_project(
326324
build: c.build.as_ref().map(|b| Build {
327325
label: b.label.clone(),
328326
build_file: b.build_file.clone(),
329-
target_kind: c.crate_type.into(),
327+
target_kind,
330328
}),
331329
});
332330
}
@@ -472,6 +470,7 @@ mod tests {
472470
env: BTreeMap::new(),
473471
target: "x86_64-unknown-linux-gnu".into(),
474472
crate_type: CrateType::Rlib,
473+
is_test: false,
475474
build: None,
476475
}]),
477476
)
@@ -506,6 +505,7 @@ mod tests {
506505
env: BTreeMap::new(),
507506
target: "x86_64-unknown-linux-gnu".into(),
508507
crate_type: CrateType::Rlib,
508+
is_test: false,
509509
build: None,
510510
},
511511
CrateSpec {
@@ -522,6 +522,7 @@ mod tests {
522522
env: BTreeMap::new(),
523523
target: "x86_64-unknown-linux-gnu".into(),
524524
crate_type: CrateType::Rlib,
525+
is_test: false,
525526
build: None,
526527
},
527528
CrateSpec {
@@ -538,6 +539,7 @@ mod tests {
538539
env: BTreeMap::new(),
539540
target: "x86_64-unknown-linux-gnu".into(),
540541
crate_type: CrateType::Rlib,
542+
is_test: false,
541543
build: None,
542544
},
543545
]),

0 commit comments

Comments
 (0)