From ebc74639394b6daf870145271624f2eb73f0573b Mon Sep 17 00:00:00 2001 From: Ed Schouten Date: Mon, 23 Nov 2026 07:01:28 +0100 Subject: [PATCH] Eliminate cycles in globals in enum library I am currently working on an alternative implementation of Bazel named Bonanza. A difference between Bazel and Bonanza is that the latter requires all outcomes of individual analysis steps to be serializable. This makes it possible to write them to storage and reuse them by doing cache lookups. It also allows for better debugging/introspection (e.g., displaying the raw contents of providers). To make sure this doesn't go off the rails, Bonanza currently enforces that any globals and providers returned by rules are acyclic. It is allowed to create cycles as part of computation (l = []; l.append(l)), but these cannot be part of the end result of an analysis step. So far this limitation isn't really causing issues in practice. There is almost no Starlark code out there that uses constructs like these. However, I did find a single occurrence of it in rules_python's enum library. However, eliminating the cycle here is very easy, which is why I'd rather see rules_python eliminate the cycle than changing Bonanza's data model. --- python/private/enum.bzl | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/python/private/enum.bzl b/python/private/enum.bzl index 4d0fb10699..55765fe37f 100644 --- a/python/private/enum.bzl +++ b/python/private/enum.bzl @@ -22,8 +22,8 @@ def enum(methods = {}, **kwargs): Args: methods: {type}`dict[str, callable]` functions that will be - added to the created enum object, but will have the enum object - itself passed as the first positional arg when calling them. + added to the created enum object, but will have the `__members__` + dict passed as the first positional arg when calling them. **kwargs: The fields of the returned struct. All uppercase names will be treated as enum values and added to `__members__`. @@ -38,14 +38,13 @@ def enum(methods = {}, **kwargs): } for name, unbound_method in methods.items(): - # buildifier: disable=uninitialized - kwargs[name] = lambda *a, **k: unbound_method(self, *a, **k) + kwargs[name] = lambda *a, **k: unbound_method(members, *a, **k) self = struct(__members__ = members, **kwargs) return self -def _FlagEnum_flag_values(self): - return sorted(self.__members__.values()) +def _FlagEnum_flag_values(members): + return sorted(members.values()) def FlagEnum(**kwargs): """Define an enum specialized for flags.