google / google-java-format

Reformats Java source code to comply with Google Java Style.
Other
5.48k stars 848 forks source link

IntelliJ IDEA (1.16) - Parsing error at import time #911

Open Djaytan opened 1 year ago

Djaytan commented 1 year ago

Hello 👋

How to reproduce:

  1. In a Java file, simply start writing a class name which require an import and is not imported yet (e.g. List)
  2. Use the IntelliJ IDEA auto-import feature => google-java-format will send immediately after a parsing error message + will not format properly import:

Example:

import java.util.List;import javax.inject.Singleton;

This issue is not present with the previous version of the plugin (1.15)

Environment details

IntelliJ IDEA 2022.3.2 (Ultimate Edition) Build #IU-223.8617.56, built on January 26, 2023 Runtime version: 17.0.5+1-b653.25 amd64 VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o. Windows 10 10.0 GC: G1 Young Generation, G1 Old Generation Memory: 2048M Cores: 20 Registry: debugger.new.tool.window.layout=true ide.experimental.ui=true

Non-Bundled Plugins: com.wakatime.intellij.plugin (14.1.4) google-java-format (1.16.0.0) org.jetbrains.plugins.hocon (2022.1.0) MavenRunHelper (4.23.222.2964.0) org.sonarlint.idea (8.0.0.63273) zielu.gittoolbox (500.0.4+213) mobi.hsz.idea.gitignore (4.4.4)

Kotlin: 223-1.8.0-release-345-IJ8617.56

Djaytan commented 1 year ago

I downgraded to version 1.15 and I confirm that the issue isn't present

plumpy commented 1 year ago

The old version did not optimize your imports. Would you like an option to disable optimizing imports?

There's really no other way around this.

plumpy commented 1 year ago

Some additional clarification here:

Since the old plugin didn't optimize imports, the built-in IDE import optimizer handled that. If you had taken the time to configure your IDE to sort imports the same way that google-java-format does, then everything would work okay (and the IDE's import optimizer doesn't require a syntactically correct file to work). But it did require you to take that extra step.

The new plugin handles import ordering for you, so it'll work out of the box, no additional configuration needed...but with this one additional caveat that it only works on files without syntax errors.

Djaytan commented 1 year ago

@plumpy Thanks for your answer

I already have manually configured the import ordering as described in the README, so if I should choice between both versions, it seems better for me to stay with version 1.15 since in-fine the UX is better

So, if the issue can be solved by having an option to disable optimizing imports from plugin to rely on the configured one it would perfectly feet my needs as well for those who would be motivated to follow few additional steps to have a clean UX

jbduncan commented 1 year ago

I already have manually configured the import ordering as described in the README...

@Djaytan Which part of the README are you referring to? I can't seem to find anything on imports. 🤔

plumpy commented 1 year ago

@jbduncan I removed it from the README since it's not necessary anymore. But you can see the old version here: https://github.com/google/google-java-format/blob/v1.15.0/README.md#intellij-android-studio-and-other-jetbrains-ides

ahaczewski commented 1 year ago

The old version did not optimize your imports. Would you like an option to disable optimizing imports?

There's really no other way around this.

Hi @plumpy,

We do relied on the previous IntelliJ plugin not optimizing imports. We also set --skip-sorting-imports for our automated formatting in CI. Is it possible to have a switch in IntelliJ that does what the --skip-sorting-imports does for the command line?

tgeng commented 1 year ago

This upgrade basically makes the plugin unusable: if any IDE action needs to touch the import yet the generated/completed code is not syntactically correct yet (since code is not completed yet), google-java-format would simply fail to format the file and report error. I am not sure if there is a way to differentiate reformat request from an IDE action generating new code from reformat request from user hitting reformat command or trigger reformat on save.

SenreySong commented 1 year ago

When using a new version of a plugin, it can break IntelliJ IDEA's auto import functionality, which can disrupt code completion and cause the related code generation features, such as generating constructors, to stop working.

smarthkatyal commented 1 year ago

I think when there are syntax errors, it should let the IDE handle things as they were being done before this change. Now, when there are syntax errors, the problem is getting a bit compounded because i see an error in the IJ notification pane about the plugin failing to format and also the imports are just dumped into one long line requiring "manual" fix by putting the imports on the new line. Sometimes syntax errors can take a big changeset to be fixed completely and not being able to format till then is not ideal.

guillaume-alvarez commented 1 year ago

I have the same issue: when generating code it is not automatically free of syntax errors and thus is badly formatted to the point of being unreadable.... and it generates lots of warning popups for something that should only be a transient state.

Is it possible to revert to the previous plugin version?

stefanscheidt commented 1 year ago

@guillaume-alvarez Yes, you can download the version you want (probably 1.15.0.0 - works fine for me) here and then install it from disk.

Please note that with version 1.15.0.0 and before you also need to use this code style thingy to get proper import organization.

jzanon commented 1 year ago

Same problem here than @guillaume-alvarez . I use using IntelliJ IDEA 2022.3.3. + plugin google-java-format (1.16.0.1) I had to downgrade plugin google-java-format to 1.15.0.0.

aaime commented 1 year ago

Same here.. the latest version is not usable, takes a lot of time for format the files, plays pretty poorly with IntelliJ code generation (e.g., creating getters/setters, constructors and the like). Reverting to 1.15.0.0 fixes the problem.

dangolbeeker commented 1 year ago

Same issue

binhnv commented 1 year ago

Have the same issue and it makes this plugin unusable

mattnworb commented 1 year ago

I think the problem is broader than when a missing import causes syntax/compiler errors - I get the same sort of behavior (" google-java-format failed. Does Foobar.java have syntax errors?", IntelliJ-generated code appearing on the same line as other existing code without newlines being inserted) any time the file is incomplete and the formatter is running. It seems like the new formatter interferes with general code generation abilities of IntelliJ

raphaelNguyen commented 1 year ago

Same issue here