Skip to content

Commit

Permalink
Merge pull request #177 from usefulness/updates
Browse files Browse the repository at this point in the history
Print file path when `ktlint` fails to parse the file
  • Loading branch information
mateuszkwiecinski authored Feb 22, 2024
2 parents 2c799cd + 29eb8ae commit ab2d2a3
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 31 deletions.
20 changes: 10 additions & 10 deletions gradlew.bat
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ internal abstract class KtlintWorker : WorkAction<KtlintWorker.Parameters> {
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 }}",
Expand All @@ -56,31 +58,37 @@ internal abstract class KtlintWorker : WorkAction<KtlintWorker.Parameters> {
}

val fileErrors = mutableListOf<KtlintCliError>()
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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'")
}
Expand Down

0 comments on commit ab2d2a3

Please sign in to comment.