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

bugfix: reorder import in GJF using Google Style #1680

Merged
merged 4 commits into from
May 17, 2023

Conversation

simschla
Copy link
Contributor

This fixes #1679.

In the origin GJF reflection code (before #1630), we used to call reorderImports(String) which internally expanded to reorderImports(String, Style.GOOGLE) no matter what style the user had configured.

The report in #1679 uses the AOSP style, so the code after #1630 called reorderImports(String, Style.AOSP) (instead of the reorderImports(String) (which is an alias for reorderImports(String, Style.GOOGLE)).

Changing that, resolves the issue. (This PR)

@nedtwigg Underlying, however, is another issue: Calling the actual reorderImports(String, Style.AOSP) on the example file results in a Situation where spotless reorders imports even though they are in the formatting:off and formatting:on section of the file. You can see that if you change the checkIssue1679() test method, to only include the formatters (toggleOffOnPair.in(), gjf, toggleOffOnPair.out()). I did not investigate that further, but could be that there is a general issue with those toggles in the import section?

@big-andy-coates
Copy link

Thanks for fixing @simschla!

I did not investigate that further, but could be that there is a general issue with those toggles in the import section?

Is is worth raising a new bug to track this secondary issue?

@simschla
Copy link
Contributor Author

Is is worth raising a new bug to track this secondary issue?

I think it's worth keeping in mind (and as such raise an issue for that) - but I'm not very at home in that part of the code, so maybe @nedtwigg should weigh in here whenever he finds the time.

@simschla
Copy link
Contributor Author

BTW @big-andy-coates are you OK with me using your "problem file" in testlib/src/main/resources/combined/issue1679.clean and testlib/src/main/resources/combined/issue1679.dirty directly or would you rather have me create an artificial file?

@big-andy-coates
Copy link

Yep, no worries - its OSS anyway

@simschla simschla requested a review from nedtwigg May 9, 2023 21:09
@nedtwigg
Copy link
Member

Thanks very much for reporting your issue @big-andy-coates, and thanks for tracking down the very subtle cause @simschla.

As for ignoring the formatter:off tag - if a step removes those tags, then we can't restore their content. We rely on intermediate steps keeping those tags inside the source code.

The real "problem" here is (until recently) we don't actually respect AOSP style when using gjf to do imports. But that has been our behavior for so long that it's essentially a regression for existing users.

If we wanted to respect style when doing GJF import order, we would have to add a second flag for import style, e.g.
.style(AOSP).importOrder(AOSP). Back-compat for existing users is the most important thing, I'm open to supporting alternative import styles as an "enhancement" if somebody wants to contribute that, but I don't think it's very important.

@nedtwigg nedtwigg enabled auto-merge May 17, 2023 19:21
@nedtwigg nedtwigg merged commit c2ec7e1 into diffplug:main May 17, 2023
11 checks passed
@nedtwigg
Copy link
Member

Released in plugin-gradle 6.19.0 and plugin-maven 2.37.0.

@bjrke
Copy link

bjrke commented May 31, 2023

we adapted 6.18 and the new import order inherited from google-java-format since it perfectly works with the gjf plugin of IntelliJ which also changed its import order with the update.
adding importOrder("AOSP") doesn't really help, because you have to declare the import order manually like importOrder('java|javax', 'com.acme', '', '\\#com.acme', '\\#') (just copied from the docs)
an importOrder(AOSP) doesn't exist in the gradle dsl and I don't see a trick to achieve it.

@simschla
Copy link
Contributor Author

simschla commented May 31, 2023

@bjrke I think you misunderstood. the importOrder("AOSP") API does not exist, that was a suggestion of @nedtwigg if we wanted to go the way of exposing these different gjf import orderings.

As of now, you can upgrade to 6.19.0 and get the original gjf import order back, or set a custom import order using importOrder('java', 'com', ...) if you'd like to override it to something else

@bjrke
Copy link

bjrke commented Jun 19, 2023

are there any plans (different issue or PR) to introduce such a possibility? For the moment we just don't upgrade, but in the long term it would be very useful to have this.

@simschla
Copy link
Contributor Author

@bjrke AFAIK, there is nothing planned (yet). I suggest you create an Issue describing what the changes are, that you are proposing. Adding them to the API should be quite straightforward, and I'll gladly implement it as soon as I have some time to spare. (Or maybe someone from the community or yourself is willing to step-in in the meantime?)

benkard pushed a commit to benkard/mulkcms2 that referenced this pull request Aug 29, 2023
This MR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [com.diffplug.spotless:spotless-maven-plugin](https://github.com/diffplug/spotless) | build | minor | `2.38.0` -> `2.39.0` |
| [io.quarkus:quarkus-maven-plugin](https://github.com/quarkusio/quarkus) | build | patch | `3.3.0` -> `3.3.1` |
| [io.quarkus:quarkus-universe-bom](https://github.com/quarkusio/quarkus-platform) | import | patch | `3.3.0` -> `3.3.1` |

---

### Release Notes

<details>
<summary>diffplug/spotless</summary>

### [`v2.39.0`](https://github.com/diffplug/spotless/blob/HEAD/CHANGES.md#&#8203;2390---2023-05-24)

##### Added

-   `Jvm.Support` now accepts `-SNAPSHOT` versions, treated as the non`-SNAPSHOT`. ([#&#8203;1583](diffplug/spotless#1583))
-   Support Rome as a formatter for JavaScript and TypeScript code. Adds a new `rome` step to `javascript` and `typescript` formatter configurations. ([#&#8203;1663](diffplug/spotless#1663))
-   Add semantics-aware Java import ordering (i.e. sort by package, then class, then member). ([#&#8203;522](diffplug/spotless#522))

##### Fixed

-   Fixed a regression which changed the import sorting order in `googleJavaFormat` introduced in `2.38.0`. ([#&#8203;1680](diffplug/spotless#1680))
-   Equo-based formatters now work on platforms unsupported by Eclipse such as PowerPC (fixes [durian-swt#&#8203;20](diffplug/durian-swt#20))
-   When P2 download fails, indicate the responsible formatter. ([#&#8203;1698](diffplug/spotless#1698))

##### Changes

-   Equo-based formatters now download metadata to `~/.m2/repository/dev/equo/p2-data` rather than `~/.equo`, and for CI machines without a home directory the p2 data goes to `$GRADLE_USER_HOME/caches/p2-data`. ([#&#8203;1714](diffplug/spotless#1714))
-   Bump default `googleJavaFormat` version to latest `1.16.0` -> `1.17.0`. ([#&#8203;1710](diffplug/spotless#1710))
-   Bump default `ktfmt` version to latest `0.43` -> `0.44`. ([#&#8203;1691](diffplug/spotless#1691))
-   Bump default `ktlint` version to latest `0.48.2` -> `0.49.1`. ([#&#8203;1696](diffplug/spotless#1696))
    -   Dropped support for `ktlint 0.46.x` following our policy of supporting two breaking changes at a time.
-   Bump default `sortpom` version to latest `3.0.0` -> `3.2.1`. ([#&#8203;1675](diffplug/spotless#1675))

</details>

<details>
<summary>quarkusio/quarkus</summary>

### [`v3.3.1`](quarkusio/quarkus@3.3.0...3.3.1)

[Compare Source](quarkusio/quarkus@3.3.0...3.3.1)

</details>

<details>
<summary>quarkusio/quarkus-platform</summary>

### [`v3.3.1`](quarkusio/quarkus-platform@3.3.0...3.3.1)

[Compare Source](quarkusio/quarkus-platform@3.3.0...3.3.1)

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Enabled.

♻ **Rebasing**: Whenever MR is behind base branch, or you tick the rebase/retry checkbox.

👻 **Immortal**: This MR will be recreated if closed unmerged. Get [config help](https://github.com/renovatebot/renovate/discussions) if that's undesired.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this MR, check this box

---

This MR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNC4yNC4wIiwidXBkYXRlZEluVmVyIjoiMzQuMjQuMCJ9-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Upgrading gradle/6.17.0 to 6.18.0 results in "An intermediate step removed a match"
4 participants