facebook / buck2

Build system, successor to Buck
https://buck2.build/
Apache License 2.0
3.53k stars 215 forks source link

Why does bxl_actions require an execution platform? #529

Open zjturner opened 9 months ago

zjturner commented 9 months ago

If you write ctx.bxl_actions from a bxl, the first thing you're confronted with is an error saying that you need to enable execution platforms.

It's not documented how to do this anywhere, the closest I could find was this page, but all it really says is that you have to enable execution platforms, but doesn't offer any information about how to do this.

For anyone else coming along with the same problem, I had some code like this:

# defs.bzl
def _execution_platform_impl(ctx: AnalysisContext) -> list[Provider]:
    constraints = dict()
    constraints.update(ctx.attrs.cpu_configuration[ConfigurationInfo].constraints)
    constraints.update(ctx.attrs.os_configuration[ConfigurationInfo].constraints)
    cfg = ConfigurationInfo(constraints = constraints, values = {})

    name = ctx.label.raw_target()
    platform = ExecutionPlatformInfo(
        label = name,
        configuration = cfg,
        executor_config = CommandExecutorConfig(
            local_enabled = True,
            remote_enabled = False,
            use_windows_path_separators = ctx.attrs.use_windows_path_separators,
        ),
    )

    return [
        DefaultInfo(),
        platform,
        PlatformInfo(label = str(name), configuration = cfg),
        ExecutionPlatformRegistrationInfo(platforms = [platform]),
    ]

execution_platform = rule(
    impl = _execution_platform_impl,
    attrs = {
        "cpu_configuration": attrs.dep(providers = [ConfigurationInfo]),
        "os_configuration": attrs.dep(providers = [ConfigurationInfo]),
        "use_windows_path_separators": attrs.bool(),
    },
)

# BUCK
load(":defs.bzl", "execution_platform")

execution_platform(
    name = "host-execution",
    cpu_configuration = host_configuration.cpu,
    os_configuration = host_configuration.os,
    use_windows_path_separators = host_info().os.is_windows,
)

And then added this to my .buckconfig:

[build]
execution_platforms = root//platforms:host-execution

Not sure if this is correct / ideal / missing something, but hopefully that helps someone else coming along.

Back to my original question, why is this needed? Core buck2 runtime doesn't need this when just building software. Obviously it can run actions, declare outputs, run arbitrary rule code, etc, without an execution platform. So what is special about bxl that makes it need an execution platform in order to do the exact same operations?

wendy728 commented 9 months ago

What you did is correct, but also I think that normal buck2 runtime has some fallback behavior that wasn't implemented in BXL. I think it makes sense to add it to BXL too.

Side note - we are planning on adding more platform docs soon, sorry for the sparse documentation!

cbarrete commented 1 month ago

This has just bit me as well, so I'd like to know what it really means to "enable" execution platforms. I've tried to set the various parameters in ctx.bxl_actions, but I keep getting a error: Execution platforms are not enabled. Adding a platform via configuration does make things "work", but it's not clear to me what impact it might have on other commands: will this have an impact on my builds, tests and buck2 runs?

If someone points me to the right place to add the fallback behavior for BXL, I don't mind submitting a PR to do it myself.