@@ -69,21 +69,23 @@ def _list_dependencies_by_crate(path: pathlib.Path, *, basedir: pathlib.Path, ca
69
69
70
70
packages_by_id = {p ['id' ]: p for p in metadata ['packages' ]}
71
71
72
- # Collect the `(|dev-|build-)dependencies` into a <"extern crate name"> → <package> dictionary.
73
- dependencies = {}
72
+ # Collect the `(|dev-|build-)dependencies` into a <is a `build-dependency`> → (< "extern crate name"> → <package>) dictionary.
73
+ dependencies : Dict [ bool , Dict [ str , Dict [ str , Any ]]] = {False : {}, True : {} }
74
74
for dep in next (n ['deps' ] for n in metadata ['resolve' ]['nodes' ] if n ['id' ] == main_package ['id' ]):
75
- if _need_dev_deps (main_target ) or any (dep_kind ['kind' ] != ['dev' ] for dep_kind in dep ['dep_kinds' ]):
76
- dependencies [dep ['name' ]] = packages_by_id [dep ['pkg' ]]
75
+ if any (k ['kind' ] == ['build' ] for k in dep ['dep_kinds' ]):
76
+ dependencies [True ][dep ['name' ]] = packages_by_id [dep ['pkg' ]]
77
+ if _need_dev_deps (main_target ) or any (k ['kind' ] is None for k in dep ['dep_kinds' ]):
78
+ dependencies [False ][dep ['name' ]] = packages_by_id [dep ['pkg' ]]
77
79
78
80
# Decides whether to include `main_package` itself.
79
81
# Note that cargo-udeps does not detect it if it is unused.
80
82
# https://github.com/est31/cargo-udeps/pull/35
81
83
depends_on_main_package_itself = not _is_lib_or_proc_macro (main_target ) and any (map (_is_lib_or_proc_macro , main_package ['targets' ]))
82
84
83
85
# If `cargo_udeps_toolchain` is present, collects packages that are "unused" by `target`.
84
- unused_packages = set ()
86
+ unused_packages : Dict [ bool , Set [ str ]] = { False : set (), True : set ()}
85
87
if cargo_udeps_toolchain is not None :
86
- explicit_names_in_toml = {d ['rename' ] for d in main_package ['dependencies' ] if d ['rename' ]}
88
+ explicit_names_in_toml = {( d ['kind' ] == 'build' , d [ ' rename' ]) for d in main_package ['dependencies' ] if d ['rename' ]}
87
89
if not shutil .which ('cargo-udeps' ):
88
90
raise RuntimeError ('`cargo-udeps` not in $PATH' )
89
91
unused_deps = json .loads (subprocess .run (
@@ -94,21 +96,23 @@ def _list_dependencies_by_crate(path: pathlib.Path, *, basedir: pathlib.Path, ca
94
96
).stdout .decode ())['unused_deps' ].values ()
95
97
unused_dep = next ((u for u in unused_deps if u ['manifest_path' ] == main_package ['manifest_path' ]), None )
96
98
if unused_dep :
97
- for name_in_toml in [* unused_dep ['normal' ], * unused_dep ['development' ], * unused_dep ['build' ]]:
98
- if name_in_toml in explicit_names_in_toml :
99
+ for is_build , name_in_toml in [* [( False , n ) for n in [ * unused_dep ['normal' ], * unused_dep ['development' ]]] , * [( True , n ) for n in unused_dep ['build' ] ]]:
100
+ if ( is_build , name_in_toml ) in explicit_names_in_toml :
99
101
# If the `name_in_toml` is explicitly renamed one, it equals to the `extern_crate_name`.
100
- unused_packages .add (dependencies [name_in_toml ]['id' ])
102
+ unused_packages [ is_build ] .add (dependencies [ is_build ] [name_in_toml ]['id' ])
101
103
else :
102
104
# Otherwise, it equals to the `package.name`.
103
- unused_packages .add (next (p ['id' ] for p in dependencies .values () if p ['name' ] == name_in_toml ))
105
+ unused_packages [ is_build ] .add (next (p ['id' ] for p in dependencies [ is_build ] .values () if p ['name' ] == name_in_toml ))
104
106
105
107
# Finally, adds source files related to the depended crates except:
106
108
#
107
109
# - those detected by cargo-udeps
108
110
# - those come from Crates.io or Git repositories (e.g. `proconio`, other people's libraries including `ac-library-rs`)
109
111
ret = common_result
110
- for depended_package in [* dependencies .values (), * ([main_package ] if depends_on_main_package_itself else [])]:
111
- if depended_package ['id' ] in unused_packages or depended_package ['source' ]:
112
+ depended_packages = [(False , main_package )] if depends_on_main_package_itself else []
113
+ depended_packages .extend ((is_build , package ) for is_build , dependencies in dependencies .items () for package in dependencies .values ())
114
+ for is_build , depended_package in depended_packages :
115
+ if depended_package ['id' ] in unused_packages [is_build ] or depended_package ['source' ]:
112
116
continue
113
117
depended_target = next (filter (_is_lib_or_proc_macro , depended_package ['targets' ]), None )
114
118
if depended_target :
0 commit comments