-
Notifications
You must be signed in to change notification settings - Fork 507
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Passing the option --patterns-from-stdin=
throws an IllegalArgumentException
#2578
Comments
Seems like the problem was introduced in commit cb17bbf when switching from picocli to clikt: diff diff --git ktlint-cli/src/main/kotlin/com/pinterest/ktlint/cli/internal/KtlintCommandLine.kt ktlint-cli/src/main/kotlin/com/pinterest/ktlint/cli/internal/KtlintCommandLine.kt
index 36e84c38..039e4746 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
[…]
@@ -338,42 +334,46 @@ internal class KtlintCommandLine {
return EditorConfigDefaults.load(fullyExpandedEditorConfigPath, ruleProviders.propertyTypes())
}
- private fun List<String>.replaceWithPatternsFromStdinOrDefaultPatternsWhenEmpty(): List<String> {
- val localStdinDelimiter: String? = stdinDelimiter
- return when {
- localStdinDelimiter != null -> {
- val stdinPatterns: Set<String> = readPatternsFromStdin(localStdinDelimiter.ifEmpty { "\u0000" })
- if (isNotEmpty() && stdinPatterns.isNotEmpty()) {
- logger.warn {
- "Patterns specified at command line ($this) and patterns from 'stdin' due to flag '--patterns-from-stdin' " +
- "($stdinPatterns) are merged"
+ private fun List<String>.replaceWithPatternsFromStdinOrDefaultPatternsWhenEmpty(): List<String> =
+ when {
+ patternsFromStdin != null -> {
+ readPatternsFromStdin(patternsFromStdin!!)
+ .let { stdinPatterns ->
+ if (stdinPatterns.isNotEmpty()) {
+ if (isEmpty()) {
+ logger.debug { "Patterns read from 'stdin' due to flag '--patterns-from-stdin': $stdinPatterns" }
+ } else {
+ logger.warn {
+ "Patterns specified at command line ($this) and patterns from 'stdin' due to flag " +
+ "'--patterns-from-stdin' ($stdinPatterns) are merged"
+ }
+ }
+ }
+ // Note: it is okay in case both the original patterns and the patterns from stdin are empty
+ this.plus(stdinPatterns)
}
- }
- // Note: it is okay in case both the original patterns and the patterns from stdin are empty
- this.plus(stdinPatterns)
}
+
this.isEmpty() -> {
logger.info { "Enable default patterns $DEFAULT_PATTERNS" }
DEFAULT_PATTERNS
}
+
else -> {
// Keep original patterns
this
}
}
- }
[…] |
Either of these changes should fix the issue: (both untested) diff --git ktlint-cli/src/main/kotlin/com/pinterest/ktlint/cli/internal/KtlintCommandLine.kt ktlint-cli/src/main/kotlin/com/pinterest/ktlint/cli/internal/KtlintCommandLine.kt
index 8aab056c..184376a2 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
@@ -337,7 +337,7 @@ internal class KtlintCommandLine :
private fun List<String>.replaceWithPatternsFromStdinOrDefaultPatternsWhenEmpty(): List<String> =
when {
patternsFromStdin != null -> {
- readPatternsFromStdin(patternsFromStdin!!)
+ readPatternsFromStdin(patternsFromStdin!!.ifEmpty { "\u0000" })
.let { stdinPatterns ->
if (stdinPatterns.isNotEmpty()) {
if (isEmpty()) {
@@ -638,7 +638,9 @@ internal class KtlintCommandLine :
}
private fun readPatternsFromStdin(delimiter: String): Set<String> {
- require(delimiter.isNotEmpty())
+ require(delimiter.isNotEmpty()) {
+ "Stdin delimiter must not be empty"
+ }
return String(System.`in`.readBytes())
.split(delimiter)
diff --git ktlint-cli/src/main/kotlin/com/pinterest/ktlint/cli/internal/KtlintCommandLine.kt ktlint-cli/src/main/kotlin/com/pinterest/ktlint/cli/internal/KtlintCommandLine.kt
index 8aab056c..ae640534 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
@@ -638,10 +638,8 @@ internal class KtlintCommandLine :
}
private fun readPatternsFromStdin(delimiter: String): Set<String> {
- require(delimiter.isNotEmpty())
-
return String(System.`in`.readBytes())
- .split(delimiter)
+ .split(delimiter.ifEmpty { "\u0000" })
.let { patterns: List<String> ->
patterns.filterTo(LinkedHashSet(patterns.size), String::isNotEmpty)
}
|
Example:
|
The previous behavior was that passing the option $ ktlint --version
1.1.1
$ printf 'foo\0bar\0' | ktlint --patterns-from-stdin=
<timestamp> [main] WARN com.pinterest.ktlint.cli.internal.KtlintCommandLine -- No files matched [foo, bar]
$ printf 'foo\nbar\n' | ktlint --patterns-from-stdin
<timestamp> [main] WARN com.pinterest.ktlint.cli.internal.KtlintCommandLine -- No files matched [foo, bar] The behavior with no/omitted argument (i.e.: no $ ktlint --version
1.2.0
$ printf 'foo\nbar\n' | ktlint --patterns-from-stdin
<timestamp> [main] WARN com.pinterest.ktlint.cli.internal.KtlintCommandLine -- No files matched [foo, bar] But I was expecting that the behavior with an empty argument (NUL byte as pattern delimiter) would be the same. |
Yes, I just found the problem is caused by the default value I have provided. I think that I previously did not understand the real behavior of this option. Your second solution does work:
I will restore the old behavior. |
Since version
1.2.0
, passing the option--patterns-from-stdin=
(empty string as argument) will cause anIllegalArgumentException
to be thrown.Expected Behavior
Executing the command
ktlint --patterns-from-stdin=
should not throw an exception.Observed Behavior
Executing the command
ktlint --patterns-from-stdin=
throws the following exception:Steps to Reproduce
Simply execute the command
ktlint --patterns-from-stdin=
.Your Environment
1.2.0
The text was updated successfully, but these errors were encountered: