{"record":{"id":"b2c71e5738d1ca57","repo":"bazelbuild/bazel","slug":"malformed-value-of-invocation-policy","errorCode":null,"errorMessage":"Malformed value of --invocation_policy: ","messagePattern":"Malformed value of --invocation_policy: ","errorType":"exception","errorClass":"OptionsParsingException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/google/devtools/common/options/InvocationPolicyParser.java","lineNumber":52,"sourceCode":"   *     --invocation_policy is invalid.\n   */\n  public static InvocationPolicy parsePolicy(String policy) throws OptionsParsingException {\n    if (Strings.isNullOrEmpty(policy)) {\n      return InvocationPolicy.getDefaultInstance();\n    }\n\n    try {\n      try {\n        // First try decoding the policy as a base64 encoded binary proto.\n        return InvocationPolicy.parseFrom(\n            BaseEncoding.base64().decode(CharMatcher.whitespace().removeFrom(policy)));\n      } catch (IllegalArgumentException e) {\n        // If the flag value can't be decoded from base64, try decoding the policy as a text\n        // formatted proto.\n        return TextFormat.parse(policy, InvocationPolicy.class);\n      }\n    } catch (InvalidProtocolBufferException | TextFormat.ParseException e) {\n      throw new OptionsParsingException(\"Malformed value of --invocation_policy: \" + policy, e);\n    }\n  }\n}\n","sourceCodeStart":34,"sourceCodeEnd":56,"githubUrl":"https://github.com/bazelbuild/bazel/blob/e6e199d0601a244511b4cf18c8b2828aa73db1fd/src/main/java/com/google/devtools/common/options/InvocationPolicyParser.java#L34-L56","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["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","If using text format, check field names against invocation_policy.proto and fix typos/quotes","If passing binary, ensure the value is BaseEncoding.base64() of the serialized proto with whitespace removed","Print the exact flag value received (it is echoed in the exception message) to spot shell escaping issues"],"exampleFix":"# before\nbazel --invocation_policy='flag_value { name: \"compilation_mode\" value: \"opt\" }' build\n# after (valid text format uses set_value)\nbazel --invocation_policy='flag_settings { flag: \"compilation_mode\" set_value: \"opt\" }' build","handlingStrategy":"validation","validationCode":"// Java: validate an invocation policy before passing it to Bazel\nimport com.google.protobuf.TextFormat;\nimport com.google.devtools.build.lib.runtime.InvocationPolicy;\n\nboolean isValidPolicy(String policy) {\n  try {\n    InvocationPolicy.Builder b = InvocationPolicy.newBuilder();\n    TextFormat.merge(policy, b);\n    b.build(); // throws on unknown fields / type mismatches\n    return true;\n  } catch (Exception e) {\n    return false;\n  }\n}","typeGuard":null,"tryCatchPattern":"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.","preventionTips":["Generate policies programmatically via the proto builder and pass base64, never hand-edit strings","Keep the invocation_policy.proto schema version in lockstep with the Bazel version under test","Pipe policies through protoc/text-format validation in CI before deployment"],"tags":["bazel","options","proto","invocation-policy","flag-parsing"],"backgroundTag":null,"analyzedSha":"e6e199d0601a244511b4cf18c8b2828aa73db1fd","analyzedAt":"2026-08-14T10:24:27.848Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}