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

import java.util.* ignored as wildcard import #1614

Closed
jbruchanov opened this issue Aug 27, 2022 · 6 comments
Closed

import java.util.* ignored as wildcard import #1614

jbruchanov opened this issue Aug 27, 2022 · 6 comments

Comments

@jbruchanov
Copy link

jbruchanov commented Aug 27, 2022

Expected Behavior

Failing ktlintCheck for wildcard import rule

import java.util.*

Observed Behavior

ktlintCheck task finishes as success

Steps to Reproduce

.editorconfig

[*.{kt,kts}]
disabled_rules=comment-spacing,import-ordering
max_line_length=180

ktlint.gradle

apply plugin: "org.jlleitschuh.gradle.ktlint"
//https://github.com/jlleitschuh/ktlint-gradle

ktlint {
    version.set(project["ktLint"])
    verbose.set(true)
    android.set(true)
    outputToConsole.set(true)
    outputColorName.set("RED")
}

test.kt

import java.util.*

fun test() {
    val d = Date()
}

run ./gradlew ktlintCheck
(doesn't fail even for case of unused imports) (doesn't seem to be an issue for 0.47.0)
might be related to #1256

Your Environment

ktLintPlugin=11.0.0
ktLint=0.45.2 (due to incompatibility plugin vs ktlint 0.46+, this seems to be latest working version)

@paul-dingemans
Copy link
Collaborator

I am failing to understand your issue.

You mention at the start of the issue a problem with the wildcard import rule (e.g. no-wildcard-imports rule) that it does not report import java.util.*. When I use ktlint CLI with version 0.45.2, 0.46.0, 0.46.1 and 0.47.0 the wildcard is reported with all versions. Would you mind trying to run ktlint -a -v on your project with the CLI version (https://pinterest.github.io/ktlint/install/cli/). Please send the log file in case the problem persists.

The part that is confusing is that at the end of the issue you mention that in sample below ktlint even doesn't fail for the case of unused imports:

import java.util.*

fun test() {
    val d = Date()
}

In this sample java.util.* is required for the Date() so it is correct that the import is not marked as "unused". But it should report the wildcard import.

@jbruchanov
Copy link
Author

jbruchanov commented Aug 27, 2022

@paul-dingemans
import java.util.* is just being ignored as wildcard import, I expect to be marked as unwanted as following example for com.test.*

used: curl -sSLO https://github.com/pinterest/ktlint/releases/download/0.47.0/ktlint
running java -jar ktlint *.kt with only Test.kt in the folder

Example 1

import com.test.*
import com.test2.*
import java.util.*

fun test() {
    val d = Date()
}

output: (only 2 errors detected)

c:\Temp\ktlint>java -jar ktlint *.kt
c:\Temp\ktlint\Test.kt:1:1: Wildcard import (cannot be auto-corrected) (no-wildcard-imports)
c:\Temp\ktlint\Test.kt:2:1: Wildcard import (cannot be auto-corrected) (no-wildcard-imports)
c:\Temp\ktlint>

Example 2

import java.util.*

fun test() {
    val d = Date()
}

output: (unexpectedly no error for java.util.*)

c:\Temp\ktlint>java -jar ktlint *.kt
c:\Temp\ktlint>

@jbruchanov
Copy link
Author

jbruchanov commented Aug 27, 2022

and the log of java -jar ktlint -a -v *.kt for Example 2 file
log.txt

@jbruchanov
Copy link
Author

just a guess,
but isn't it this what makes it ignoring ?

defaultValue = parseAllowedWildcardImports("java.util.*,kotlinx.android.synthetic.**"),

if so, can I disable it somehow ?

@paul-dingemans
Copy link
Collaborator

That is really a good catch. I can only vaguely remember that I saw this code before.

You are really close to solve this yourself. Line 94 define the default value for property ij_kotlin_packages_to_use_import_on_demand.

        public val packagesToUseImportOnDemandProperty: UsesEditorConfigProperties.EditorConfigProperty<List<PatternEntry>> =
            UsesEditorConfigProperties.EditorConfigProperty(
                type = PropertyType(
                    "ij_kotlin_packages_to_use_import_on_demand",
                    "Defines allowed wildcard imports",
                    packagesToUseImportOnDemandPropertyParser,
                ),
                /**
                 * Default IntelliJ IDEA style: Use wildcard imports for packages in "java.util", "kotlin.android.synthetic" and
                 * it's subpackages.
                 *
                 * https://github.com/JetBrains/kotlin/blob/ffdab473e28d0d872136b910eb2e0f4beea2e19c/idea/formatter/src/org/jetbrains/kotlin/idea/core/formatter/KotlinCodeStyleSettings.java#L81-L82
                 */
                defaultValue = parseAllowedWildcardImports("java.util.*,kotlinx.android.synthetic.**"),
                propertyWriter = { it.joinToString(separator = ",") },
            )

You can override that property in your .editorconfig, but this might result in conflicts with the default formatter of IntelliJ IDEA. But it should :

ij_kotlin_packages_to_use_import_on_demand=""

@jbruchanov
Copy link
Author

awesome, it does the trick, so closing it..
thanks...

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

No branches or pull requests

2 participants