-
Notifications
You must be signed in to change notification settings - Fork 236
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add lazy utils function to try resolve lazy_dict
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
1 parent
26af837
commit 2dc0165
Showing
4 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = {}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
) |