Skip to content

AddProfile is not idempotent: removes and re-adds profile every cycle #7355

@timtebeek

Description

@timtebeek

Problem

org.openrewrite.maven.AddProfile unconditionally removes and re-adds a profile with the same id on every recipe cycle, even when the existing profile already matches the desired state. This makes it non-idempotent.

The relevant code in AddProfile.java:

if (maybeProfile.isPresent()) {
    Xml.Tag profile = maybeProfile.get();
    t = (Xml.Tag) new RemoveContentVisitor(profile, false, false)
        .visitNonNull(t, ctx, getCursor().getParentOrThrow());
}
// Always adds, even if the existing profile was identical
Xml.Tag profileTag = Xml.Tag.build(...);
t = (Xml.Tag) new AddToTagVisitor<>(profiles, profileTag)
    .visitNonNull(t, ctx, getCursor().getParentOrThrow());

Impact

Any recipe using AddProfile will always report changes, even on the second cycle when nothing should change. Tests that use AddProfile need expectedCyclesThatMakeChanges(2) as a workaround.

Suggested Fix

Before removing and re-adding, compare the existing profile tag content to the one being built. If they're equivalent, return early without making changes.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions