openrewrite / rewrite

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

rewrite:run (warns about) failing to download SNAPSHOT parent from local repository #4508

Open DidierLoiseau opened 1 week ago

DidierLoiseau commented 1 week ago

What version of OpenRewrite are you using?

I am using

How are you running OpenRewrite?

I am using the Maven plugin, and my project is a single module project with a parent in SHAPSHOT version.

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

Parent pom Just install it in your local repository with mvn install.

<?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.example</groupId>
    <artifactId>parent-pom</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>

</project>

Child pom

<?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>
    <parent>
        <groupId>com.example</groupId>
        <artifactId>parent-pom</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>child-pom</artifactId>

    <build>
        <plugins>
            <plugin>
                <groupId>org.openrewrite.maven</groupId>
                <artifactId>rewrite-maven-plugin</artifactId>
                <version>5.39.2</version>
                <configuration>
                    <activeRecipes>
                        <recipe>org.openrewrite.java.migrate.UpgradeBuildToJava17</recipe>
                    </activeRecipes>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.openrewrite.recipe</groupId>
                        <artifactId>rewrite-migrate-java</artifactId>
                        <version>2.23.0</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
</project>

I’m running rewrite:run on the child pom.

What did you expect to see?

No warning from the maven plugin.

What did you see instead?

Multiple warnings about failing to download the parent from the local repository. I’m not sure about the impact – parent gets ignored in recipes?

[INFO] --- rewrite:5.39.2:run (default-cli) @ child-pom ---
[INFO] Using active recipe(s) [org.openrewrite.java.migrate.UpgradeBuildToJava17]
[INFO] Using active styles(s) []
[INFO] Validating active recipes...
[INFO] Project [child-pom] Resolving Poms...
[WARNING] Failed to download com.example:parent-pom:1.0-SNAPSHOT. Attempted URIs:
  - file:///C:/Users/loiseadi/.m2/repository/
[WARNING] Failed to download com.example:parent-pom:1.0-SNAPSHOT. Attempted URIs:
  - file:///C:/Users/loiseadi/.m2/repository/
[WARNING] Failed to download com.example:parent-pom:1.0-SNAPSHOT. Attempted URIs:
  - file:///C:/Users/loiseadi/.m2/repository/
[INFO] Project [child-pom] Parsing source files
[INFO] Running recipe(s)...
[WARNING] Failed to download com.example:parent-pom:1.0-SNAPSHOT. Attempted URIs:
  - file:///C:/Users/loiseadi/.m2/repository/
[WARNING] Failed to download com.example:parent-pom:1.0-SNAPSHOT. Attempted URIs:
  - file:///C:/Users/loiseadi/.m2/repository/
[WARNING] Failed to download com.example:parent-pom:1.0-SNAPSHOT. Attempted URIs:
  - file:///C:/Users/loiseadi/.m2/repository/

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

With debug output:

[DEBUG] -- end configuration --
[INFO] Using active recipe(s) [org.openrewrite.java.migrate.UpgradeBuildToJava17]
[INFO] Using active styles(s) []
[DEBUG] No rewrite configuration found at C:\projects\maven-tests\child-pom\rewrite.yml
[INFO] Validating active recipes...
[INFO] Project [child-pom] Resolving Poms...
[WARNING] Failed to download com.example:parent-pom:1.0-SNAPSHOT. Attempted URIs:
  - file:///C:/Users/loiseadi/.m2/repository/
[DEBUG] Downloaded com.example:parent-pom:1.0-SNAPSHOT from com.example:child-pom:1.0-SNAPSHOT
[WARNING] Failed to download com.example:parent-pom:1.0-SNAPSHOT. Attempted URIs:
  - file:///C:/Users/loiseadi/.m2/repository/
[WARNING] Failed to download com.example:parent-pom:1.0-SNAPSHOT. Attempted URIs:
  - file:///C:/Users/loiseadi/.m2/repository/
[DEBUG] Project [child-pom] Base directory : 'C:\projects\maven-tests\child-pom'
[DEBUG] Project [child-pom]   Collected Maven POM : 'C:\projects\maven-tests\child-pom\pom.xml'
[DEBUG] Project [child-pom]   Maven Source : 'C:\projects\maven-tests\child-pom\pom.xml'
[INFO] Project [child-pom] Parsing source files
[DEBUG] Project [child-pom] Scanned 0 resource files in main scope.
[DEBUG] Project [child-pom] Scanned 0 resource files in test scope.
[DEBUG] Project [child-pom] Parsed 0 additional files found within the project.
[INFO] Running recipe(s)...
[DEBUG] Using SLF4J as the default logging framework
[WARNING] Failed to download com.example:parent-pom:1.0-SNAPSHOT. Attempted URIs:
  - file:///C:/Users/loiseadi/.m2/repository/
[WARNING] Failed to download com.example:parent-pom:1.0-SNAPSHOT. Attempted URIs:
  - file:///C:/Users/loiseadi/.m2/repository/
[WARNING] Failed to download com.example:parent-pom:1.0-SNAPSHOT. Attempted URIs:
  - file:///C:/Users/loiseadi/.m2/repository/

Note the Downloaded com.example:parent-pom:1.0-SNAPSHOT from com.example:child-pom:1.0-SNAPSHOT – so it failed successfully?

Also note that there is no warning when parent is not -SNAPSHOT.

DidierLoiseau commented 1 week ago

I decided to try debugging it. Despite the log messages coming from rewrite-maven-plugin, I think the issue comes from rewrite-maven:

                    String uri = repo.getUri() + (repo.getUri().endsWith("/") ? "" : "/") +
                                 requireNonNull(gav.getGroupId()).replace('.', '/') + '/' +
                                 gav.getArtifactId() + '/' +
                                 (gav.getVersion() == null ? "" : gav.getVersion() + '/') +
                                 "maven-metadata.xml";

Indeed, in local, the metadata files are called maven-metadata-<repo-id>.xml – so maven-metadata-local.xml for the local repository itself. See Maven Repository Metadata Model.

I don’t know if this ticket should be moved to rewrite then? Or should I recreate it myself maybe?

timtebeek commented 3 days ago

Hi! Thanks for logging your findings & the patience before a reply. We've pushed a change in 5.40.2 that might change the behavior that you're seeing here:

Although in this case it seems a child module is still looking for the parent in ~/.m2/repository as opposed to looking up the <relativePath>../</relativePath>? Indeed seems odd that you're seeing warnings on that. Appreciate you calling it out and doing some initial debugging, and the helpful link to the meta data model. Indeed would lean towards an issue with openrewrite/rewrite then. I'll move the issue accordingly.