{"record":{"id":"4704421bdfc877b2","repo":"elastic/elasticsearch","slug":"duplicate-version-constants-v1","errorCode":null,"errorMessage":"Duplicate version constants {v1}","messagePattern":"Duplicate version constants (.+?)","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/release/UpdateVersionsTask.java","lineNumber":139,"sourceCode":"        }\n    }\n\n    @VisibleForTesting\n    static Optional<CompilationUnit> addVersionConstant(CompilationUnit versionJava, Version version, boolean updateCurrent) {\n        String newFieldName = toVersionField(version);\n\n        ClassOrInterfaceDeclaration versionClass = versionJava.getClassByName(\"Version\").get();\n        if (versionClass.getFieldByName(newFieldName).isPresent()) {\n            LOGGER.lifecycle(\"New version constant [{}] already present, skipping\", newFieldName);\n            return Optional.empty();\n        }\n\n        NavigableMap<Version, FieldDeclaration> versions = versionClass.getFields()\n            .stream()\n            .map(f -> Map.entry(f, parseVersionField(f.getVariable(0).getNameAsString())))\n            .filter(e -> e.getValue().isPresent())\n            .collect(Collectors.toMap(e -> e.getValue().get(), Map.Entry::getKey, (v1, v2) -> {\n                throw new IllegalArgumentException(\"Duplicate version constants \" + v1);\n            }, TreeMap::new));\n\n        // find the version this should be inserted after\n        var previousVersion = versions.lowerEntry(version);\n        if (previousVersion == null) {\n            throw new IllegalStateException(String.format(\"Could not find previous version to [%s]\", version));\n        }\n        FieldDeclaration newVersion = createNewVersionConstant(\n            previousVersion.getValue(),\n            newFieldName,\n            String.format(\"%d_%02d_%02d_99\", version.getMajor(), version.getMinor(), version.getRevision())\n        );\n        versionClass.getMembers().addAfter(newVersion, previousVersion.getValue());\n\n        if (updateCurrent) {\n            versionClass.getFieldByName(\"CURRENT\")\n                .orElseThrow(() -> new IllegalArgumentException(\"Could not find CURRENT constant\"))\n                .getVariable(0)","sourceCodeStart":121,"sourceCodeEnd":157,"githubUrl":"https://github.com/elastic/elasticsearch/blob/db6a809a667c081ca1dc7500389d26975573215f/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/release/UpdateVersionsTask.java#L121-L157","documentation":"Thrown inside the Collectors.toMap merge function in UpdateVersionsTask while scanning Version.java field declarations: two distinct fields both parsed to the same Version value. This indicates a real duplication defect in the Version constants source file.","triggerScenarios":"Version.java contains two field constants whose names parse via VERSION_FIELD to the same (major, minor, revision, qualifier). A merge in toMap then hits the collision merger.","commonSituations":"A botched merge introduced a duplicate version constant; a constant was renamed but the old one was not removed; the naming convention (V_X_YY_ZZ) collided for two qualifiers.","solutions":["Open server/.../Version.java and find the two constants whose parsed version collides; remove or rename the erroneous one.","Run parseVersionField logic mentally on each field name to spot the collision.","Re-run updateVersions after fixing the source file."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// Scan Version.java for duplicate parsed versions before running the task\nPattern VF = Pattern.compile(\"V_(\\\\d+)_(\\\\d+)_(\\\\d+)_(\\\\d+)\");\nMap<String, Long> counts = Files.readAllLines(versionJava)\n    .stream().map(VF::matcher).filter(Matcher::find)\n    .map(m -> m.group(1)+\".\"+m.group(2)+\".\"+m.group(3)+\"(\"+m.group(4)+\")\")\n    .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));\nMap<String,Long> dups = counts.entrySet().stream().filter(e -> e.getValue() > 1)\n    .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));\nif (!dups.isEmpty()) throw new IllegalStateException(\"Duplicate version constants: \" + dups);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["When renaming a version constant, delete the old field in the same commit.","Add a CI check that asserts each parsed version in Version.java is unique."],"tags":["release-tooling","version-constants","duplicate","source-defect","data-integrity"],"analyzedSha":"db6a809a667c081ca1dc7500389d26975573215f","analyzedAt":"2026-08-12T01:39:14.192Z","schemaVersion":2},"datasetVersion":"2026-08-12T06:17:24.410Z"}