From 2580c3f6ac4b77403b89da936e7d961c9a6cf111 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Kwiecin=CC=81ski?= Date: Thu, 22 Feb 2024 21:28:57 +0100 Subject: [PATCH 1/2] Update wrapper scripts --- gradlew.bat | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/gradlew.bat b/gradlew.bat index 6689b85b..7101f8e4 100644 --- a/gradlew.bat +++ b/gradlew.bat @@ -43,11 +43,11 @@ set JAVA_EXE=java.exe %JAVA_EXE% -version >NUL 2>&1 if %ERRORLEVEL% equ 0 goto execute -echo. -echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. -echo. -echo Please set the JAVA_HOME variable in your environment to match the -echo location of your Java installation. +echo. 1>&2 +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 goto fail @@ -57,11 +57,11 @@ set JAVA_EXE=%JAVA_HOME%/bin/java.exe if exist "%JAVA_EXE%" goto execute -echo. -echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% -echo. -echo Please set the JAVA_HOME variable in your environment to match the -echo location of your Java installation. +echo. 1>&2 +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 goto fail From 29eb8ae8d34d350075b9441ab25f2bc3dbcb4a64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Kwiecin=CC=81ski?= Date: Thu, 22 Feb 2024 21:40:24 +0100 Subject: [PATCH 2/2] Print file path when ktlint fails to parse the file --- .../usefulness/tasks/workers/KtlintWorker.kt | 50 +++++++++++-------- .../functional/KotlinProjectTest.kt | 28 +++++++++++ 2 files changed, 57 insertions(+), 21 deletions(-) diff --git a/ktlint-gradle-plugin/src/main/kotlin/io/github/usefulness/tasks/workers/KtlintWorker.kt b/ktlint-gradle-plugin/src/main/kotlin/io/github/usefulness/tasks/workers/KtlintWorker.kt index 2ccd4524..174ad185 100644 --- a/ktlint-gradle-plugin/src/main/kotlin/io/github/usefulness/tasks/workers/KtlintWorker.kt +++ b/ktlint-gradle-plugin/src/main/kotlin/io/github/usefulness/tasks/workers/KtlintWorker.kt @@ -36,8 +36,10 @@ internal abstract class KtlintWorker : WorkAction { changedEditorconfigFiles = parameters.changedEditorConfigFiles, logger = logger, ) - logger.info("$name - resolved ${ktLintEngine.ruleProviders.size} RuleProviders") - logger.info("$name - executing against ${files.count()} file(s)") + if (logger.isInfoEnabled) { + logger.info("$name - resolved ${ktLintEngine.ruleProviders.size} RuleProviders") + logger.info("$name - executing against ${files.count()} file(s)") + } if (logger.isDebugEnabled) { logger.debug( "Resolved RuleSetProviders = ${ktLintEngine.ruleProviders.joinToString { it.createNewRuleInstance().ruleId.value }}", @@ -56,31 +58,37 @@ internal abstract class KtlintWorker : WorkAction { } val fileErrors = mutableListOf() - when (parameters.mode.get()) { - KtlintRunMode.Check, - null, - -> ktLintEngine.lint( - code = Code.fromFile(file), - callback = { fileErrors.add(it.toKtlintCliErrorForLint()) }, - ) - - KtlintRunMode.Format -> { - var fileFixed = false - val fixedContent = ktLintEngine.format( + runCatching { + when (parameters.mode.get()) { + KtlintRunMode.Check, + null, + -> ktLintEngine.lint( code = Code.fromFile(file), - callback = { error, corrected -> - if (corrected) { - fileFixed = true - } - fileErrors.add(error.toKtlintCliErrorForFormat(corrected)) - }, + callback = { fileErrors.add(it.toKtlintCliErrorForLint()) }, ) - if (fileFixed) { - file.writeText(fixedContent) + KtlintRunMode.Format -> { + var fileFixed = false + val fixedContent = ktLintEngine.format( + code = Code.fromFile(file), + callback = { error, corrected -> + if (corrected) { + fileFixed = true + } + fileErrors.add(error.toKtlintCliErrorForFormat(corrected)) + }, + ) + + if (fileFixed) { + file.writeText(fixedContent) + } } } } + .onFailure { throwable -> + logger.quiet("ktlint failed when parsing file: ${file.path}") + throw throwable + } if (fileErrors.isNotEmpty()) { errors += KtlintErrorResult( file = file, diff --git a/ktlint-gradle-plugin/src/test/kotlin/io/github/usefulness/functional/KotlinProjectTest.kt b/ktlint-gradle-plugin/src/test/kotlin/io/github/usefulness/functional/KotlinProjectTest.kt index ff49bf92..45a76e38 100644 --- a/ktlint-gradle-plugin/src/test/kotlin/io/github/usefulness/functional/KotlinProjectTest.kt +++ b/ktlint-gradle-plugin/src/test/kotlin/io/github/usefulness/functional/KotlinProjectTest.kt @@ -475,6 +475,34 @@ internal class KotlinProjectTest : WithGradleTest.Kotlin() { } } + @Test + fun `behavior on non-compiling code`() { + settingsFile() + buildFile() + + val className = "KotlinClass" + kotlinSourceFile( + "$className.kt", + """ + class $className { + private fun hi() = // this does not compile + } + + """.trimIndent(), + ) + + val expectedFilPath = "/src/main/kotlin/KotlinClass.kt".replace("/", File.separator) + buildAndFail("lintKotlin").apply { + assertThat(output).contains("ktlint failed when parsing file").contains(expectedFilPath) + assertThat(task(":lintKotlinMain")?.outcome).isEqualTo(TaskOutcome.FAILED) + } + + buildAndFail("formatKotlin").apply { + assertThat(output).contains("ktlint failed when parsing file").contains(expectedFilPath) + assertThat(task(":formatKotlinMain")?.outcome).isEqualTo(TaskOutcome.FAILED) + } + } + private fun settingsFile() = settingsFile.apply { writeText("rootProject.name = 'ktlint-gradle-test-project'") }