{"record":{"id":"2837b0e19cd19005","repo":"elastic/elasticsearch","slug":"could-not-find-previous-version-to-s","errorCode":null,"errorMessage":"Could not find previous version to [%s]","messagePattern":"Could not find previous version to \\[(.+?)\\]","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/release/UpdateVersionsTask.java","lineNumber":145,"sourceCode":"\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)\n                .setInitializer(new NameExpr(newFieldName));\n        }\n\n        return Optional.of(versionJava);\n    }\n","sourceCodeStart":127,"sourceCodeEnd":163,"githubUrl":"https://github.com/elastic/elasticsearch/blob/db6a809a667c081ca1dc7500389d26975573215f/build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/release/UpdateVersionsTask.java#L127-L163","documentation":"Thrown as IllegalStateException in UpdateVersionsTask.addVersionConstant() when versions.lowerEntry(version) returns null — meaning the version being added sorts below every existing version constant, so there is no predecessor to insert after.","triggerScenarios":"Attempting to add a version constant whose Version is lower than all existing constants in Version.java (e.g. adding an 7.x constant into a tree whose lowest is 8.0).","commonSituations":"Backporting a very old version; the Version.java file was pruned of older constants; a malformed version string sorted incorrectly.","solutions":["Confirm the --add-version is actually newer than the lowest existing constant; correct it if wrong.","If intentionally adding an older version, the insertion logic must be extended to handle insertion at the head (no previousVersion).","Verify the version string format so it sorts as expected."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// Ensure the version to add sorts above the lowest existing constant\nVersion lowest = parseVersionField(/* scan Version.java fields */).stream().min(Comparator.naturalOrder()).orElseThrow();\nif (newVersion.compareTo(lowest) <= 0) {\n    throw new GradleException(\"Cannot add \" + newVersion + \"; it is not newer than the lowest existing constant \" + lowest);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Only add monotonically newer versions; for historical versions, extend addVersionConstant to support head insertion.","Verify the version string format so it sorts correctly."],"tags":["release-tooling","version-constants","ordering","precondition"],"backgroundTag":null,"analyzedSha":"db6a809a667c081ca1dc7500389d26975573215f","analyzedAt":"2026-08-12T01:39:14.192Z","schemaVersion":2},"datasetVersion":"2026-08-12T12:31:55.035Z"}