Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

External parent versions being overridden when using IncrementProjectVersion #4607

Open
ammachado opened this issue Oct 24, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@ammachado
Copy link
Contributor

What version of OpenRewrite are you using?

I am using

  • OpenRewrite v8.38.0
  • Maven/Gradle plugin v5.43.0

How are you running OpenRewrite?

I am using the Maven plugin, and my project is a multi module project.

What is the smallest, simplest way to reproduce the problem?

import org.intellij.lang.annotations.Language;
import org.jspecify.annotations.Nullable;
import org.junit.jupiter.api.Test;
import org.openrewrite.InMemoryExecutionContext;
import org.openrewrite.maven.MavenExecutionContextView;
import org.openrewrite.maven.cache.MavenPomCache;
import org.openrewrite.maven.tree.GroupArtifactVersion;
import org.openrewrite.maven.tree.MavenMetadata;
import org.openrewrite.maven.tree.MavenRepository;
import org.openrewrite.maven.tree.Pom;
import org.openrewrite.maven.tree.ResolutionEventListener;
import org.openrewrite.maven.tree.ResolvedGroupArtifactVersion;
import org.openrewrite.maven.tree.ResolvedPom;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

import java.net.URI;
import java.util.Collections;
import java.util.List;

import static org.openrewrite.java.Assertions.mavenProject;
import static org.openrewrite.maven.Assertions.pomXml;

class MultiModulePomTest implements RewriteTest {

    @Override
    public void defaults(RecipeSpec spec) {
        MavenExecutionContextView ctx = new MavenExecutionContextView(new InMemoryExecutionContext())
          .setAddLocalRepository(true);

        setupExecutionContext(ctx);

        //language=YAML
        spec.executionContext(ctx)
          .recipeFromYaml("""
              type: specs.openrewrite.org/v1beta/recipe
              name: test-recipe
              description: test-recipe.
              recipeList:
                - org.openrewrite.maven.IncrementProjectVersion:
                    groupId: '*'
                    artifactId: '*'
                    digit: MINOR
                - org.openrewrite.maven.ChangeParentPom:
                    oldGroupId: my.company
                    oldArtifactId: old-parent
                    newGroupId: my.company
                    newArtifactId: new-parent
                    newVersion: 2.0.0
              """, "test-recipe");
    }

    @Test
    void incrementVersionAndUpgradeParent() {
        rewriteRun(
          mavenProject("jar", pomXml(JAR_POM_BEFORE, JAR_POM_AFTER)),
          pomXml(PARENT_POM_BEFORE, PARENT_POM_AFTER)
        );
    }

    @Language("xml")
    private static final String PARENT_POM_BEFORE = """
      <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
          <modelVersion>4.0.0</modelVersion>
          <parent>
              <groupId>my.company</groupId>
              <artifactId>old-parent</artifactId>
              <version>1.0.1</version>
          </parent>
          <groupId>my.company</groupId>
          <artifactId>my-service-parent</artifactId>
          <version>10.0.0-SNAPSHOT</version>
          <packaging>pom</packaging>
          <modules>
              <module>jar</module>
          </modules>
      </project>
      """;

    @Language("xml")
    private static final String PARENT_POM_AFTER = """
      <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
          <modelVersion>4.0.0</modelVersion>
          <parent>
              <groupId>my.company</groupId>
              <artifactId>new-parent</artifactId>
              <version>2.0.0</version>
          </parent>
          <groupId>my.company</groupId>
          <artifactId>my-service-parent</artifactId>
          <version>10.1.0-SNAPSHOT</version>
          <packaging>pom</packaging>
          <modules>
              <module>jar</module>
          </modules>
      </project>
      """;

    @Language("xml")
    private static final String JAR_POM_BEFORE = """
      <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
          <modelVersion>4.0.0</modelVersion>
          <parent>
              <groupId>my.company</groupId>
              <artifactId>my-service-parent</artifactId>
              <version>10.0.0-SNAPSHOT</version>
              <relativePath>../pom.xml</relativePath>
          </parent>
          <artifactId>my-service-jar</artifactId>
      </project>
      """;

    @Language("xml")
    private static final String JAR_POM_AFTER = """
      <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
          <modelVersion>4.0.0</modelVersion>
          <parent>
              <groupId>my.company</groupId>
              <artifactId>my-service-parent</artifactId>
              <version>10.1.0-SNAPSHOT</version>
              <relativePath>../pom.xml</relativePath>
          </parent>
          <artifactId>my-service-jar</artifactId>
      </project>
      """;

    private static void setupExecutionContext(MavenExecutionContextView ctx) {
        MavenPomCache pomCache = ctx.getPomCache();
        MavenRepository localRepository = ctx.getLocalRepository();

        createParentPom(pomCache, new ResolvedGroupArtifactVersion(localRepository.getUri(), "my.company", "old-parent", "1.0.1", "1.0.1"));
        ResolvedGroupArtifactVersion rgav = new ResolvedGroupArtifactVersion(localRepository.getUri(), "my.company", "new-parent", "2.0.0", "2.0.0");
        createParentPom(pomCache, rgav);

        MavenMetadata mavenMetadata = new MavenMetadata(new MavenMetadata.Versioning(Collections.singletonList("2.0.0"), null, null, null));
        pomCache.putMavenMetadata(URI.create(localRepository.getUri()), new GroupArtifactVersion(rgav.getGroupId(), rgav.getArtifactId(), null), mavenMetadata);
    }

    private static void createParentPom(MavenPomCache pomCache, ResolvedGroupArtifactVersion rgav) {
        pomCache.putPom(rgav, Pom.builder().gav(rgav).packaging("pom").build());
    }
}

What did you expect to see?

No errors.

What did you see instead?

diff --git a/pom.xml b/pom.xml
index cfa878b..214f4b6 100644
--- a/pom.xml
+++ b/pom.xml
@@ -2,8 +2,8 @@ 
     <modelVersion>4.0.0</modelVersion>
     <parent>
         <groupId>my.company</groupId>
-        <artifactId>new-parent</artifactId>
-        <version>2.0.0</version>
+        <artifactId>old-parent</artifactId>
+        <version>10.1.0-SNAPSHOT</version>
     </parent>
     <groupId>my.company</groupId>
     <artifactId>my-service-parent</artifactId>

What is the full stack trace of any errors you encountered?

N/A

Are you interested in contributing a fix to OpenRewrite?

Yes, I am.

@ammachado ammachado added the bug Something isn't working label Oct 24, 2024
@ammachado
Copy link
Contributor Author

I'm still testing, but I could isolate the problem on the IncrementProjectVersion recipe. It is a scanning recipe, the scanner is considering the project tag (https://github.com/openrewrite/rewrite/blob/main/rewrite-maven/src/main/java/org/openrewrite/maven/IncrementProjectVersion.java#L92-L94) to identify which versions should be updated, but the visitor is changing versions on both project and parent: https://github.com/openrewrite/rewrite/blob/main/rewrite-maven/src/main/java/org/openrewrite/maven/IncrementProjectVersion.java#L168-L171. This is probably to support multiple maven projects, but in my scenario, the parent project itself has an external parent, and it's version is being updated incorrectly.

@ammachado ammachado changed the title Dependency versions being overriden using ChangeParentPom External parent versions being overridden when using IncrementProjectVersion Oct 24, 2024
@ammachado
Copy link
Contributor Author

ammachado commented Oct 24, 2024

This unit test also demonstrates issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Status: Backlog
Development

No branches or pull requests

1 participant