Bazel与Starlark是否支持类似Python*、JS...的展开运算符?
* or JavaScript's ...? Short answer: No, Starlark (the language used in Bazel build files) does not have a dedicated list unpacking operator equivalent to Python's * or JavaScript's ... for expanding a list into another list.
Alternative 1: Use the + operator for list concatenation
The most common and concise way to achieve your desired behavior is to use the + operator to combine your static list with the returned list from get_more_files(). This works directly in the rule's parameter definition:
some_rule( name = "target_name", src = ["file1.txt", "file2.txt"] + get_more_files(), )
This creates a new list that includes all elements from your static files followed by every element returned by get_more_files()—exactly the unpack-and-combine effect you're looking for.
Alternative 2: Use the extend() method
As you noted, you're already aware of the extend() method. This modifies a list in-place by appending all elements from another list to it. Here's how to implement it:
# Start with your base files srcs = ["file1.txt", "file2.txt"] # Add all elements from get_more_files() to the list srcs.extend(get_more_files()) some_rule( name = "target_name", src = srcs, )
This approach is handy if you need to build your list incrementally (e.g., adding elements from multiple functions or sources) before passing it to the rule.
Quick note on function argument unpacking
It's also worth mentioning that Starlark doesn't support unpacking lists into function arguments like Python's func(*args) syntax either. If you need to pass multiple arguments from a list to a function, you'll have to access individual elements directly or use helper functions if the target function accepts a list parameter.
内容的提问来源于stack exchange,提问作者Erran

