Runfiles from multiple targets in a single directory in Bazel

Viewed 257

I have an integration test target which uses runfiles from two different targets:

java_test(
  name = "test",
  srcs = ["Test.java"],
  data = ["//package:resource1", "//package:resource2"],
  ...
)

The first one is a filegroup:

filegroup(
    name = "resource1",
    srcs = glob(
        ["test-resources/sample-repo/**"],
        exclude_directories = 0,
    ),
    visibility = ["//visibility:public"],
)

and the second one is a genrule:

genrule(
    name = "resource2",
    srcs = [],
    outs = [
        "test-resources/sample-repo/file1",
        "test-resources/sample-repo/file2",
    ],
    cmd = "$(location //package:tool) -d $(RULEDIR)/test-resources/sample-repo",
    tools = ["//package:tool"],
    visibility = ["//visibility:public"],
)

The test requires the files obtained from both the filegroup and the genrule to be placed in a single directory (let's say: test-resources/sample-repo).

However, apparently it is not possible to achieve that using the method described above:

WARNING: [path to my repo]/BUILD:8:17: runfiles symlink test/test-resources/sample-repo/file1 -> bazel-out/k8-fastbuild/bin/test/test-resources/sample-repo/file1 obscured by test/test-resources/sample-repo -> test/test-resources/sample-repo
WARNING: [path to my repo]/BUILD:8:17: runfiles symlink test/test-resources/sample-repo/file2 -> bazel-out/k8-fastbuild/bin/test/test-resources/sample-repo/file2 obscured by test/test-resources/sample-repo -> test/test-resources/sample-repo

My last resort would be copying the files at runtime, but I'd rather avoid it. Is there any way to gather runfiles from multiple targets in a single directory?

1 Answers

Try omitting exclude_directories = 0 (was that needed for something else?).

With exclude_directories = 0 Bazel will symlink directories in the runfiles tree. In the case of the filegroup, Bazel creates a symlink to the directory in the source tree (test-resources/sample-repo). But then it also needs to symlink the generated files from the output directory into the same spot in the runfiles tree -- but that spot is a symlink. So there's no where to put those symlinks (besides the source tree itself... but Bazel by design doesn't modify the source tree).

Related