Skip to content

spec: Load an entire .bzl file as a single symbol (glob/star imports) #344

Description

@jmillikin

It is relatively common to see a struct used to aggregate a module's public API, for example in https://github.com/bazelbuild/rules_cc/blob/0.2.17/cc/common/cc_helper.bzl

cc_helper = struct(
    rule_error = _rule_error,
    attribute_error = _attribute_error,
    # ... a lot of symbols ...
    package_exec_path = _package_exec_path,
    should_create_test_dwp_for_statically_linked_test = _should_create_test_dwp_for_statically_linked_test,
)

This pattern is used to simplify the load for that module:

load("@rules_cc//cc/common:cc_helper.bzl", "cc_helper")

There's been some proposals to add glob/star imports in the past (example: #111) but they haven't been adopted due to breaking syntax compatibility.

I propose to add this syntax:

# load the entire `helpers.bzl` namespace under the `helpers` symbol
load("@example//path/to/helpers.bzl", helpers = "*")

# roughly equivalent to:
load("@example//path/to/helpers.bzl", _sym0 = "do_something", _sym1 = "do_something_else")
helpers = struct(
    do_something = _sym0,
    do_something_else = _sym1,
)

This would enable new code to have simpler imports while maintaining syntax compatibility with existing AST parsers and formatters.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions