openrewrite / rewrite

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

OpenRewrite may not correctly detecting the import styles #1674

Closed luisrodval closed 2 years ago

luisrodval commented 2 years ago

Hi! When executing rewrite:run if I change 5 or more types of the same package then it nests the imports with asterisk. The problem is that sonar marks it as a critical issue.

Is there any way to prevent it from doing that? Thank you!

tkvangorder commented 2 years ago

Hi @luisrodval ,

You can define your own styles in yaml:

https://docs.openrewrite.org/reference/yaml-format-reference#styles

---
type: specs.openrewrite.org/v1beta/style
name: com.yourorg.NoStarImports
styleConfigs:
  - org.openrewrite.java.style.ImportLayoutStyle:
      classCountToUseStarImport: 9999

There are instructions on how to activate your style in the plugin configuration:

https://docs.openrewrite.org/reference/rewrite-maven-plugin https://docs.openrewrite.org/reference/gradle-plugin-configuration

Hope this helps!

tkvangorder commented 2 years ago

@okundzich Had a valid point, rewrite should be detecting the import styles.

tkvangorder commented 2 years ago

@luisrodval You can use the above to work around the issue, but we are interested in your use case, as Rewrite should be detecting the import styles present in your code.

luisrodval commented 2 years ago

Hi, I have created yml with:

    ---
    #########################################################
    # No star imports style.
    type: specs.openrewrite.org/v1beta/style
    name: com.santander.darwin.migrationassistant.NoStarImports
    styleConfigs:
      - org.openrewrite.java.style.ImportLayoutStyle:
          classCountToUseStarImport: 999
          nameCountToUseStarImport: 999

In the plugin of pom.xml I use this tag:

When execute rewrite:run there this trace in console: [INFO] Using active recipe(s) [com.santander.darwin.migrationassistant.myRecipes] [INFO] Using active styles(s) [com.santander.darwin.migrationassistant.NoStarImports]

It seems try apply styles, but also trace this for each executed recipe: [WARNING] Expected to find a block to fit import into.

The recipes (org.openrewrite.java.ChangeType) not works when I use the new styles. What am I doing wrong? IMPORTANT: If I not use styles, the recipes work successfull.

PD: this is a example project such use changeType and styles (with NotStarImports) not works -> https://github.com/josemariavillar/test_project/tree/ImportLayoutStyle

Thank you.

traceyyoshima commented 2 years ago

Hi @luisrodval, the warning is caused by missing blocks in the layout style. I opened an issue and will track down the cause in a moment.

Update: the warning was fixed by #1691

traceyyoshima commented 2 years ago

When executing rewrite:run if I change 5 or more types of the same package then it nests the imports with asterisk. The problem is that sonar marks it as a critical issue.

@luisrodval I'd be happy to look into the issue! I think there may be a star import somewhere in the project, and it'd be really helpful to know more information.

Do you happen to know if SonarQube has different settings for main and test for the project you are working on?

traceyyoshima commented 2 years ago

Closing the issue due to inactivity.

Info: I checked Autodetect of the ImportLayoutStyle and the import thresholds are only lowered when a * import is detected. I suspect the SQ rules are only applied to main and that * imports exist in test.

We've seen this before in a few projects and if requested, we could use the JavaSourceSet to detect layouts separately between main and test.