Jhinrichsen
Jhinrichsen

Reputation: 1

Can java_library.data runtime location be changed within bazel?

On my way to migrate an existing build to bazel, i have a submodule mod1 that has some JUnit tests reading files from a "testdata" directory. When trying to load those files, i have to use "mod1/testdata/test.txt" instead of "testdata/test.txt", i.e. the unit tests have to be aware of their corresponding bazel module directory.

(1) Is this the correct behaviour for bazel 0.23.2@debian and 0.23.2-homebrew?

(2) Is there a way to use the .java tests without changes, and to remove the need for a "mod1" prefix in bazel data/ runfiles?

My sample project is here: https://gitlab.com/jhinrichsen/bazel-data-test. I am looking for a way to use the same path "testdata/test.txt" for both root module and submodule. In my example project, bazel test AllTests suceeds, while bazel test mod1/AllTests fails because i need to prepend "mod1/" to "testdata/test.txt".

Not looking for a resources/classpath based solution as i cannot modify the existing test sources.

Upvotes: 0

Views: 199

Answers (1)

Rohan Singh
Rohan Singh

Reputation: 21475

The behavior that you are seeing is indeed the correct behavior, and there is no way to strip the "mod1" prefix with the native Java rules. Anything you include with data will be scoped to its own package in the way you're seeing.

The reason for this is pretty straightforward. Let's say that your test target, //mod1:AllTests, also depended on a hypothetical //mod2:tests library. And let's say that hypothetical library also had a testdata/test.txt as a data dependency. The multiple test.txt files would conflict unless they were namespaced to their packages.

If you absolutely cannot modify the test source at all, then you are pretty much stuck. Here's a previous discussion about this: https://groups.google.com/forum/#!topic/bazel-discuss/w6TDwSZvN0k

I would recommend if you're trying to work with Bazel, you accept the concept of runfiles and modify your tests to either work with the runfiles structure, or accept a command-line argument for where to find the test data.

Upvotes: 0

Related Questions