From d9c664a270c0ce55dd49ffdee6cf035cce835c25 Mon Sep 17 00:00:00 2001 From: Xavier Claessens Date: Wed, 22 Nov 2023 13:14:19 -0500 Subject: [PATCH] cargo: Fix error when defining options on build-dependencies We are not using them yet, but we should already at least declare its options dictionary. This avoid crashing when generated code adds features into that dict that was not yet declared. --- mesonbuild/cargo/interpreter.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/mesonbuild/cargo/interpreter.py b/mesonbuild/cargo/interpreter.py index 4be78f9b9a3c..616e7d1ca7de 100644 --- a/mesonbuild/cargo/interpreter.py +++ b/mesonbuild/cargo/interpreter.py @@ -437,6 +437,14 @@ def _create_project(cargo: Manifest, build: builder.Builder) -> T.List[mparser.B return [build.function('project', args, kwargs)] +def _all_dependencies(cargo: Manifest) -> T.Iterable[T.Tuple[str, Dependency]]: + yield from cargo.dependencies.items() + yield from cargo.dev_dependencies.items() + yield from cargo.build_dependencies.items() + for deps in cargo.target.values(): + yield from deps.items() + + def _process_feature(cargo: Manifest, feature: str) -> T.Tuple[T.Set[str], T.Dict[str, T.Set[str]], T.Set[str]]: # Set of features that must also be enabled if this feature is enabled. features: T.Set[str] = set() @@ -474,7 +482,7 @@ def _create_features(cargo: Manifest, build: builder.Builder) -> T.List[mparser. # and one per dependency. ast: T.List[mparser.BaseNode] = [] ast.append(build.assign(build.dict({}), 'features')) - for depname in itertools.chain.from_iterable([cargo.dependencies] + [deps for deps in cargo.target.values()]): + for depname, _ in _all_dependencies(cargo): ast.append(build.assign(build.dict({}), _options_varname(depname))) # Declare a dict that map required dependencies to true @@ -769,13 +777,7 @@ def dependencies(source_dir: str) -> T.Dict[str, Dependency]: deps: T.Dict[str, Dependency] = {} manifests = _load_manifests(source_dir) for cargo in manifests.values(): - deps_i = [ - cargo.dependencies.values(), - cargo.dev_dependencies.values(), - cargo.build_dependencies.values(), - ] - deps_i += [deps.values() for deps in cargo.target.values()] - for dep in itertools.chain.from_iterable(deps_i): + for _, dep in _all_dependencies(cargo): depname = _dependency_name(dep.package, dep.api) deps[depname] = dep return deps