Adding ImportLayoutStyle to OrderImports recipe removes all import statements...
Thank you for creating this framework and the platform.
Problem Context:
This one is straight from the "getting started" documentation (https://docs.openrewrite.org/running-recipes/getting-started). I cloned the project "spring-petclinic-migration" and setup the plugin (version 6.1.1.) as documented and configured the plugin for OrderImports recipe.
The recipe worked fine. The order of imports were changed from the original. However, I found that the class org.springframework.samples.petclinic.owner.Owner got its javax.persistence classes got collapsed as javax.persistence.*. As I prefer the imports to include individual classes, I wanted to change the style. Based on another documentation (https://docs.openrewrite.org/concepts-and-explanations/styles), when I added the rewrite.yml and configured maven plugin to include the style. At this time, the rewrite:run removed all imports from all classes.
Creating this as new issue, as I could not find a similar one reported. Apologize, if this is already reported/resolved.
What version of OpenRewrite are you using?
I am using maven plugin version 6.1.1. I do have Java JDK 21 installed; but maven is configured to use version JDK 11.
How are you running OpenRewrite?
From the command line. Not using any IDE. I am using WinMerge to view the differences against original.
What is the smallest, simplest way to reproduce the problem?
- Cloned the project spring pet clinic from the Github.
- Configured the maven plugin as documented.
- Running the recipe with Order Imports works fine
- Added the rewrite.yml with ImportLayoutStyle and configured the maven plugin with the style.
- Running the recipe removed all imports.
Here is the maven plugin configuration:
<plugin>
<groupId>org.openrewrite.maven</groupId>
<artifactId>rewrite-maven-plugin</artifactId>
<version>6.1.1</version>
<configuration>
<activeRecipes>
<recipe>org.openrewrite.java.format.AutoFormat</recipe>
<recipe>org.openrewrite.java.OrderImports</recipe>
</activeRecipes>
<activeStyles>
<style>com.yourorg.YesTabsNoStarImports</style>
</activeStyles>
</configuration>
</plugin>
And here is the rewrite.yml
---
type: specs.openrewrite.org/v1beta/style
name: com.yourorg.YesTabsNoStarImports
styleConfigs:
- org.openrewrite.java.style.TabsAndIndentsStyle:
useTabCharacter: true
- org.openrewrite.java.style.ImportLayoutStyle:
classCountToUseStarImport: 9999
What did you expect to see?
The java class with the imports as below. But the entire section of imports were removed. From all classes.
package org.springframework.samples.petclinic.owner;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.validation.constraints.Digits;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.beans.support.MutableSortDefinition;
import org.springframework.beans.support.PropertyComparator;
import org.springframework.core.style.ToStringCreator;
import org.springframework.samples.petclinic.model.Person;
/**
* Simple JavaBean domain object representing an owner.
*
* @author Ken Krebs
* @author Juergen Hoeller
* @author Sam Brannen
* @author Michael Isvy
*/
@Entity
@Table(name = "owners")
public class Owner extends Person {
...
}
Thanks for the clear steps to reproduce this issue @ajoshi66 ! Indeed strange how setting a preferred style leads to removed imports. Admittedly I haven't used explicit styles much myself. Seems like something to explore & fix. Did you already try to also configure nameCountToUseStarImport ? Looks like that's required as well:
https://github.com/openrewrite/rewrite/blob/525f36804b46fd803de2b2010d58dbfe811d99e9/rewrite-java/src/main/java/org/openrewrite/java/style/ImportLayoutStyle.java#L71-L75
Thanks, @timtebeek for the response.
I took a fresh copy of the source, added the nameCountToUseStarImport with a value 9999 and rerun the recipe. The issue is still the same. All imports from all classes are removed.
That's unfortunate! Guess we'll need to figure out a fix & update the docs then. /cc @mike-solomon for visibility already
Just an update. May not be of much use to you.
I found the JUnit test case that tests OrderImports using ImportLayoutStyle from rewrite-java-test. It runs a simple snippet with classCountToUseStarImport and nameCountToUseStarImport to 999 and is working fine - keeps all imports intact. Also, it collapses the imports to * when tried with values 2 to both attributes and has imports more than 2 classes from the same package. Can't run the test with full class of pet clinic, as it expects dependencies to be included.
This issue is stale because it has not had any activity for 60 days. Remove question label or comment or this will be closed in two weeks. Issues may be reopened when there is renewed interest.
I've ran into the same problem with a config missing the layout attribute:
styleConfigs:
- org.openrewrite.java.style.ImportLayoutStyle:
classCountToUseStarImport: 999
nameCountToUseStarImport: 999