-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Java editor shows compile errors for Hamcrest/JUnit references in Java tests #43
Comments
This is caused by a known issue in Bazel. The issue can be 'fixed' by setting this in your .bazelrc and running bazel test in your command line build: test --explicit_java_test_deps=true Setting that property eliminates implicit deps, and thus requires hamcrest and junit to be explicitly included in the BUILD file where needed. When you do this, there is an explicit dependency on those libs, and so BEF knows to add it to the project classpath in Eclipse. Then JDT will not show red squigglies in the editor. This is where the Bazel code has that option: I didn't track down exactly where those deps are added, but the flag meanders through this code path: https://github.com/bazelbuild/bazel/blob/master/src/main/java/com/google/devtools/build/lib/bazel/rules/java/BazelJavaSemantics.java#L454 |
I have decided that I am going to document this as a known limitation of package import, and not do anything automatic in BEF to correct this configuration. There are workarounds below. How would BEF know what version of junit or hamcrest libs to add? I feel like Bazel is doing the wrong thing here, and I don't want to build BEF logic to accommodate it. Hopefully explicit_java_test_deps will be set to true by default in the future. Workaround #1: Fix your BUILD files Workaround #2: Manually fix up your classpath in Eclipse after import
|
I just requested that we switch the default to disable implicit deps on Bazel discuss: If that happens, then this becomes a short-term issue and I just doc it like suggested above, or provide a hacky short term solution until people transition to Bazel 3.0. If it looks like implicit deps will remain the default long term, we will need to figure out how to determine which version of these deps to add to each project that needs them. |
If we implement a hack solution, the way the IJ plugin solves this is by adding the Runner_deploy.jar (actually, Runner_deploy-ijar.jar) to the classpath which is seen in bazel query as: @bazel_tools//tools/jdk:TestRunner with relative path from bazel-bin: But to be correct, this should only be added if: --explicit_java_test_deps=false |
I am working on adding the hack, but in the meantime have added doc discouraging implicit deps: |
added the hack, will write tests next |
Over a series of commits, this feature was implemented. User impact:
To see what entries the BEF has supplied to the JDT classpath for a project, do the following:
Implementation Details: |
Repro:
Create a java_test rule in a BUILD file with a valid junit test
do NOT add hamcrest as a dep
add this line to the test method:
org.hamcrest.core.StringContains.containsString("test");
bazel test //... (your test should succeed)
in BEF, open the test .java file in the editor, and the hamcrest line will be red squigglied
There are some implicit deps that the command line test runner adds to the classpath, that BEF needs to imply for packages that contain java_test targets. Figure out what they are, and add them.
The text was updated successfully, but these errors were encountered: