bazelbuild/bazel · error · OptionProcessorException

Can't set an option to accumulate multiple values and let it

Error message

Can't set an option to accumulate multiple values and let it expand to other flags.

What it means

This compile-time error from Bazel's options annotation processor fires when an option with allowMultiple = true is also an expansion option or has implicit requirements. Repeated (accumulating) options cannot also rewrite the command line, because each occurrence would expand or add requirements in ways the parser cannot accumulate coherently.

Source

Thrown at src/main/java/com/google/devtools/common/options/processor/OptionsClassProcessor.java:445

              + annotation.category()
              + "\" is disallowed, see OptionMetadataTags for the relevant tags.");
    }
  }

  private void checkExpansionOptions(ExecutableElement method) throws OptionProcessorException {
    Option annotation = method.getAnnotation(Option.class);
    boolean isExpansion = annotation.expansion().length > 0;
    boolean hasImplicitRequirements = annotation.implicitRequirements().length > 0;

    if (isExpansion && hasImplicitRequirements) {
      throw new OptionProcessorException(
          method,
          "Can't set an option to be both an expansion option and have implicit requirements.");
    }

    if (isExpansion || hasImplicitRequirements) {
      if (annotation.allowMultiple()) {
        throw new OptionProcessorException(
            method,
            "Can't set an option to accumulate multiple values and let it expand to other flags.");
      }
    }
  }

  private void checkNoDefaultValueForMultipleOption(ExecutableElement method)
      throws OptionProcessorException {
    Option annotation = method.getAnnotation(Option.class);
    if (annotation.allowMultiple()
        && !annotation.defaultValue().equals("null")
        && !ImmutableList.of("runs_per_test", "flaky_test_attempts").contains(annotation.name())) {
      throw new OptionProcessorException(
          method,
          "Default values for multiple options are not allowed - use \"null\" special value");
    }
  }

View on GitHub (pinned to e6e199d060)

Solutions

  1. Decide the option's role: for expansion/implicit-requirement flags, remove allowMultiple (they should be Void-typed single-occurrence options).
  2. For repeatable value-collection options, remove the expansion and implicitRequirements attributes and let each --flag=value occurrence append to the List.
  3. If you need both behaviors, split into two options: one repeatable plain option and one expansion option referencing it.
  4. Recompile to verify.

Example fix

// before
@Option(
  name = "multi_expand",
  defaultValue = "null",
  allowMultiple = true,
  expansion = {"--foo", "--bar"}
)
// after (repeatable plain option; expansion handled by a separate Void option)
@Option(
  name = "multi_expand",
  defaultValue = "null",
  allowMultiple = true,
  documentationCategory = OptionDocumentationCategory.UNDOCUMENTED,
  effectTags = {OptionEffectTag.EAGER}
)
Defensive patterns

Strategy: validation

Validate before calling

static void checkMultipleNotExpanding(boolean allowMultiple,
                                      String[] expansion,
                                      String[] implicitRequirements) {
  if (allowMultiple) {
    Preconditions.checkState(expansion.length == 0 && implicitRequirements.length == 0,
        "allowMultiple options cannot expand or have implicit requirements");
  }
}

Prevention

When it happens

Trigger: An @Option with allowMultiple = true where expansion.length > 0 or implicitRequirements.length > 0.

Common situations: Taking a single-value expansion flag and switching it to allowMultiple so users can pass it repeatedly; adding expansion to an existing list-type option to alias several flags; converting a comma-separated option into a repeatable one without removing its expansion.

Related errors


AI-assisted analysis of bazelbuild/bazel@e6e199d060 (2026-08-14). Data as JSON: /api/errors/8f21f503c3fd6fef. Report an issue: GitHub.