bazelbuild / bazelbuild/bazel

Java annotation processors do not have access to resources

Open
#12,964 11 comments 0 reactions 0 assignees View on GitHub
not stale P4 team-Rules-Java type: feature request
Dominant language
Java
Stars
25.8k
Forks
4.6k
Avg merge
2d 16h
Merged PRs (30d)
72

Description

When executing a Java annotation processor, it does not have access to the resources referenced by the originating class.

```
java_library(
name = "processor",
srcs = ...,
)

java_plugin(
name = "processor_plugin",
deps = [":processor"],
# generates_api = 1, # tried with and without this.
)

java_library(
name = "annotation_processor",
exported_plugins = [":processor_plugin"],
exports = [":processor"],
)

java_library(
name = "res",
resources = ...,
)

java_library(
name = "target",
srcs = ...,
deps = [":res"],
)
```

Where `target` contains a class that has an annotation that makes reference to a resource in `:res`.

The resources are attempted to be loaded doing
```
processingEnv.getFiler().getResource(StandardLocation.CLASS_OUTPUT, "", fileName);
```
and if that fails, with
```
processingEnv.getFiler().getResource(StandardLocation.CLASS_PATH, "", fileName);
```

At first, it looks like the issue was that in the classpath there were only java headers/interfaces present, so tried with
- Adding `--nojava_header_compilation --nouse_ijars`
- Adding `--nojava_header_compilation --nouse_ijars` and creating a dummy class in `:res`
None worked.

Using bazel release 4.0.0-homebrew

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the example with Bazel 4.0.0 and the annotation processor calls to processingEnv.getFiler().getResource at CLASS_OUTPUT and CLASS_PATH. Investigate how Java annotation processors receive resources from originating classes; done means the processor can load the resource from :res when target depends on it, with coverage for the reported setup.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
build-system
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.