openrewrite / rewrite-apache

OpenRewrite recipes for Apache projects.
Apache License 2.0
2 stars 5 forks source link

Running `UpgradeApacheHttpClient_5` results in `IllegalArgumentException` #3

Open rickie opened 10 months ago

rickie commented 10 months ago

What version of OpenRewrite are you using?

I am using

How are you running OpenRewrite?

 mvn -U org.openrewrite.maven:rewrite-maven-plugin:run \
  -Drewrite.recipeArtifactCoordinates=org.openrewrite.recipe:rewrite-spring:RELEASE,org.openrewrite.recipe:rewrite-migrate-java:RELEASE \
  -Drewrite.activeRecipes=org.openrewrite.java.apache.httpclient5.UpgradeApacheHttpClient_5

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

<plugin>
      <groupId>org.openrewrite.maven</groupId>
      <artifactId>rewrite-maven-plugin</artifactId>
      <version>5.4.2</version>
  </plugin>

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

I have a simple project that contains this in the pom.xml.

    <dependencies>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.14</version>
        </dependency>
    </dependencies>

And a simple Java class that contains the following:

import org.apache.http.client.config.RequestConfig;

class Test {
  private void createHttpClient() {
    RequestConfig config =
        RequestConfig.custom()
            .setConnectTimeout(1000)
            .setConnectionRequestTimeout(1000)
            .setSocketTimeout(1000)
            .build();
  }
}

What did you expect to see?

I expect that the build doesn't crash or throws an exception.

For most of our internal repositories at Picnic this recipe works fine, but in some cases it doesn't. Therefore I made a reproduction case to try and identify what is going wrong. Do you have an idea?

What did you see instead?

An error is shown instead.

What is the stack trace of any errors you encountered?

[ERROR] Failed to execute goal org.openrewrite.maven:rewrite-maven-plugin:5.4.2:run (default-cli) on project openrewrite-httpclient-reproduction:
Execution default-cli of goal org.openrewrite.maven:rewrite-maven-plugin:5.4.2:run failed:
Error while visiting src/main/java/tech/picnic/openrewrite/Test.java:
java.lang.IllegalArgumentException: Unable to find runtime dependencies beginning with: 'httpclient5', 'httpcore5',
classpath: [jrt:/com.azul.crs.client, jrt:/com.azul.tooling, file:///home/rick/.sdkman/candidates/maven/3.8.8/conf/logging/, file:/home/rick/.sdkman/candidates/maven/3.8.8/lib/maven-embedder-3.8.8.jar, <the rest is ommitted for brevity>]
        [ERROR]   org.openrewrite.java.JavaParser.dependenciesFromClasspath(JavaParser.java:107)
        [ERROR]   org.openrewrite.java.JavaParser$Builder.classpath(JavaParser.java:351)
        [ERROR]   org.openrewrite.java.apache.httpclient5.AddTimeUnitArgument$1.visitMethodInvocation(AddTimeUnitArgument.java:80)
        [ERROR]   org.openrewrite.java.apache.httpclient5.AddTimeUnitArgument$1.visitMethodInvocation(AddTimeUnitArgument.java:70)
        [ERROR]   org.openrewrite.java.tree.J$MethodInvocation.acceptJava(J.java:3723)
        [ERROR]   org.openrewrite.java.tree.J.accept(J.java:59)
        [ERROR]   org.openrewrite.TreeVisitor.visit(TreeVisitor.java:278)
        [ERROR]   org.openrewrite.TreeVisitor.visitAndCast(TreeVisitor.java:361)
        [ERROR]   ...
        [ERROR] -> [Help 1]
        [ERROR]
        [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
        [ERROR] Re-run Maven using the -X switch to enable full debug logging.
        [ERROR]
        [ERROR] For more information about the errors and possible solutions, please read the following articles:
        [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginExecutionException
timtebeek commented 10 months ago

Hmm; the key part here seems to be the

Unable to find runtime dependencies beginning with: 'httpclient5', 'httpcore5'

Which is odd, since we make that available on the classpath bundled into the jar: https://github.com/openrewrite/rewrite-spring/tree/main/src/main/resources/META-INF/rewrite/classpath

And I also see that in the latest 5.0.8 release available on Maven Central.

timtebeek commented 10 months ago

I've not often seen a mix of rewrite-maven-plugin in the pom.xml with additional recipeArtifactCoordinates passed in through a command. Does it work if you either exclusively use the command, without rewrite added to the pom.xml, or with the recipe dependencies added to the pom.xml instead?

rickie commented 10 months ago

Ahh I did that because for some reason it started using a newer version than 5.4.2 of the rewrite-maven-plugin when I was tweaking some things.

I looked into that now and with 5.5.0 it gave a different error 🤔:

[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.openrewrite.maven:rewrite-maven-plugin:5.5.0:run (default-cli) on project openrewrite-httpclient-reproduction: Execution default-cli of goal org.openrewrite.maven:rewrite-maven-plugin:5.5.0:run failed: Error while visiting src/main/java/tech/picnic/openrewrite/Test.java: java.lang.IllegalStateException: Unable to construct Java17Parser.
[ERROR]   org.openrewrite.java.Java17Parser$Builder.build(Java17Parser.java:96)
[ERROR]   org.openrewrite.java.Java17Parser$Builder.build(Java17Parser.java:63)
[ERROR]   org.openrewrite.java.internal.template.JavaTemplateParser.compileTemplate(JavaTemplateParser.java:238)
[ERROR]   org.openrewrite.java.internal.template.JavaTemplateParser.parseMethodArguments(JavaTemplateParser.java:189)
[ERROR]   org.openrewrite.java.internal.template.JavaTemplateJavaExtension$1.visitMethodInvocation(JavaTemplateJavaExtension.java:404)
[ERROR]   org.openrewrite.java.internal.template.JavaTemplateJavaExtension$1.visitMethodInvocation(JavaTemplateJavaExtension.java:56)
[ERROR]   org.openrewrite.java.tree.J$MethodInvocation.acceptJava(J.java:3723)
[ERROR]   org.openrewrite.java.tree.J.accept(J.java:59)

I see that 5.5.0 is from yesterday 😄. So I just added the pom.xml definition to make sure it used 5.4.2 but we usually don't do that.

timtebeek commented 9 months ago

We did another batch of releases yesterday; you might want to try again with those to see if that has helped. (After your holiday; enjoy!)