Skip to content

FormatterService.fixImports formats the whole file out of process #221

Closed

Description

@abashev

What happens

FormatterService.fixImports formats the whole file when the formatter runs out of process. BootstrappingFormatterService.fixImports and NativeImageFormatterService.fixImports both call runFormatterCommand(input), which runs Main - with no flags: the imports get fixed, and every line of the file is laid out again. FormatterServiceImpl.fixImports, in process, only fixes the imports.

The command line can do the right thing with --fix-imports-only; the services do not pass it. With the 21.0.2 native binary:

package p;
import java.util.Map;
import java.io.File;
import java.util.List;
class C {   List<String>   l;  Map<String,String> m;   }

open-java-format --fix-imports-only - gives

package p;
import java.util.List;
import java.util.Map;

class C {   List<String>   l;  Map<String,String> m;   }

and open-java-format -, which fixImports runs, gives the whole file formatted.

Why it matters

Nothing calls fixImports through these services today, so nobody sees it. It is what the IntelliJ plugin's import optimizer called while it existed: palantir#1078 added PalantirJavaFormatImportOptimizer on 2024-04-26, and palantir#1089 removed it on 2024-05-21 because "optimizing imports via palantir-java-format is slow". Every Optimize Imports started a JVM and reformatted the file. An import optimizer in the plugin needs this fixed first.

Proposal

  • Both services pass --fix-imports-only in fixImports.
  • FormatterServicesTest checks fixImports on the in-process, bootstrapping and native services: imports sorted and unused ones removed, the rest of the file as it was.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions