{"record":{"id":"76ce786e01759a11","repo":"apache/hadoop","slug":"flag-both-append-and-overwrite-options-cannot-be","errorCode":null,"errorMessage":"${flag}Both append and overwrite options cannot be enabled.","messagePattern":"(.+?)Both append and overwrite options cannot be enabled\\.","errorType":"exception","errorClass":"HadoopIllegalArgumentException","httpStatus":null,"severity":"error","filePath":"hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/CreateFlag.java","lineNumber":159,"sourceCode":"    return mode;\n  }\n  \n  /**\n   * Validate the CreateFlag and throw exception if it is invalid\n   * @param flag set of CreateFlag\n   * @throws HadoopIllegalArgumentException if the CreateFlag is invalid\n   */\n  public static void validate(EnumSet<CreateFlag> flag) {\n    if (flag == null || flag.isEmpty()) {\n      throw new HadoopIllegalArgumentException(flag\n          + \" does not specify any options\");\n    }\n    final boolean append = flag.contains(APPEND);\n    final boolean overwrite = flag.contains(OVERWRITE);\n    \n    // Both append and overwrite is an error\n    if (append && overwrite) {\n      throw new HadoopIllegalArgumentException(\n          flag + \"Both append and overwrite options cannot be enabled.\");\n    }\n  }\n  \n  /**\n   * Validate the CreateFlag for create operation\n   * @param path Object representing the path; usually String or {@link Path}\n   * @param pathExists pass true if the path exists in the file system\n   * @param flag set of CreateFlag\n   * @throws IOException on error\n   * @throws HadoopIllegalArgumentException if the CreateFlag is invalid\n   */\n  public static void validate(Object path, boolean pathExists,\n      EnumSet<CreateFlag> flag) throws IOException {\n    validate(flag);\n    final boolean append = flag.contains(APPEND);\n    final boolean overwrite = flag.contains(OVERWRITE);\n    if (pathExists) {","sourceCodeStart":141,"sourceCodeEnd":177,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/CreateFlag.java#L141-L177","documentation":"CreateFlag.validate refuses a set containing both APPEND and OVERWRITE with HadoopIllegalArgumentException (note the message concatenates the flag set straight into 'Both append and overwrite...' with no separator). The two options describe contradictory write modes - resume at end-of-file vs replace from byte zero - so the filesystem cannot honor both.","triggerScenarios":"Passing EnumSet.of(CreateFlag.APPEND, CreateFlag.OVERWRITE) (directly or via a builder/union of two option groups, e.g., flags = EnumSet.of(APPEND); flags.addAll(otherFlags)) to fs.create(...)/fc.create(...) or CreateFlag.validate.","commonSituations":"Combining CLI-style booleans into flags ('-append' and '-overwrite' both given to a tool like distcp/ FsShell wrapper); merging a default flag set with a user-supplied one without removing the conflicting member; copy-pasted flag literals.","solutions":["Pick one mode: EnumSet.of(CreateFlag.APPEND) to keep existing bytes, or EnumSet.of(CreateFlag.CREATE, CreateFlag.OVERWRITE) to replace the file.","If you merge flag sets, resolve the conflict explicitly first: if (merged.contains(APPEND)) merged.remove(OVERWRITE);.","Validate early with CreateFlag.validate(flags) so the error points at your own code, and treat both-set as a usage/configuration error upstream."],"exampleFix":"// before\nEnumSet<CreateFlag> flags = EnumSet.noneOf(CreateFlag.class);\nif (cfg.append()) flags.add(CreateFlag.APPEND);\nif (cfg.overwrite()) flags.add(CreateFlag.OVERWRITE); // both true -> throws\nfs.create(path, perms, flags, 4096, (short)1, 1<<26, null);\n\n// after: overwrite wins, append only when not overwriting\nEnumSet<CreateFlag> flags = cfg.overwrite()\n    ? EnumSet.of(CreateFlag.CREATE, CreateFlag.OVERWRITE)\n    : (cfg.append() ? EnumSet.of(CreateFlag.APPEND)\n                     : EnumSet.of(CreateFlag.CREATE));","handlingStrategy":"validation","validationCode":"if (flags.contains(CreateFlag.APPEND)) flags.remove(CreateFlag.OVERWRITE);\nCreateFlag.validate(flags);","typeGuard":"static boolean validFlagCombo(EnumSet<CreateFlag> f) {\n  return !f.isEmpty() && !(f.contains(CreateFlag.APPEND) && f.contains(CreateFlag.OVERWRITE));\n}","tryCatchPattern":"try {\n  fs.create(path, perms, flags, buf, rep, block, null);\n} catch (HadoopIllegalArgumentException e) { // 'Both append and overwrite...'\n  // resolve to a single mode: keep APPEND, drop OVERWRITE (or vice versa), retry\n}","preventionTips":["Never union user-supplied flags with defaults without removing the conflicting member.","Expose one 'mode' knob (create|overwrite|append) in your API instead of independent booleans."],"tags":["hadoop","filesystem","create-flag","validation","conflicting-options"],"backgroundTag":"invalid-create-flag","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}