在 Bazel 中构建后如何避免删除缓存文件

Ker*_*ley 5 bazel

genrule在 Bazel 中有一个应该操作一些文件的文件。我想我没有通过正确的路径访问这些文件,所以我想查看 Bazel 创建的目录结构,以便我可以调试。

echo在我的 genrule 中添加了一些语句,我可以看到 Bazel 在目录中工作/home/lyft/.cache/bazel/_bazel_lyft/8de0a1069de8d166c668173ca21c04ae/sandbox/linux-sandbox/1/execroot/。但是bazel运行完后,这个目录就没有了,所以无法查看目录结构。

如何防止 Bazel 删除其临时文件,以便我可以调试正在发生的事情?

acc*_*uck 8

由于这个问题是“在构建 bazel 后保留沙箱文件” Google 搜索的最佳结果,并且从接受的答案中对我来说并不明显,我觉得有必要写下这个答案。

简答

使用--sandbox_debug. 如果传递了此标志,则 Bazel 不会在构建完成后删除沙箱文件夹中的文件。

更长的答案

使用--sandbox_debug选项运行 bazel build :

$ bazel build mypackage:mytarget --sandbox_debug
Run Code Online (Sandbox Code Playgroud)

然后,您可以检查项目的沙箱文件夹的内容。

要获取当前项目的沙箱文件夹的位置,请导航到项目,然后运行:

$ bazel info output_base
/home/johnsmith/.cache/bazel/_bazel_johnsmith/d949417420413f64a0b619cb69f1db69  # output will be something like this
Run Code Online (Sandbox Code Playgroud)

在那个目录里面会有sandbox文件夹。

可能的警告:(我不确定这一点,但是)sandbox如果您之前运行了没有--sandbox_debug标志的构建并且部分成功,则文件夹中可能缺少某些文件。原因是 Bazel 不会重新运行已经成功的构建部分,因此与成功构建部分对应的文件可能不会最终出现在沙箱中。

如果要确保所有沙箱文件都在那里,请先使用bazel clean或清理项目bazel clean --expunge


Lás*_*zló 5

您可以使用--spawn_strategy=standalone。您还可以用来--sandbox_debug查看哪些目录已安装到沙箱中。

您还可以将 genrule 的 cmd 设置为find . > $@来调试 genrule 可用的内容。

重要提示:声明 genrule 将读取/写入/使用的所有 srcs/outs/tools,并用于$(location //label/of:target)查找它们的路径。例子:

genrule(
    name = "x1",
    srcs = ["//foo:input1.txt", "//bar:generated_file"],
    outs = ["x1out.txt", "x1err.txt"],
    tools = ["//util:bin1"],
    cmd = "$(location //util:bin1) --input1=$(location //foo:input1.txt) --input2=$(location //bar:generated_file) --some_flag --other_flag >$(location x1out.txt) 2>$(location x1err.txt)",
)
Run Code Online (Sandbox Code Playgroud)