Skip to content

Commit

Permalink
Add lazy utils function to try resolve lazy_dict
Browse files Browse the repository at this point in the history
Summary:
Add lazy utils function to try resolve lazy_dict

Will add doc later

Reviewed By: IanChilds

Differential Revision: D68962012

fbshipit-source-id: 9b3444348698cddc3d2bada3d9f7885c65ff9031
  • Loading branch information
Nero5023 authored and facebook-github-bot committed Feb 4, 2025
1 parent 26af837 commit 2dc0165
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 0 deletions.
39 changes: 39 additions & 0 deletions prelude/bxl/lazy.bxl
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under both the MIT license found in the
# LICENSE-MIT file in the root directory of this source tree and the Apache
# License, Version 2.0 found in the LICENSE-APACHE file in the root directory
# of this source tree.

def catch_resolve_lazy_dict(bxl_ctx: bxl.Context, lazy_dict: dict[typing.Any, bxl.Lazy]) -> dict[typing.Any, bxl.Result]:
"""
Resolves a dictionary of bxl.Lazy operations in parallel while capturing errors.

Parameters:
- `bxl_ctx`: BXL context
- `lazy_dict`: Dictionary to resolve, where values must be `bxl.Lazy` instances

Returns:
A new dictionary preserving original keys, with values as `bxl.Result` objects.

Example:
```python
def _impl(ctx):
lazy_dict = {
"app": ctx.lazy.configured_targets("cell//:app"),
"lib": ctx.lazy.configured_targets("cell//:lib")
}

# Batch resolve and process
results = catch_resolve_lazy_dict(ctx, lazy_dict)
for name, res in results.items():
if res.is_ok():
ctx.output.print(f"{name}: {res.unwrap()}")
else:
ctx.output.print(f"{name} failed: {res.unwrap_err()}")
```
"""
keys = lazy_dict.keys()
lazies = [lazy.catch() for lazy in lazy_dict.values()]
values = bxl_ctx.lazy.join_all(lazies).resolve()
return dict(zip(keys, values))
8 changes: 8 additions & 0 deletions tests/e2e/bxl/utils/BUCK
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
load("@fbcode//buck2/tests:buck_e2e.bzl", "buck2_e2e_test")

oncall("build_infra")

buck2_e2e_test(
name = "test_lazy_utils",
srcs = ["test_lazy_utils.py"],
)
33 changes: 33 additions & 0 deletions tests/e2e/bxl/utils/test_lazy_utils.bxl
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under both the MIT license found in the
# LICENSE-MIT file in the root directory of this source tree and the Apache
# License, Version 2.0 found in the LICENSE-APACHE file in the root directory
# of this source tree.

load("@prelude//:asserts.bzl", "asserts")
load("@prelude//bxl:lazy.bxl", "catch_resolve_lazy_dict")

def _test_catch_resolve_lazy_dict(ctx: bxl.Context):
targets = [
"fbcode//buck2:buck2",
"fbcode//buck2:buck2_bundle",
"fbcode//buck2/app/buck2:buck2",
"fbcode//buck2/app/buck2_bxl:buck2_bxl",
]
name2lazy = {
target: ctx.lazy.unconfigured_target_node(target)
for target in targets
}
res = catch_resolve_lazy_dict(ctx, name2lazy)
for key, value in res.items():
asserts.true(isinstance(key, str))
asserts.true(isinstance(value, bxl.Result))
asserts.true(value.is_ok())
node = value.unwrap()
asserts.equals(key, str(node.label))

test_catch_resolve_lazy_dict = bxl_main(
impl = _test_catch_resolve_lazy_dict,
cli_args = {},
)
16 changes: 16 additions & 0 deletions tests/e2e/bxl/utils/test_lazy_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under both the MIT license found in the
# LICENSE-MIT file in the root directory of this source tree and the Apache
# License, Version 2.0 found in the LICENSE-APACHE file in the root directory
# of this source tree.

from buck2.tests.e2e_util.api.buck import Buck
from buck2.tests.e2e_util.buck_workspace import buck_test


@buck_test(inplace=True)
async def test_catch_resolve_lazy_dict(buck: Buck) -> None:
await buck.bxl(
"fbcode//buck2/tests/e2e/bxl/utils/test_lazy_utils.bxl:test_catch_resolve_lazy_dict",
)

0 comments on commit 2dc0165

Please sign in to comment.