openrewrite / rewrite-gradle-plugin

OpenRewrite's Gradle plugin.
Apache License 2.0
60 stars 37 forks source link

`DefaultProjectParser` looses subproject from the path #231

Closed iuliiasobolevska closed 12 months ago

iuliiasobolevska commented 1 year ago

When org.openrewrite:plugin gets upgraded from 6.1.26 to 6.2.0 (diff between versions), all recipes based on org.openrewrite.properties.ChangePropertyKey create new application.yml/application.properties files in the root of the project and under src/main/resources folder:

% git status -u
...
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        application.yml
        src/main/resources/application.yml

instead of updating the application.yml/application.properties files already present in the project.

The issue is reproducible both for single and multi-module projects by adding a property to be updated and running any ChangePropertyKey-based recipe, e.g. add

management:
  trace:
    http:
      enabled: true

to application.yml file and run org.openrewrite.java.spring.boot3.SpringBootProperties_3_0 recipe. See openrewritedemo.zip.

timtebeek commented 12 months ago

Thanks for reporting this issue here and pinpointing the exact point at which you first see this issue occur. Just to be clear: do you still see this issue with the most recent 6.3.8 release of rewrite-gradle-plugin ?

timtebeek commented 12 months ago

Thanks for the helpful sample project too! When I run ./gradlew rewriteRun on that I indeed see a new application.yml file added to the root, in addition to changes to the intended src/main/resources/application.yml. 😞

iuliiasobolevska commented 12 months ago

Hi Tim!

Just to be clear: do you still see this issue with the most recent 6.3.8 release of rewrite-gradle-plugin ?

Yes, I still get it with rewrite-gradle-plugin:6.3.8.

When I run ./gradlew rewriteRun on that I indeed see a new application.yml file added to the root, in addition to changes to the intended src/main/resources/application.yml.

For single-module projects (e.g. my example repo) it at least updates the intended src/main/resources/application.yml so the only impact is an extra file. For multi-module projects, it just creates 2 extra files and the intended ones are not being updated. Given that most of our projects are multi-module ones, this bug is pretty severe (we are not blocked as we pinned to 6.1.26 for now but I can't verify other issues since we are not on the latest).

timtebeek commented 12 months ago

Hmm yes pinning is not ideal; I've been trying for bit to replicate the issue, first starting with the OmniParser that was added in the switch to 6.2.0; no such luck there yet with a test added to OmniParserTest

@Test
@Issue("https://github.com/openrewrite/rewrite-gradle-plugin/issues/231")
void applicationResourcesParsedOnce() throws Exception {
    OmniParser parser = OmniParser.builder(OmniParser.RESOURCE_PARSERS)
      .build();
    List<SourceFile> sourceFileStream = parser.parseAll(Path.of("src/test/resources/demo")).toList();
    assertThat(sourceFileStream).hasSize(2); // pom.xml and application.yml

    ChangePropertyKey changePropertyKey = new ChangePropertyKey("foo.bar", "bar.foo", null, null);
    Yaml.Documents visited = (Yaml.Documents) changePropertyKey.getVisitor().visit(sourceFileStream.get(1), null);
    assertThat(((Yaml.Mapping)visited.getDocuments().get(0).getBlock()).getEntries().get(0).getKey().getValue()).isEqualTo("bar.foo");
}

Next stop is likely a test in this project, but likely after the weekend for me, if I get to it then with talks to prepare & give.

Unless @sambsnyd is able to have a look; I know he's looked into duplicate files in the past week as well.

sambsnyd commented 12 months ago

I am taking a look at this

sambsnyd commented 12 months ago

Thanks for reporting @iuliiasobolevska ! I will be issuing a new plugin release to get this fix out there soon

iuliiasobolevska commented 12 months ago

Thank you both for the quick turnaround! Highly appreciated!