diff --git a/CHANGELOG.md b/CHANGELOG.md index 099c580188..5ab20dc245 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,6 +32,8 @@ The "visit" life cycle hook will be removed in Ktlint 0.48. In KtLint 0.47 the " ### Changed +* Print an error message and return with non-zero exit code when no files are found that match with the globs ([#629](https://github.com/pinterest/ktlint/issue/629)). + ### Removed ## [0.46.1] - 2022-06-21 diff --git a/ktlint/src/main/kotlin/com/pinterest/ktlint/internal/FileUtils.kt b/ktlint/src/main/kotlin/com/pinterest/ktlint/internal/FileUtils.kt index 65d10b0e0c..0c9ffacb76 100644 --- a/ktlint/src/main/kotlin/com/pinterest/ktlint/internal/FileUtils.kt +++ b/ktlint/src/main/kotlin/com/pinterest/ktlint/internal/FileUtils.kt @@ -26,7 +26,7 @@ private val tildeRegex = Regex("^(!)?~") private val os = System.getProperty("os.name") private val userHome = System.getProperty("user.home") -private val defaultPatterns = setOf( +internal val defaultPatterns = setOf( "**$globSeparator*.kt", "**$globSeparator*.kts" ) diff --git a/ktlint/src/main/kotlin/com/pinterest/ktlint/internal/KtlintCommandLine.kt b/ktlint/src/main/kotlin/com/pinterest/ktlint/internal/KtlintCommandLine.kt index 63a97a25d8..f18f30b6e2 100644 --- a/ktlint/src/main/kotlin/com/pinterest/ktlint/internal/KtlintCommandLine.kt +++ b/ktlint/src/main/kotlin/com/pinterest/ktlint/internal/KtlintCommandLine.kt @@ -211,6 +211,13 @@ internal class KtlintCommandLine { failOnOldRulesetProviderUsage() + // Set default value to patterns only after the logger has been configured to avoid a warning about initializing + // the logger multiple times + if (patterns.isEmpty()) { + logger.info { "Enable default patterns $defaultPatterns" } + patterns = ArrayList(defaultPatterns) + } + val start = System.currentTimeMillis() val baselineResults = loadBaseline(baseline) @@ -242,6 +249,10 @@ internal class KtlintCommandLine { ) } reporter.afterAll() + if (fileNumber.get() == 0) { + logger.error { "No files matched $patterns" } + exitProcess(1) + } logger.debug { "${System.currentTimeMillis() - start}ms / $fileNumber file(s) / $errorNumber error(s)" } if (tripped.get()) { exitProcess(1) @@ -281,8 +292,7 @@ internal class KtlintCommandLine { ruleSets ) } - } - .parallel({ (file, errList) -> report(file.location(relative), errList, reporter) }) + }.parallel({ (file, errList) -> report(file.location(relative), errList, reporter) }) } private fun lintStdin( diff --git a/ktlint/src/test/kotlin/com/pinterest/ktlint/SimpleCLITest.kt b/ktlint/src/test/kotlin/com/pinterest/ktlint/SimpleCLITest.kt index eadf750e77..d7be6368d9 100644 --- a/ktlint/src/test/kotlin/com/pinterest/ktlint/SimpleCLITest.kt +++ b/ktlint/src/test/kotlin/com/pinterest/ktlint/SimpleCLITest.kt @@ -63,6 +63,20 @@ class SimpleCLITest : BaseCLITest() { } } + @Test + fun `Given some code with an error but a glob which does not select the file`() { + runKtLintCliProcess( + "too-many-empty-lines", + listOf("SomeOtherFile.kt") + ) { + assertErrorExitCode() + + assert(normalOutput.find { it.contains("No files matched [SomeOtherFile.kt]") } != null) { + "Unexpected output:\n${normalOutput.joinToString(separator = "\n")}" + } + } + } + @Test fun `Given some code with an error which can be autocorrected then return from from with the normal exit code`() { runKtLintCliProcess( @@ -78,14 +92,17 @@ class SimpleCLITest : BaseCLITest() { } @Test - fun `Given some code with an error for a rule which is disabled via CLI argument --disabled_rules then return from lint with the normal exit code and without error output`() { + fun `Given some code which only contains errors for rules which are disabled via CLI argument --disabled_rules then return from lint with the normal exit code and without error output`() { runKtLintCliProcess( "too-many-empty-lines", - listOf("disabled_rules=no-consecutive-blank-lines") + listOf("--disabled_rules=no-consecutive-blank-lines,no-empty-first-line-in-method-block") ) { assertNormalExitCode() - assertThat(normalOutput).doesNotContain("Needless blank line(s)") + assertThat(normalOutput).doesNotContain( + "no-consecutive-blank-lines", + "no-empty-first-line-in-method-block" + ) } } }