Closed Sti2nd closed 5 months ago
Please add a code sample, and .editorconfig
, and the output you get. I expect that with 'throwing an error' you do not mean that an exception is thrown (if so, include the stacktrace).
Thank you for the reply.
Could be that Ktlint is just logging with error level so I rephrased. Also added the .editorconfig
file (in details)
You have still not supplied your code sample. Of course I can add any random comment in between, but I would like to understand your use case to use comments in the import list.
But anyways, comments in the import list can not just be ignored. From the parsers perspective comments are not bound with a specific import. So when reordering the imports, assumptions have to be made to which import a comment belongs. It is too simple to assume that a comment is always associated with the next import:
// comment before foo
import foo
// comment after foo
// comment before bar
import bar
From the perspective of the parser, it is unclear that comment after foo
should be kept close to the import foo
while comment before bar
(and the blank line) have to be kept together with import bar
.
Thank you for asking for the use case! The use case is that we use ktlint for auto-formatting. I am used to Prettier and CSharpier which are just formatters, so I have actually forgotten ktlint is more than a formatter.
We run ktlint on compile (and git commit). I believe these are the relevant gradle tasks
tasks.withType<KotlinCompile> {
dependsOn("addPreCommitHook")
dependsOn("ktlintFormat")
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "21"
}
}
tasks.withType<KtLintCheckTask> {
dependsOn("ktlintFormat")
}
Running a test requires compilation first, which requires ktlint to be happy. Ktlint will complain about unused imports, so then I figured commenting out the unused imports could make ktlint happy. Code example:
// import org.springframework.http.ProblemDetail Uncommented because it is unused right now and I want to run a test
import org.springframework.http.ResponseEntity
import org.springframework.validation.annotation.Validated
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestMethod
import org.springframework.web.bind.annotation.RestController
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.Parameter
import io.swagger.v3.oas.annotations.media.Content
import io.swagger.v3.oas.annotations.media.Schema
import io.swagger.v3.oas.annotations.responses.ApiResponse
import jakarta.validation.Valid
You might consider to add suppression below (must be before the package
statement):
@file:Suppress("ktlint:standard:no-unused-imports")
In this way you do not need to (de)comment the import. Downside is that unused imports in this particular file will not be reported anymore.
Hm. I think you are right. If we just want formatting, removing unused imports are out of scope (i.e. linting). Do you know of an easy way to disable all rules that cannot be auto-fixed? Or can Ktlint run in "formatting mode" and disable linting?
A rule can emit both violations that can be autocorrected as well as errors that can not be autocorrected. So there is no easy fix to disable rules that can not be autocorrected.
But it looks that you just want to run format, and ignore it's return code. All violations with an autocorrect will be fixed. At least this works with the ktlint CLI. I don't know how the ktlint gradle plugin handles this.
Good thinking! Indeed I am just interested in the "side-effect" that is formatting. I will look into how I can ignore errors in gradle. Will try to remember to update this thread if I find something useful. And thank you for the good help :)
Expected Behavior
I expect Ktlint to proceeed (as opposed to stop and log error) when there are comments in the code. As an anti-bikeshedding tool it is weird that Ktlint wants us to move comments before sorting imports (i.e. bike-shedding activities). Other tools (like IntelliJ) do not stop when one run format or organize imports with comments in them.
Current Behavior
Ktlint logs the following error:
Additional information
Ktlint already recognises comments, so why not ignore them insted of stopping and logging.
.editorconfig