Kerrick Staley
Kerrick Staley

Reputation: 1933

How to avoid deleting cached files after build in Bazel

I have a genrule in Bazel that is supposed to manipulate some files. I think I'm not accessing these files by the correct path, so I want to look at the directory structure that Bazel is creating so I can debug.

I added some echo statements to my genrule and I can see that Bazel is working in the directory /home/lyft/.cache/bazel/_bazel_lyft/8de0a1069de8d166c668173ca21c04ae/sandbox/linux-sandbox/1/execroot/. However, after Bazel finishes running, this directory is gone, so I can't look at the directory structure.

How can I prevent Bazel from deleting its temporary files so that I can debug what's happening?

Upvotes: 8

Views: 8722

Answers (2)

user1857492
user1857492

Reputation: 767

Since this question is a top result for "keep sandbox files after build bazel" Google search and it wasn't obvious for me from the accepted answer, I feel the need to write this answer.

Short answer

Use --sandbox_debug. If this flag is passed, Bazel will not delete the files inside the sandbox folder after the build finishes.

Longer answer

Run bazel build with --sandbox_debug option:

$ bazel build mypackage:mytarget --sandbox_debug

Then you can inspect the contents of the sandbox folder for the project.

To get the location of the sandbox folder for current project, navigate to project and then run:

$ bazel info output_base
/home/johnsmith/.cache/bazel/_bazel_johnsmith/d949417420413f64a0b619cb69f1db69  # output will be something like this

Inside that directory there will be sandbox folder.

Possible caveat: (I'm NOT sure about this but) It's possible that some of the files are missing in sandbox folder, if you previously ran a build without --sandbox_debug flag and it partially succeeded. The reason is Bazel won't rerun parts of the build that already succeeded, and consequently the files corresponding to the successful build parts might not end up in the sandbox.

If you want to make sure all the sandbox files are there, clean the project first using either bazel clean or bazel clean --expunge.

Upvotes: 17

László
László

Reputation: 4271

You can use --spawn_strategy=standalone. You can also use --sandbox_debug to see which directories are mounted to the sandbox.

You can also set the genrule's cmd to find . > $@ to debug what's available to the genrule.

Important: declare all srcs/outs/tools that the genrule will read/write/use, and use $(location //label/of:target) to look up their path. Example:

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)",
)

Upvotes: 5

Related Questions