Bazel 使用标签路径检查文件位置是否存在

Bazel 使用标签路径检查文件位置是否存在,bazel,Bazel,是否有一种简单的方法来获取路径对象,以便检查给定的标签路径是否存在。例如,假设path.exists(“@external\u project\u name/:filethatthaghtexist.txt”):。我可以看到存储库上下文中有这样的内容。但我需要有一个包装库规则。是否可以改为在宏或Skylark本机调用中执行此操作?即使使用存储库规则,我在这方面也遇到了很多问题,因为您已经指出: 如果使用不存在的路径创建标签,将导致生成失败 但如果您愿意执行存储库规则,这里有一个可能的解决方案 在

是否有一种简单的方法来获取路径对象,以便检查给定的标签路径是否存在。例如,假设path.exists(“@external\u project\u name/:filethatthaghtexist.txt”):。我可以看到存储库上下文中有这样的内容。但我需要有一个包装库规则。是否可以改为在宏或Skylark本机调用中执行此操作?

即使使用存储库规则,我在这方面也遇到了很多问题,因为您已经指出:

如果使用不存在的路径创建标签,将导致生成失败

但如果您愿意执行存储库规则,这里有一个可能的解决方案

在本例中,如果配置文件不存在,我的规则允许指定默认配置。可以将配置签入.gitignore并为单个开发人员覆盖,但在大多数情况下都是开箱即用的

我想我理解了为什么ctx.actions现在有兄弟参数,这里的想法是一样的。诀窍是config_file_location是一个真正的标签,然后config_file是一个字符串属性。我随意选择构建,但因为所有工作区都有顶级构建,所以公共构建似乎是合法的

WORKSPACE Definition
...
workspace(name="E02_mysql_database")
json_datasource_configuration(name="E02_datasources",
                              config_file_location="@E02_mysql_database//:BUILD",
                              config_file="database.json")
json_数据源_配置的定义如下所示:

json_datasource_configuration = repository_rule(
    attrs = {
        "config_file_location": attr.label(
            doc="""
            Path relative to the repository root for a datasource config file.

            """),
        "config_file": attr.string(
            doc="""
            Config file, maybe absent
            """),
        "default_config": attr.string(
            # better way to do this?
            default="None",
            doc = """
            If no config is at the path, then this will be the default config.
            Should look something like:

            {
                "datasource_name": {
                    "host": "<host>"
                    "port": <port>
                    "password": "<password>"
                    "username": "<username>"
                    "jdbc_connection_string": "<optional>"
                }
            }

            There can be more than datasource configured... maybe, eventually.
            """,
        ),
    },
    local = True,
    implementation = _json_config_impl,
)
可能已经太迟了,但你的问题是我在这个目标中发现的唯一一件事。如果有人知道更好的方法,我正在寻找其他选择。要向其他开发人员解释为什么规则必须以这种方式工作是很复杂的


另外请注意,如果更改配置文件,则必须清除以使工作区重新读取配置。我还没能想出任何办法来解决这个问题。glob()在工作区中不工作。

看起来不可能这样。有趣的是,即使在创建存储库时遇到麻烦,如果创建的标签的路径不存在,也会导致构建失败。path.exists()需要一个标签。只有存在的路径才能被检查,如果它们存在的话。你能依赖于这个标签然后调用.files吗?这是大多数规则返回的提供程序
def _json_config_impl(ctx):
    """
    Allows you to specify a file on disk to use for data connection.

    If you pass a default
    """
    config_path = ctx.path(ctx.attr.config_file_location).dirname.get_child(ctx.attr.config_file)

    config = ""
    if config_path.exists:
        config = ctx.read(config_path)
    elif ctx.attr.default_config == "None":
        fail("Could not find config at %s, you must supply a default_config if this is intentional" % ctx.attr.config_file)
    else:
        config = ctx.attr.default_config
...