Skip to content

Commit 497e81a

Browse files
committed
Consider build-dependencies correctly
1 parent 29a181e commit 497e81a

File tree

1 file changed

+16
-12
lines changed
  • onlinejudge_verify/languages

1 file changed

+16
-12
lines changed

onlinejudge_verify/languages/rust.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,21 +69,23 @@ def _list_dependencies_by_crate(path: pathlib.Path, *, basedir: pathlib.Path, ca
6969

7070
packages_by_id = {p['id']: p for p in metadata['packages']}
7171

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: {}}
7474
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']]
7779

7880
# Decides whether to include `main_package` itself.
7981
# Note that cargo-udeps does not detect it if it is unused.
8082
# https://github.com/est31/cargo-udeps/pull/35
8183
depends_on_main_package_itself = not _is_lib_or_proc_macro(main_target) and any(map(_is_lib_or_proc_macro, main_package['targets']))
8284

8385
# 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()}
8587
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']}
8789
if not shutil.which('cargo-udeps'):
8890
raise RuntimeError('`cargo-udeps` not in $PATH')
8991
unused_deps = json.loads(subprocess.run(
@@ -94,21 +96,23 @@ def _list_dependencies_by_crate(path: pathlib.Path, *, basedir: pathlib.Path, ca
9496
).stdout.decode())['unused_deps'].values()
9597
unused_dep = next((u for u in unused_deps if u['manifest_path'] == main_package['manifest_path']), None)
9698
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:
99101
# 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'])
101103
else:
102104
# 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))
104106

105107
# Finally, adds source files related to the depended crates except:
106108
#
107109
# - those detected by cargo-udeps
108110
# - those come from Crates.io or Git repositories (e.g. `proconio`, other people's libraries including `ac-library-rs`)
109111
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']:
112116
continue
113117
depended_target = next(filter(_is_lib_or_proc_macro, depended_package['targets']), None)
114118
if depended_target:

0 commit comments

Comments
 (0)