openrewrite/rewrite

addProperty creates a new gradle.properties file instead of adding new properties when running in multi-module project.

Closed this issue · 3 comments

What version of OpenRewrite are you using?

I am using

  • OpenRewrite v8.33.8
  • Gradle plugin v8.4

How are you running OpenRewrite?

I'm running openrewrite over a multi-module project, it seems to be key in this bug.

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

My project has the following structure
gradle.properties
settings.gradle
api-grpc/build.gradle
server/build.gradle

There are not a build.gradle file at the root.

The gradle.properties file has several properties, like:

my.property.number1=1
my.property.number2=2

My recipe adds several properties:

recipeList:
 - org.openrewrite.gradle.AddProperty:
     key: my.property.jakarta
     value: 1
     overwrite: false
     filePattern: 'gradle.properties'
 - org.openrewrite.gradle.AddProperty:
     key: my.property.boot3
     value: 1
     overwrite: true
     filePattern: 'gradle.properties'

What did you expect to see?

my.property.number1=1
my.property.number2=2
my.property.jakarta=1
my.property.boot3=1

What did you see instead?

It is resulting in a new properties file without any of the original properties and just the first property of my recipe:

my.property.jakarta=1

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

In the log is possible to see it is re creating the file:

Generated new file gradle.properties by:
    org.openrewrite.gradle.AddProperty: {key=my.property.jakarta, value=1, overwrite=true, filePattern=gradle.properties}

NOTE: When I added a build.gradle file at the root of the project, it works fine!

Are you interested in [contributing a fix to OpenRewrite]

I'm, but I don't have enough experience to do it now. If someone can help me, I can try to fix this issue.

So it looks like the recipe is expecting for a build.gradle file to exist next to the properties file and that is leading to your existing file not being edited.

https://github.com/openrewrite/rewrite/blob/main/rewrite-gradle%2Fsrc%2Fmain%2Fjava%2Forg%2Fopenrewrite%2Fgradle%2FAddProperty.java#L99-L101

Also it seems like you may be referring to your Gradle version as the current release is 6.23.3.

One question that I'd have as well is where/how you are applying the Gradle plugin specifically from a project standpoint. It's typically recommended to utilize the Running Rewrite on a Gradle project without modifying the build guide which applies the plugin to the root project.

So it looks like the recipe is expecting for a build.gradle file to exist next to the properties file and that is leading to your existing file not being edited.

That's it. What do you think about modifying it to consider the settings.gradle file also an indicator of a gradle project?

Also it seems like you may be referring to your Gradle version as the current release is 6.23.3.

I couldn't understand it. I'm running gradle 8.4.
and org.openrewrite:rewrite-gradle:8.33.4

where/how you are applying the Gradle plugin

We are running it without modifying the project, embedded on our own gradle plugin.

This doesn't seem to be an openrewrite issue. I'm running the recipe inside a Gradle Plugin, and the root folder without a build.gradle file is not detected as a Project. With that, the plugin is not applied to the root folder.
When I run the recipe in a standalone way it works properly.