You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Please Build中结合filegroup使用target|file语法?

在Please Build中结合filegroup使用target|file语法的解决方案

结论先行:不能直接对filegroup使用target|file语法。原因是filegroup的srcs映射只是内部的分组逻辑标签,并非实际输出文件的命名绑定;而你用genrule实现生效,是因为genrule通过outs明确声明了输出文件的命名映射,这才是target|file语法能识别的前提。

下面提供两种等效实现方式:

方式一:直接引用子target

如果不需要通过filegroup的分组间接引用,可以直接在genrule的srcs里指定子目录的具体target:

# BUILD.plz

a_and_c_concat = genrule(
    name = 'a_and_c_concat',
    srcs = {
        'A': ['@//subdir:a_file'],
        'C': ['@//subdir:c_file'],
    },
    outs = ["out"],
    cmd = """        set -eux
        cat "${SRCS_A}" "${SRCS_C}" > "${OUTS}"    """,
    visibility = ['PUBLIC'],
)

方式二:嵌套filegroup实现分组引用

如果希望保留分组逻辑,避免直接引用单个target,可以把每个分组单独定义为filegroup,再通过总filegroup聚合:

# subdir/BUILD.plz

this = package_name()

a = text_file(
    name = 'a_file',
    content = "aaaaaaaaa",
)

b = text_file(
    name = 'b_file',
    content = "bbbbbbbbb",
)

c = text_file(
    name = 'c_file',
    content = "ccccccccc",
)

# 为每个分组单独定义filegroup
filegroup(
    name = 'group_A',
    srcs = [a],
    visibility = ['PUBLIC']
)

filegroup(
    name = 'group_B',
    srcs = [b],
    visibility = ['PUBLIC']
)

filegroup(
    name = 'group_C',
    srcs = [c],
    visibility = ['PUBLIC']
)

# 总filegroup聚合所有分组
filegroup(
    name = this,
    srcs = ['group_A', 'group_B', 'group_C'],
    visibility = ['PUBLIC']
)

然后在根BUILD.plz中引用这些分组filegroup:

# BUILD.plz

a_and_c_concat = genrule(
    name = 'a_and_c_concat',
    srcs = {
        'A': ['@//subdir:group_A'],
        'C': ['@//subdir:group_C'],
    },
    outs = ["out"],
    cmd = """        set -eux
        cat "${SRCS_A}" "${SRCS_C}" > "${OUTS}"    """,
    visibility = ['PUBLIC'],
)

内容的提问来源于stack exchange,提问作者izissise

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 02:20:38