From 6d0d4021a4d0369a96f69710ec0bf66bce002ea0 Mon Sep 17 00:00:00 2001 From: Paul Dingemans Date: Wed, 18 Sep 2024 16:53:57 +0200 Subject: [PATCH] Return exit code 1 in case violations have been found but file is unchanged (#2803) In very rare cases it is possible that Lint violations are detected while formatting but that they have opposite effects and the file gets not altered at all. As the logging already might contain the message "Format was not able to resolve all violations which (theoretically) can be autocorrected in file ..." the format may not return with exit code 0. Closes #2795 --- .../com/pinterest/ktlint/cli/internal/KtlintCommandLine.kt | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/ktlint-cli/src/main/kotlin/com/pinterest/ktlint/cli/internal/KtlintCommandLine.kt b/ktlint-cli/src/main/kotlin/com/pinterest/ktlint/cli/internal/KtlintCommandLine.kt index 92e04f72a5..41c5fa2a7b 100644 --- a/ktlint-cli/src/main/kotlin/com/pinterest/ktlint/cli/internal/KtlintCommandLine.kt +++ b/ktlint-cli/src/main/kotlin/com/pinterest/ktlint/cli/internal/KtlintCommandLine.kt @@ -508,6 +508,11 @@ internal class KtlintCommandLine : } } ?: NO_AUTOCORRECT }.also { formattedFileContent -> + if (ktlintCliErrors.isNotEmpty() && code.content == formattedFileContent) { + // In very rare cases it is possible that Lint violations are detected but that they have opposite effects and the + // file gets not altered at all. This is to be treated as unfixable error. + containsUnfixedLintErrors.set(true) + } if (forceLintAfterFormat && code.content != formattedFileContent) { // Rerun lint to check that the formatted code can still be successfully parsed. try { @@ -721,7 +726,7 @@ internal class KtlintCommandLine : q.put(executorService.submit(task)) } q.put(pill) - } catch (e: InterruptedException) { + } catch (_: InterruptedException) { // we've been asked to stop consuming sequence } finally { executorService.shutdown()