{"record":{"id":"0027783899c222c7","repo":"bazelbuild/bazel","slug":"unable-to-find-the-type-of-annotation-s","errorCode":null,"errorMessage":"Unable to find the type of annotation %s.","messagePattern":"Unable to find the type of annotation (.+?)\\.","errorType":"validation","errorClass":"OptionProcessorException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/google/devtools/common/options/processor/ProcessorUtils.java","lineNumber":41,"sourceCode":"import javax.lang.model.type.DeclaredType;\nimport javax.lang.model.type.TypeMirror;\nimport javax.lang.model.util.Elements;\nimport javax.lang.model.util.Types;\n\n/** Convenient utilities for dealing with the javax.lang.model types. */\npublic class ProcessorUtils {\n\n  /** Return the AnnotationMirror for the annotation of the given type on the element provided. */\n  static AnnotationMirror getAnnotation(\n      Elements elementUtils,\n      Types typeUtils,\n      Element element,\n      Class<? extends Annotation> annotation)\n      throws OptionProcessorException {\n    TypeElement annotationElement = elementUtils.getTypeElement(annotation.getCanonicalName());\n    if (annotationElement == null) {\n      // This can happen if the annotation is on the -processorpath but not on the -classpath.\n      throw new OptionProcessorException(\n          element, \"Unable to find the type of annotation %s.\", annotation);\n    }\n    TypeMirror annotationMirror = annotationElement.asType();\n\n    for (AnnotationMirror annot : element.getAnnotationMirrors()) {\n      if (typeUtils.isSameType(annot.getAnnotationType(), annotationMirror)) {\n        return annot;\n      }\n    }\n    // No annotation of this requested type found.\n    throw new OptionProcessorException(\n        element, \"No annotation %s found for this element.\", annotation);\n  }\n\n  /**\n   * Returns the contents of a {@code Class}-typed field in an annotation.\n   *\n   * <p>Taken & adapted from AutoValueProcessor.java","sourceCodeStart":23,"sourceCodeEnd":59,"githubUrl":"https://github.com/bazelbuild/bazel/blob/e6e199d0601a244511b4cf18c8b2828aa73db1fd/src/main/java/com/google/devtools/common/options/processor/ProcessorUtils.java#L23-L59","documentation":"ProcessorUtils.getAnnotation throws this when the annotation processor cannot resolve the TypeElement for a requested annotation class — classpath visibility is broken. Concretely, the comment in the source notes this happens when the annotation type is on the processor's -processorpath but not on the compile -classpath, so the compiler's element utilities return null for it.","triggerScenarios":"Compiling options classes where com.google.devtools.common.options.Option (or another annotation the processor inspects) is resolved by the processor but absent from the compilation classpath — mismatched -processorpath/-classpath, an annotation JAR shaded differently between the two, or a broken toolchain/IDE annotation-processing classpath setup.","commonSituations":"Building with a custom javac invocation that puts options libraries only on -processorpath; IDE (IntelliJ/Eclipse) annotation-processing configuration diverging from Bazel's; upgrading the options library so processor and annotation come from different versions; migrating a build to Bazel with half-configured javac_opts.","solutions":["Ensure the JAR containing the annotation (e.g. com.google.devtools.common.options.Option) is on the compile classpath of the module being compiled, not only on -processorpath.","Prefer building via Bazel (bazel build //src:bazel-dev and dependent targets) so processor/classpath deps are wired consistently rather than hand-rolled javac flags.","Check for version skew: the annotation seen by the processor and by javac must come from the same library version.","In IDEs, re-import/refresh the Bazel project so annotation processor paths regenerate."],"exampleFix":"// before: custom javac with annotation only on processor path\njavac -processorpath lib/options-processor.jar \\\n      -classpath app.jar \\\n      src/MyOptions.java   // Option class not on -classpath -> error\n// after: put the annotations jar on the compile classpath too\njavac -processorpath lib/options-processor.jar \\\n      -classpath app.jar:lib/options-annotations.jar \\\n      src/MyOptions.java","handlingStrategy":"validation","validationCode":"// Build-time sanity: verify the annotation class is resolvable on the compile classpath\nstatic boolean annotationOnCompileClasspath(ClassLoader compileCpLoader,\n                                             String annotationFqn) {\n  try {\n    Class.forName(annotationFqn, false, compileCpLoader);\n    return true;\n  } catch (ClassNotFoundException e) {\n    return false;\n  }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep annotation JARs and their processors on the same classpath/version; put annotations on -classpath, processors on -processorpath.","Build via Bazel rather than hand-rolled javac so these paths stay consistent.","After IDE or dependency upgrades, do a clean compile of options classes to catch classpath skew early."],"tags":["java","bazel","annotation-processing","classpath","build-configuration","toolchain"],"backgroundTag":null,"analyzedSha":"e6e199d0601a244511b4cf18c8b2828aa73db1fd","analyzedAt":"2026-08-14T10:24:27.848Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}