elastic/elasticsearch · error · IllegalStateException

Could not find previous version to [%s]

Error message

Could not find previous version to [%s]

What it means

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.

Source

Thrown at build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/release/UpdateVersionsTask.java:145

        ClassOrInterfaceDeclaration versionClass = versionJava.getClassByName("Version").get();
        if (versionClass.getFieldByName(newFieldName).isPresent()) {
            LOGGER.lifecycle("New version constant [{}] already present, skipping", newFieldName);
            return Optional.empty();
        }

        NavigableMap<Version, FieldDeclaration> versions = versionClass.getFields()
            .stream()
            .map(f -> Map.entry(f, parseVersionField(f.getVariable(0).getNameAsString())))
            .filter(e -> e.getValue().isPresent())
            .collect(Collectors.toMap(e -> e.getValue().get(), Map.Entry::getKey, (v1, v2) -> {
                throw new IllegalArgumentException("Duplicate version constants " + v1);
            }, TreeMap::new));

        // find the version this should be inserted after
        var previousVersion = versions.lowerEntry(version);
        if (previousVersion == null) {
            throw new IllegalStateException(String.format("Could not find previous version to [%s]", version));
        }
        FieldDeclaration newVersion = createNewVersionConstant(
            previousVersion.getValue(),
            newFieldName,
            String.format("%d_%02d_%02d_99", version.getMajor(), version.getMinor(), version.getRevision())
        );
        versionClass.getMembers().addAfter(newVersion, previousVersion.getValue());

        if (updateCurrent) {
            versionClass.getFieldByName("CURRENT")
                .orElseThrow(() -> new IllegalArgumentException("Could not find CURRENT constant"))
                .getVariable(0)
                .setInitializer(new NameExpr(newFieldName));
        }

        return Optional.of(versionJava);
    }

View on GitHub (pinned to db6a809a66)

Solutions

  1. Confirm the --add-version is actually newer than the lowest existing constant; correct it if wrong.
  2. If intentionally adding an older version, the insertion logic must be extended to handle insertion at the head (no previousVersion).
  3. Verify the version string format so it sorts as expected.
Defensive patterns

Strategy: validation

Validate before calling

// Ensure the version to add sorts above the lowest existing constant
Version lowest = parseVersionField(/* scan Version.java fields */).stream().min(Comparator.naturalOrder()).orElseThrow();
if (newVersion.compareTo(lowest) <= 0) {
    throw new GradleException("Cannot add " + newVersion + "; it is not newer than the lowest existing constant " + lowest);
}

Prevention

When it happens

Trigger: 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).

Common situations: Backporting a very old version; the Version.java file was pruned of older constants; a malformed version string sorted incorrectly.

Related errors


AI-assisted analysis of elastic/elasticsearch@db6a809a66 (2026-08-12). Data as JSON: /api/errors/2837b0e19cd19005. Report an issue: GitHub.