question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Spotless with Eclipse Import Order has different ordering than Eclipse Formatter

See original GitHub issue

If you are submitting a bug, please include the following:

  • summary of problem Spotless with eclipse formatter orders static imports differently than eclipse or eclipse plugin in Intellij
  • gradle or maven version mvn 3.3.3
  • spotless version 1.27.0
  • operating system and version Description: Ubuntu 16.04.6 LTS
  • copy-paste your full Spotless configuration block(s), and a link to a public git repo that reproduces the problem if possible
                   <java>
                       <importOrder>
                           <file>/style/myTeam.importorder</file>
                       </importOrder>
                   </java>
               </configuration>

with order

#Organize Import Order
#Fri Jun 28 15:00:43 PDT 2019
4=com.myTeam
3=java
2=javax
1=
0=\#
  • copy-paste the full content of any console errors emitted by gradlew spotless[Apply/Check] --stacktrace N/A

If you’re just submitting a feature request or question, no need for the above.

EclipseFormatter issues https://github.com/krasa/EclipseCodeFormatter/issues/200 https://github.com/krasa/EclipseCodeFormatter/issues/105

Write

package somePackage;

import static somePackage.Foo.SomeEnum.Bar;
import static somePackage.Foo.SomeZClass;
import static somePackage.Foo.SomeZClass.m;

import java.util.function.Supplier;

// Use as a type argument to be a static import.
public class Foo implements Supplier<SomeZClass> {

    public void someMethod() {
        SomeEnum e = Bar;
        m(); // call m with a static import
    }

    @Override
    public SomeZClass get() {
        return null;
    }

    public enum SomeEnum {
        Bar
    }

    // Add Z this should be sorted after SomeEnum
    public static class SomeZClass {
        static void m() {}
    }
}

Eclipse Plugin will order imports:

import static somePackage.Foo.SomeZClass;
import static somePackage.Foo.SomeEnum.Bar;
import static somePackage.Foo.SomeZClass.m;

Eclipse will order imports:

import static somePackage.Foo.SomeEnum.Bar;
import static somePackage.Foo.SomeZClass.m;

import java.util.function.Supplier;

import somePackage.Foo.SomeZClass;

and spotless will order imports


import static somePackage.Foo.SomeEnum.Bar;
import static somePackage.Foo.SomeZClass;
import static somePackage.Foo.SomeZClass.m;

import java.util.function.Supplier;

Spotless using Eclipse format rules should have the same behavior as Eclipse

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:1
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
cimicommented, Jul 21, 2020

So, in a nutshell the problem is that Spotless tries to arrange imports of packages that are not specified in “importorder” config in alphabetical order with respect to other imports. Eclipse Code Fromatter, in turn, places such imports at the end of the imports block.

@ekropotin we can work around this issue by adding an additional item to the .importorder file that forces unspecified imports to go at the end, for example:

#Organize Import Order
#Mon Apr 15 23:51:55 CEST 2019
0=java
1=javax
2=org
3=com
4=

This makes spotless formatting consistent with IDEA/Eclipse code formatter, so I’m able to use save actions with reformat on save without causing any conflicts.

2reactions
ekropotincommented, Apr 24, 2020

@nedtwigg yes it is. So, in a nutshell the problem is that Spotless tries to arrange imports of packages that are not specified in “importorder” config in alphabetical order with respect to other imports. Eclipse Code Fromatter, in turn, places such imports at the end of the imports block. Below is an example: build.gradle:

plugins {
    // Language plugins
    id 'java'
    id "com.diffplug.gradle.spotless" version "3.28.1"
}

repositories {
    maven { url "https://plugins.gradle.org/m2/" }
    mavenCentral()
    jcenter()
}

dependencies {
    implementation 'io.opencensus:opencensus-api:0.26.0'
}

spotless {
    java {
        importOrderFile 'Test.importorder'
        eclipse().configFile 'CodeStyle.xml'
    }
}

Test.importorder:

5=org
4=com
0=com.test

The file pre-formatted with ECF with Test.importorder:

package com.test;

import com.test.foo.Bar;

import org.xml.sax.SAXException;

import io.grpc.Deadline;

public class Example {

    public void test() throws SAXException {
        Deadline.getSystemTicker();
        throw new SAXException(Bar.BAR);
    }
}

Output of gradle spotlessCheck:

> Task :spotlessJava FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':spotlessJava'.
> The following files had format violations:
      src/main/java/com/test/Example.java
          @@ -2,10 +2,10 @@

           import·com.test.foo.Bar;

          +import·io.grpc.Deadline;
          +
           import·org.xml.sax.SAXException;

          -import·io.grpc.Deadline;
          -
           public·class·Example·{

           ····public·void·test()·throws·SAXException·{
  Run 'gradlew spotlessApply' to fix these violations.
Read more comments on GitHub >

github_iconTop Results From Across the Web

Is it possible for intellij to organize imports the same way as in ...
I set up eclipse code formatter including formatting and import order. Sometimes I ended up importing with * although the intellij formatting scheme...
Read more >
534342 – [formatter] Add option to separate static imports from ...
There is no way to configure the Organize Imports action in Eclipse to add this blank line (without adding blank lines between all...
Read more >
Google Java Style Guide
Imports are ordered as follows: ... If there are both static and non-static imports, a single blank line separates the two blocks. There...
Read more >
Eclipse Order Of Imports - ADocLib
Due to some legacy third party code, we have import packages Spotless with Eclipse Import Order has different ordering than Eclipse Formatter #522....
Read more >
Imports - checkstyle
To configure the check so that it matches default Eclipse formatter configuration (tested on Kepler and Luna releases):.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found