bazelbuild/bazel · error · OptionsParsingException

Malformed value of --invocation_policy:

Error message

Malformed value of --invocation_policy: 

What it means

Thrown by InvocationPolicyParser.parse when the value passed to --invocation_policy cannot be interpreted as either a base64-encoded binary InvocationPolicy proto or a text-format InvocationPolicy proto. The parser first strips all whitespace and attempts base64+binary-proto decoding; only if that fails does it fall back to text-format parsing. If both paths raise, the raw policy string is reported as malformed.

Source

Thrown at src/main/java/com/google/devtools/common/options/InvocationPolicyParser.java:52

   *     --invocation_policy is invalid.
   */
  public static InvocationPolicy parsePolicy(String policy) throws OptionsParsingException {
    if (Strings.isNullOrEmpty(policy)) {
      return InvocationPolicy.getDefaultInstance();
    }

    try {
      try {
        // First try decoding the policy as a base64 encoded binary proto.
        return InvocationPolicy.parseFrom(
            BaseEncoding.base64().decode(CharMatcher.whitespace().removeFrom(policy)));
      } catch (IllegalArgumentException e) {
        // If the flag value can't be decoded from base64, try decoding the policy as a text
        // formatted proto.
        return TextFormat.parse(policy, InvocationPolicy.class);
      }
    } catch (InvalidProtocolBufferException | TextFormat.ParseException e) {
      throw new OptionsParsingException("Malformed value of --invocation_policy: " + policy, e);
    }
  }
}

View on GitHub (pinned to e6e199d060)

Solutions

  1. Validate the policy offline first: load it as text-format proto with TextFormat.merge into InvocationPolicy, or use a generated Java/Python proto to serialize and then base64-encode it
  2. If using text format, check field names against invocation_policy.proto and fix typos/quotes
  3. If passing binary, ensure the value is BaseEncoding.base64() of the serialized proto with whitespace removed
  4. Print the exact flag value received (it is echoed in the exception message) to spot shell escaping issues

Example fix

# before
bazel --invocation_policy='flag_value { name: "compilation_mode" value: "opt" }' build
# after (valid text format uses set_value)
bazel --invocation_policy='flag_settings { flag: "compilation_mode" set_value: "opt" }' build
Defensive patterns

Strategy: validation

Validate before calling

// Java: validate an invocation policy before passing it to Bazel
import com.google.protobuf.TextFormat;
import com.google.devtools.build.lib.runtime.InvocationPolicy;

boolean isValidPolicy(String policy) {
  try {
    InvocationPolicy.Builder b = InvocationPolicy.newBuilder();
    TextFormat.merge(policy, b);
    b.build(); // throws on unknown fields / type mismatches
    return true;
  } catch (Exception e) {
    return false;
  }
}

Try / catch

Catch OptionsParsingException at the call site of InvocationPolicyParser.parse and report the invalid flag value plus cause (InvalidProtocolBufferException/ParseException) to the user instead of the raw stack.

Prevention

When it happens

Trigger: Calling InvocationPolicyParser.parse(policy) (or launching Bazel with --invocation_policy=...) with a string that is neither valid base64 of a binary proto nor a valid text-format proto, e.g. a typo'd field name, a policy copied with smart quotes, or binary-proto bytes that are not base64.

Common situations: Hand-writing a text-format invocation policy and misspelling a field; passing a policy generated for a different proto schema/version; feeding raw binary bytes instead of base64; trailing shell interpolation mangling the value.

Understand the failure class

Related errors


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