openrewrite / rewrite

Automated mass refactoring of source code.
https://docs.openrewrite.org
Apache License 2.0
2.22k stars 330 forks source link

MavenResolutionResult's ResolvedDependencies List Includes Excluded Dependencies #4126

Open matthew-js-porter opened 7 months ago

matthew-js-porter commented 7 months ago

What version of OpenRewrite are you using?

I am using

How are you running OpenRewrite?

I am Running from a Junit Test

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

LogMavenDependencies.kt

package org.sample

import org.openrewrite.ExecutionContext
import org.openrewrite.Recipe
import org.openrewrite.maven.MavenIsoVisitor
import org.openrewrite.maven.tree.Scope
import org.openrewrite.xml.tree.Xml

class LogMavenDependencies : Recipe() {
    override fun getDisplayName() = "Log Maven Dependencies"

    override fun getDescription() = "Log Maven Dependencies."

    override fun getVisitor() = object : MavenIsoVisitor<ExecutionContext>() {
        override fun visitDocument(document: Xml.Document, ctx: ExecutionContext): Xml.Document {
            println(resolutionResult.dependencies.get(Scope.Compile)?.map { it.gav }?.joinToString(separator = System.lineSeparator()))
            return document
        }
    }
}

ShowExclusionIssueTest.kt

package org.sample

import org.junit.jupiter.api.Test
import org.openrewrite.java.Assertions
import org.openrewrite.test.RecipeSpec
import org.openrewrite.test.RewriteTest

class ShowExclusionIssueTest: RewriteTest{

    override fun defaults(spec: RecipeSpec) {
        spec.recipe(LogMavenDependencies())
    }

    @Test
    fun `log maven dependencies`() = rewriteRun(
        Assertions.mavenProject(
            "proj",
            org.openrewrite.maven.Assertions.pomXml(
                """
                <?xml version="1.0" encoding="UTF-8"?>
                <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/xsd/maven-4.0.0.xsd">
                    <modelVersion>4.0.0</modelVersion>
                    <groupId>com.sample</groupId>
                    <artifactId>sample</artifactId>
                    <version>1.0-SNAPSHOT</version>

                    <parent>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-starter-parent</artifactId>
                        <version>3.2.4</version>
                    </parent>

                    <dependencies>
                        <dependency>
                            <groupId>org.springframework.boot</groupId>
                            <artifactId>spring-boot-starter-web</artifactId>
                            <exclusions>
                              <exclusion>
                                    <groupId>org.apache.tomcat.embed</groupId>
                                    <artifactId>tomcat-embed-core</artifactId>
                                </exclusion>
                            </exclusions>
                        </dependency>
                   </dependencies>
                </project>
            """.trimIndent())
            ),
        )
}

Run the test and observe the output

org.springframework.boot:spring-boot-starter-web:3.2.4
org.springframework.boot:spring-boot-starter:3.2.4
org.springframework.boot:spring-boot-starter-json:3.2.4
org.springframework.boot:spring-boot-starter-tomcat:3.2.4
org.springframework:spring-web:6.1.5
org.springframework:spring-webmvc:6.1.5
org.springframework.boot:spring-boot:3.2.4
org.springframework.boot:spring-boot-autoconfigure:3.2.4
org.springframework.boot:spring-boot-starter-logging:3.2.4
jakarta.annotation:jakarta.annotation-api:2.1.1
org.springframework:spring-core:6.1.5
org.yaml:snakeyaml:2.2
com.fasterxml.jackson.core:jackson-databind:2.15.4
com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.15.4
com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.15.4
com.fasterxml.jackson.module:jackson-module-parameter-names:2.15.4
org.apache.tomcat.embed:tomcat-embed-core:10.1.19
org.apache.tomcat.embed:tomcat-embed-el:10.1.19
org.apache.tomcat.embed:tomcat-embed-websocket:10.1.19
org.springframework:spring-beans:6.1.5
io.micrometer:micrometer-observation:1.12.4
org.springframework:spring-aop:6.1.5
org.springframework:spring-context:6.1.5
org.springframework:spring-expression:6.1.5
ch.qos.logback:logback-classic:1.4.14
org.apache.logging.log4j:log4j-to-slf4j:2.21.1
org.slf4j:jul-to-slf4j:2.0.12
org.springframework:spring-jcl:6.1.5
com.fasterxml.jackson.core:jackson-annotations:2.15.4
com.fasterxml.jackson.core:jackson-core:2.15.4
io.micrometer:micrometer-commons:1.12.4
ch.qos.logback:logback-core:1.4.14
org.slf4j:slf4j-api:2.0.12
org.apache.logging.log4j:log4j-api:2.21.1

What did you expect to see?

A list of dependencies that did not include org.apache.tomcat.embed:tomcat-embed-core

Are you interested in contributing a fix to OpenRewrite?

timtebeek commented 6 months ago

Hi @matthew-js-porter ; Runnable tests are my absolute favorite way to receive a bug report, so thanks a lot for that!

As to what causes this we'd have to look into this; and make sure any fix lines up with what recipes expect.

I do notice you reported using rewrite-maven 8.11.5, whereas the latest version is 8.24.0, as managed through our rewrite-recipe-bom 2.10.0. Any reason you're still on an older version?

Did you already look into what might cause us to report excluded dependencies as dependency? I'd welcome a draft PR with just the above test added to this project if you're up for it; that way we can tag-team on a possible fix.