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

removeUnusedImports - JDK15 TextBlocks - Fails when running jdk11 #1589

Merged
merged 18 commits into from
Apr 5, 2023

Conversation

blacelle
Copy link
Contributor

New occurences of #681

For now, just a reproduction case

@blacelle
Copy link
Contributor Author

Options.instance(context).put(Option.SOURCE, "9");

in com.google.googlejavaformat.java.RemoveUnusedImports#removeUnusedImports(java.lang.String) looks dubious.

@blacelle
Copy link
Contributor Author

The initial fix was relying on https://github.com/diffplug/spotless/pull/687/files#diff-282881b9b1bb0861937748f383b30476cf1134c56a5a33a450a2caa6ac4efabeR220 which seems to have moved elsewhere in main branch.

@blacelle
Copy link
Contributor Author

@nedtwigg Could the issue be rooted in the runtime JDK used to execute spotless? In my case, I run a JDK11 and here I'm processing a JDK17 file

@blacelle blacelle changed the title Add reproduction case removeUnusedImports - JDK15 TextBlocks - Fails when running jdk11 Feb 26, 2023
@blacelle
Copy link
Contributor Author

This proposes an alternative removeUnusedImports based on javaParser (which can handle jdk17 syntax, even with a jdk11 runtime). (In my case, I run spotless over JDK11 as AWS Lambda is not yet JDK17 friendly aws/aws-lambda-base-images#29).

@nedtwigg
Copy link
Member

Looks great! Part of me thinks we should just replace the GJF implementation with this one. And another part of me thinks we should do removeUnusedImports(Implementation.JavaParser/Implementation.GJF).

The synthesis is to use JavaParser by default since it should have the exact same behavior but resilient to more JDK versions. And then allow users to specify Implementaiton.GJF if they depend on the old behavior somehow.

Author's choice!

@blacelle
Copy link
Contributor Author

blacelle commented Feb 26, 2023

In the meantime, the author added this as a Cleanthat mutator. Hence, it would also be available as a cleanthat mutator. We may also make it available through some dedicated Glue to Cleanthat, very similarly to GJF (hence, no duplicated code between Cleanthat and Spotless).

Pro GJF :

  • It may be considered more stable than Javaparser (older, more users, etc)
  • Typically, I reported an issue to javaparser team while working on this (while the equivalent code in CleanThat does not fail on given edge-case)

Pro Javaparser :

Part of the code is inspired from Revelc (https://github.com/revelc/impsort-maven-plugin/blob/main/src/test/java/net/revelc/code/impsort/ImpSortTest.java) but simpler as it does only removing unused imports (which Revelc also implemented imports ordering, while Spotless has a solid implementation I did not plan to challenge for this round).


Need more time to :

  1. What if this implements (now or later) import sorting/grouping? (We would introduce options? while removeUnusedImports suggests a one-size-fits-all)
  2. Replace GJF implementation? No, I'm not confident enough in JavaParser to drop GFJ all-together. I would keep GJF as a fallback.
  3. Rely on Cleanthat for such a core step? Happy-me, if we agree this is the way to go.

@nedtwigg
Copy link
Member

nedtwigg commented Feb 26, 2023

I'd be happy to rely on whatever works best for removeUnusedImports(). Of the three options, cleanThat seems like the most actively developed option, so I'd be happy for that to become the default, but I would definitely keep GJF around as a fallback. I don't want to overwhelm the user with configuration options though. removeUnusedImports() is such a simple concept, it should just work, and it's too bad that GJF's behavior changes based on the JVM that's running.

What if this implements (now or later) import sorting/grouping

IMO our current import sorter works but has ossified such that we can't really change anything about it ever again. It underspecified some things around grouping and static imports, and now we can't fix or improve these issues without breaking the formatting of people who are "relying on the bugs".

So if this PR someday becomes the foundation of a new import cleaner that can deal with groups, static imports, etc. that sounds great. That could be a Spotless DSL, or it could just be a cleanthat option that users can activate, whatever is easy.

My sticking point is always that people solve the problems they themselves have, and that Spotless helps to keep them solved instead of needing to revisit the issue over and over. For people who have already adopted our existing import formatters, I don't want them to have to think about this ever again. For people who are coming to Spotless for the first time or starting new projects, I think it would be great to have a more comprehensive import cleaning solution. It might be worth thinking about an import cleaning solution which could be used across Java/Scala/Kotlin, etc. but mostly I think users don't care about that.

@blacelle
Copy link
Contributor Author

blacelle commented Feb 26, 2023

Ok. I'm totally in-line. I'll release soon a cleanthat version with this, and update the PR accordingly.


Regarding imports sorting, I personally do not lack anything in spotless. I came to Spotless from Revelc plugins, and some options (I do not use) may be missing in Spotless. My main discomfort is about the default configuration in Spotless (which is something, while I (personally) look for Eclipse defaults.). Also, I encountered some issues (the infamous 'Why are my files not import-sorted ?'), hence digged into Spotless import-srting implementation.

I fully agree on your synthesis, and improving import-sorting is definitely a lower-value item. (I'm on this issue only because a Cleanthat user encountered this textBlock issue).

@blacelle
Copy link
Contributor Author

@blacelle
Copy link
Contributor Author

blacelle commented Mar 1, 2023

Here is an iteration.

I'm not fan of:

static final String GJF = "google-java-format";
static final String CLEANTHAT = "cleanthat-javaparser-unnecessaryimport";

@taofik69
Copy link

taofik69 commented Mar 1, 2023 via email

@blacelle
Copy link
Contributor Author

blacelle commented Mar 1, 2023

I leave the default to GJF.

Javaparser still encounter an issue with annotated packages (which are quite rare, but at least one known issue).

People will be free to switch, and we may switch to cleanthat at any moment.

@blacelle blacelle marked this pull request as ready for review March 1, 2023 18:46
@blacelle
Copy link
Contributor Author

blacelle commented Mar 4, 2023

It nows handles annotated packages.

@blacelle
Copy link
Contributor Author

blacelle commented Mar 7, 2023

@nedtwigg This is a good first version.


One limitation:

It would fails with something like:

[WARNING] Issue parsing this. 1 problems. First problem: (line 26,col 2) Parse error. Found "abstract", expected one of  "enum" "exports" "module" "open" "opens" "provides" "record" "requires" "strictfp" "to" "transitive" "uses" "with" "yield" <IDENTIFIER>
Problem stacktrace : 
  com.github.javaparser.GeneratedJavaParser.generateParseException(GeneratedJavaParser.java:13793)
  com.github.javaparser.GeneratedJavaParser.jj_consume_token(GeneratedJavaParser.java:13638)
  com.github.javaparser.GeneratedJavaParser.Identifier(GeneratedJavaParser.java:3388)
  com.github.javaparser.GeneratedJavaParser.SimpleName(GeneratedJavaParser.java:3286)
  com.github.javaparser.GeneratedJavaParser.MethodDeclaration(GeneratedJavaParser.java:2044)
  com.github.javaparser.GeneratedJavaParser.ClassOrInterfaceBodyDeclaration(GeneratedJavaParser.java:1688)
  com.github.javaparser.GeneratedJavaParser.ClassOrInterfaceBody(GeneratedJavaParser.java:1200)
  com.github.javaparser.GeneratedJavaParser.ClassOrInterfaceDeclaration(GeneratedJavaParser.java:514)
  com.github.javaparser.GeneratedJavaParser.CompilationUnit(GeneratedJavaParser.java:152)
  com.github.javaparser.JavaParser.parse(JavaParser.java:125)
  com.github.javaparser.JavaParser.parse(JavaParser.java:305)

Other potential limitations:

@blacelle
Copy link
Contributor Author

blacelle commented Mar 7, 2023

Note for myself: I could try workaround the javaParser limitations.

test. case for sealed added in the PR: it appears :

@blacelle
Copy link
Contributor Author

blacelle commented Mar 7, 2023

As a side-note, while getting issues over this case before renaming some test files from .java to .test, I see:

[...]

@nedtwigg Shouldn't the faulty filename appears in the stack?

Forget this, too much code for today.

@blacelle blacelle force-pushed the removeUnusedImports_jdk17StringBlock branch from 9dcead6 to 7ad63c9 Compare March 7, 2023 16:56
@blacelle
Copy link
Contributor Author

@nedtwigg This is ready-for-merging

@nedtwigg nedtwigg enabled auto-merge April 5, 2023 22:38
@nedtwigg nedtwigg merged commit 60367e5 into diffplug:main Apr 5, 2023
@nedtwigg
Copy link
Member

nedtwigg commented Apr 6, 2023

Published in plugin-gradle 6.18.0 and plugin-maven 2.36.0.

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

| Package | Type | Update | Change |
|---|---|---|---|
| [flow-bin](https://github.com/flowtype/flow-bin) ([changelog](https://github.com/facebook/flow/blob/master/Changelog.md)) | devDependencies | minor | [`^0.206.0` -> `^0.214.0`](https://renovatebot.com/diffs/npm/flow-bin/0.206.0/0.214.0) |
| [org.liquibase.ext:liquibase-hibernate5](https://github.com/liquibase/liquibase-hibernate/wiki) ([source](https://github.com/liquibase/liquibase-hibernate)) | build | minor | `4.21.1` -> `4.22.0` |
| [org.liquibase:liquibase-maven-plugin](http://www.liquibase.org/liquibase-maven-plugin) ([source](https://github.com/liquibase/liquibase)) | build | minor | `4.21.1` -> `4.23.0` |
| [com.vladsch.flexmark:flexmark-all](https://github.com/vsch/flexmark-java) | compile | patch | `0.64.4` -> `0.64.8` |
| [com.diffplug.spotless:spotless-maven-plugin](https://github.com/diffplug/spotless) | build | minor | `2.36.0` -> `2.38.0` |

---

### Release Notes

<details>
<summary>flowtype/flow-bin</summary>

### [`v0.214.0`](flow/flow-bin@a8d35e6...ca11e28)

[Compare Source](flow/flow-bin@a8d35e6...ca11e28)

### [`v0.213.1`](flow/flow-bin@656b64a...a8d35e6)

[Compare Source](flow/flow-bin@656b64a...a8d35e6)

### [`v0.213.0`](flow/flow-bin@733e908...656b64a)

[Compare Source](flow/flow-bin@733e908...656b64a)

### [`v0.212.0`](flow/flow-bin@d057186...733e908)

[Compare Source](flow/flow-bin@d057186...733e908)

### [`v0.211.1`](flow/flow-bin@669f2d7...d057186)

[Compare Source](flow/flow-bin@669f2d7...d057186)

### [`v0.211.0`](flow/flow-bin@c0f5f12...669f2d7)

[Compare Source](flow/flow-bin@c0f5f12...669f2d7)

### [`v0.210.2`](flow/flow-bin@6dbf435...c0f5f12)

[Compare Source](flow/flow-bin@6dbf435...c0f5f12)

### [`v0.210.1`](flow/flow-bin@572b4ff...6dbf435)

[Compare Source](flow/flow-bin@572b4ff...6dbf435)

### [`v0.210.0`](flow/flow-bin@026a117...572b4ff)

[Compare Source](flow/flow-bin@026a117...572b4ff)

### [`v0.209.0`](flow/flow-bin@b1689a0...026a117)

[Compare Source](flow/flow-bin@b1689a0...026a117)

### [`v0.208.1`](flow/flow-bin@1e8564c...b1689a0)

[Compare Source](flow/flow-bin@1e8564c...b1689a0)

### [`v0.208.0`](flow/flow-bin@97db57b...1e8564c)

[Compare Source](flow/flow-bin@97db57b...1e8564c)

### [`v0.207.0`](flow/flow-bin@7bf1c0e...97db57b)

[Compare Source](flow/flow-bin@7bf1c0e...97db57b)

</details>

<details>
<summary>liquibase/liquibase-hibernate</summary>

### [`v4.22.0`](https://github.com/liquibase/liquibase-hibernate/releases/tag/v4.22.0)

[Compare Source](liquibase/liquibase-hibernate@v4.21.1...v4.22.0)

Support for Liquibase 4.22.0.

#### What's Changed

-   Fix diff changelog is removing unique constraint since 4.21.0 (hibernate6 + postgresql) by [@&#8203;filipelautert](https://github.com/filipelautert) in liquibase/liquibase-hibernate#480
-   add Support for Hibernate EnversSettings revision_field_name and revision_type_field_name by [@&#8203;lorenzbaier](https://github.com/lorenzbaier) in liquibase/liquibase-hibernate#488
-   Bump spring.version from 6.0.8 to 6.0.9 by [@&#8203;dependabot](https://github.com/dependabot) in liquibase/liquibase-hibernate#490

#### New Contributors

-   [@&#8203;lorenzbaier](https://github.com/lorenzbaier) made their first contribution in liquibase/liquibase-hibernate#488

**Full Changelog**: liquibase/liquibase-hibernate@v4.21.0...v4.22.0

</details>

<details>
<summary>liquibase/liquibase</summary>

### [`v4.23.0`](https://github.com/liquibase/liquibase/blob/HEAD/changelog.txt#Liquibase-4230-is-a-major-release)

[Compare Source](liquibase/liquibase@v4.22.0...v4.23.0)

### [`v4.22.0`](https://github.com/liquibase/liquibase/blob/HEAD/changelog.txt#Liquibase-v4220-is-a-major-release)

[Compare Source](liquibase/liquibase@v4.21.1...v4.22.0)

</details>

<details>
<summary>vsch/flexmark-java</summary>

### [`v0.64.6`](vsch/flexmark-java@0.64.4...0.64.6)

[Compare Source](vsch/flexmark-java@0.64.4...0.64.6)

</details>

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

### [`v2.38.0`](https://github.com/diffplug/spotless/blob/HEAD/CHANGES.md#&#8203;2380---2023-04-06)

##### Added

-   Support configuration of mirrors for P2 repositories in `EquoBasedStepBuilder` ([#&#8203;1629](diffplug/spotless#1629)).
-   The `style` option in Palantir Java Format ([#&#8203;1654](diffplug/spotless#1654)).
-   Added formatter for Gherkin feature files ([#&#8203;1649](diffplug/spotless#1649)).

##### Changes

-   **POTENTIALLY BREAKING** Converted `googleJavaFormat` to a compile-only dependency and drop support for versions < `1.8`. ([#&#8203;1630](diffplug/spotless#1630))
-   Bump default `cleanthat` version to latest `2.6` -> `2.13`. ([#&#8203;1589](diffplug/spotless#1589) and [#&#8203;1661](diffplug/spotless#1661))
-   Bump default `diktat` version `1.2.4.2` -> `1.2.5`. ([#&#8203;1631](diffplug/spotless#1631))
-   Bump default `flexmark` version `0.62.2` -> `0.64.0`. ([#&#8203;1302](diffplug/spotless#1302))
-   Bump default `googleJavaFormat` version `1.15.0` -> `1.16.0`. ([#&#8203;1630](diffplug/spotless#1630))
-   Bump default `scalafmt` version `3.7.1` -> `3.7.3`. ([#&#8203;1584](diffplug/spotless#1584))
-   Bump default Eclipse formatters for the 2023-03 release. ([#&#8203;1662](diffplug/spotless#1662))
    -   JDT and GrEclipse `4.26` -> `4.27`
        -   Improve GrEclipse error reporting. ([#&#8203;1660](diffplug/spotless#1660))
    -   CDT `11.0` -> `11.1`

### [`v2.37.0`](https://github.com/diffplug/spotless/blob/HEAD/CHANGES.md#&#8203;2370---2023-03-13)

##### Added

-   You can now put the filename into a license header template with `$FILE`. ([#&#8203;1605](diffplug/spotless#1605) fixes [#&#8203;1147](diffplug/spotless#1147))

##### Changes

-   We are now opting in to Gradle's new stable configuration cache. ([#&#8203;1591](diffplug/spotless#1591))
-   Adopt [Equo Solstice OSGi and p2 shim](https://github.com/equodev/equo-ide/tree/main/solstice) to update all Eclipse-based plugins. ([#&#8203;1524](diffplug/spotless#1524))
    -   Eclipse JDT now supports `4.9` through `4.26`. Also we now recommend dropping the last `.0`, e.g. `4.26` instead of `4.26.0`, you'll get warnings to help you switch.
    -   Eclipse Groovy now supports `4.18` through `4.26`. Also we now recommend dropping the last `.0`, e.g. `4.26` instead of `4.26.0`, you'll get warnings to help you switch.
    -   Eclipse CDT now supports `10.6` through `11.0`.
    -   Eclipse WTP is still WIP at [#&#8203;1622](diffplug/spotless#1622).

</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.

3 participants