Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding ImportLayoutStyle to OrderImports recipe removes all import statements... #5032

Open
ajoshi66 opened this issue Feb 13, 2025 · 4 comments
Labels
bug Something isn't working documentation Improvements or additions to documentation

Comments

@ajoshi66
Copy link

ajoshi66 commented Feb 13, 2025

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?

  1. Cloned the project spring pet clinic from the Github.
  2. Configured the maven plugin as documented.
  3. Running the recipe with Order Imports works fine
  4. Added the rewrite.yml with ImportLayoutStyle and configured the maven plugin with the style.
  5. 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 {
   ...
}
@ajoshi66 ajoshi66 added the bug Something isn't working label Feb 13, 2025
@timtebeek
Copy link
Contributor

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:

@EqualsAndHashCode.Include
private final int classCountToUseStarImport;
@EqualsAndHashCode.Include
private final int nameCountToUseStarImport;

@timtebeek timtebeek moved this to Backlog in OpenRewrite Feb 17, 2025
@ajoshi66
Copy link
Author

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.

@timtebeek
Copy link
Contributor

That's unfortunate! Guess we'll need to figure out a fix & update the docs then. /cc @mike-solomon for visibility already

@timtebeek timtebeek added the documentation Improvements or additions to documentation label Feb 18, 2025
@ajoshi66
Copy link
Author

ajoshi66 commented Feb 21, 2025

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working documentation Improvements or additions to documentation
Projects
Status: Backlog
Development

No branches or pull requests

2 participants