Ittai
Ittai

Reputation: 5915

Bazel- how to get a short but hermetic temp dir for tests?

I have a test which tries to start an embedded mysql.
It tries to write a socket file to Files.createTempFile(null, ".sock") which returns with the following error: mysql start failed with error: [ERROR] The socket file path is too long (> 103): /private/var/tmp/_bazel_user/1c8ed8d84f6cb79483aa3cc4da758c86/bazel-sandbox/2478112867584790357/execroot/some_workspace/_tmp/dfebe48cda4dfdc8739653efedfa4933/394798020705754292.sock.
I worked around it by re-pointing java temp dir to /tmp using jvm_flags but this doesn't work when I try to use sandboxing since I guess the test isn't allowed to write there.

I've tried setting a symbolic link from the java code (like so Files.createSymbolicLink(Paths.get("/tmp/foo"),Paths.get(System.getProperty("java.io.tmpdir"))) but this doesn't seem to help.

I've also tried setting the output_base but that didn't help either.

Would really appreciate pointers and tips since I currently can't run my tests under sandbox and so can't parallelize them.

Upvotes: 3

Views: 3508

Answers (2)

rtsai2000
rtsai2000

Reputation: 348

In a bazel test, you can use the TEST_TMPDIR environment variable for a test-private writeable area.

See https://docs.bazel.build/versions/master/test-encyclopedia.html

Upvotes: 1

Philipp Wollermann
Philipp Wollermann

Reputation: 444

I think your approach of re-pointing Java to /tmp should work. The macOS sandbox always allows writing to a number of directories and /tmp and /private/tmp are included in this set. I tried to reproduce the failure with a genrule: genrule(name = "write_to_tmp", outs = ["out.txt"], cmd = "touch /tmp/something.sock && touch $@"), which works fine and creates the file /tmp/something.sock.

I think in general using /tmp should work fine, although it does seem to be a bit unusual on macOS. $TMPDIR is set to a user-specific folder with a randomized name underneath /var/folders by the OS and it seems to be generally encouraged to use that instead of /tmp. But if you know what you're doing, I don't see a real problem.

Note that we don't have tmpfs or similar mechanisms available on macOS, so we can't automatically guarantee that your usage of writable folders like /tmp by actions is hermetic, won't leak state between runs or that file names won't conflict. Make sure to generate file names in a secure way via mkstemp or similar.

Upvotes: 3

Related Questions