@@ -3,7 +3,7 @@ use std::collections::BTreeSet;
3
3
4
4
use anyhow:: { bail, Result } ;
5
5
use cargo_metadata:: {
6
- DependencyKind , Metadata as CargoMetadata , Node , NodeDep , Package , PackageId ,
6
+ DependencyKind , Metadata as CargoMetadata , Node , NodeDep , Package , PackageId , Target ,
7
7
} ;
8
8
use cargo_platform:: Platform ;
9
9
use serde:: { Deserialize , Serialize } ;
@@ -187,19 +187,28 @@ fn collect_deps_selectable(
187
187
select
188
188
}
189
189
190
+ /// Packages may have targets that match aliases of dependents. This function
191
+ /// checks a target to see if it's an unexpected type for a dependency.
192
+ fn is_ignored_package_target ( target : & Target ) -> bool {
193
+ target
194
+ . kind
195
+ . iter ( )
196
+ . any ( |t| [ "example" , "bench" , "test" ] . contains ( & t. as_str ( ) ) )
197
+ }
198
+
190
199
fn is_lib_package ( package : & Package ) -> bool {
191
200
package. targets . iter ( ) . any ( |target| {
192
201
target
193
202
. crate_types
194
203
. iter ( )
195
204
. any ( |t| [ "lib" , "rlib" ] . contains ( & t. as_str ( ) ) )
205
+ && !is_ignored_package_target ( target)
196
206
} )
197
207
}
198
208
199
209
fn is_proc_macro_package ( package : & Package ) -> bool {
200
210
package. targets . iter ( ) . any ( |target| {
201
- target. crate_types . iter ( ) . any ( |t| t == "proc-macro" )
202
- && !target. kind . iter ( ) . any ( |t| t == "example" )
211
+ target. crate_types . iter ( ) . any ( |t| t == "proc-macro" ) && !is_ignored_package_target ( target)
203
212
} )
204
213
}
205
214
@@ -238,8 +247,13 @@ fn is_workspace_member(node_dep: &NodeDep, metadata: &CargoMetadata) -> bool {
238
247
239
248
fn get_library_target_name ( package : & Package , potential_name : & str ) -> Result < String > {
240
249
// If the potential name is not an alias in a dependent's package, a target's name
241
- // should match which means we already know what the target library name is.
242
- if package. targets . iter ( ) . any ( |t| t. name == potential_name) {
250
+ // should match which means we already know what the target library name is. The
251
+ // only exception is for targets that are otherwise ignored (like benchmarks or examples).
252
+ if package
253
+ . targets
254
+ . iter ( )
255
+ . any ( |t| t. name == potential_name && !is_ignored_package_target ( t) )
256
+ {
243
257
return Ok ( potential_name. to_string ( ) ) ;
244
258
}
245
259
@@ -271,11 +285,13 @@ fn get_library_target_name(package: &Package, potential_name: &str) -> Result<St
271
285
/// for targets where packages (packages[#].targets[#].name) uses crate names. In order to
272
286
/// determine whether or not a dependency is aliased, we compare it with all available targets
273
287
/// on it's package. Note that target names are not guaranteed to be module names where Node
274
- /// dependencies are, so we need to do a conversion to check for this
288
+ /// dependencies are, so we need to do a conversion to check for this. This function will
289
+ /// return the name of a target's alias in the content of the current dependent if it is aliased.
275
290
fn get_target_alias ( target_name : & str , package : & Package ) -> Option < String > {
276
291
match package
277
292
. targets
278
293
. iter ( )
294
+ . filter ( |t| !is_ignored_package_target ( t) )
279
295
. all ( |t| sanitize_module_name ( & t. name ) != target_name)
280
296
{
281
297
true => Some ( target_name. to_string ( ) ) ,
@@ -449,6 +465,30 @@ mod test {
449
465
assert_eq ! ( proc_macro_deps, Vec :: <& str >:: new( ) ) ;
450
466
}
451
467
468
+ #[ test]
469
+ fn bench_name_alias_dep ( ) {
470
+ let metadata = metadata:: alias ( ) ;
471
+
472
+ let node = find_metadata_node ( "surrealdb-core" , & metadata) ;
473
+ let dependencies = DependencySet :: new_for_node ( node, & metadata) ;
474
+
475
+ println ! ( "{:#?}" , dependencies) ;
476
+
477
+ let bindings = dependencies. normal_deps . items ( ) ;
478
+
479
+ // It's critical that the dep be found with the correct name and not the
480
+ // alias that the `aliases` package is using that coincidentally matches the
481
+ // `bench` target `executor` in the `async-executor` package.
482
+ let async_executor = bindings
483
+ . iter ( )
484
+ . find ( |( _, dep) | dep. target_name == "async-executor" )
485
+ . map ( |( _, dep) | dep)
486
+ . unwrap ( ) ;
487
+
488
+ // Ensure alias data is still tracked.
489
+ assert_eq ! ( async_executor. alias, Some ( "executor" . to_owned( ) ) ) ;
490
+ }
491
+
452
492
#[ test]
453
493
fn sys_dependencies ( ) {
454
494
let metadata = metadata:: build_scripts ( ) ;
0 commit comments