Skip to content
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

Prevent stack overflow exception when code provided via stdin can not be parsed as Kotlin, nor Kotlin script #2380

Merged
merged 1 commit into from
Nov 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -538,18 +538,32 @@ internal class KtlintCommandLine {
}
} catch (e: Exception) {
if (code.isStdIn && e is KtLintParseException) {
logger.warn {
"""
Can not parse input from <stdin> as Kotlin, due to error below:
${e.toKtlintCliError(code).detail}
Now, trying to read the input as Kotlin Script.
""".trimIndent()
if (code.script) {
// When reading from stdin, code is only parsed as Kotlint script, if it could not be parsed as pure Kotlin. Now parsing
// of the code has failed for both, the file has to be ignored.
logger.error {
"""
Can not parse input from <stdin> as Kotlin, due to error below:
${e.toKtlintCliError(code).detail}
""".trimIndent()
}
ktlintCliErrors.add(e.toKtlintCliError(code))
} else {
// When reading from stdin, it is first assumed that the provided code is pure Kotlin instead of Kotlin script. If
// parsing fails, retry parsing at Kotlin script.
logger.warn {
"""
Can not parse input from <stdin> as Kotlin, due to error below:
${e.toKtlintCliError(code).detail}
Now, trying to read the input as Kotlin Script.
""".trimIndent()
}
return format(
ktLintRuleEngine = ktLintRuleEngine,
code = Code.fromSnippet(code.content, script = true),
baselineLintErrors = baselineLintErrors,
)
}
return format(
ktLintRuleEngine = ktLintRuleEngine,
code = Code.fromSnippet(code.content, script = true),
baselineLintErrors = baselineLintErrors,
)
} else {
ktlintCliErrors.add(e.toKtlintCliError(code))
tripped.set(true)
Expand Down Expand Up @@ -587,18 +601,32 @@ internal class KtlintCommandLine {
}
} catch (e: Exception) {
if (code.isStdIn && e is KtLintParseException) {
logger.warn {
"""
Can not parse input from <stdin> as Kotlin, due to error below:
${e.toKtlintCliError(code).detail}
Now, trying to read the input as Kotlin Script.
""".trimIndent()
if (code.script) {
// When reading from stdin, code is only parsed as Kotlint script, if it could not be parsed as pure Kotlin. Now parsing
// of the code has failed for both, the file has to be ignored.
logger.error {
"""
Can not parse input from <stdin> as Kotlin, due to error below:
${e.toKtlintCliError(code).detail}
""".trimIndent()
}
ktlintCliErrors.add(e.toKtlintCliError(code))
} else {
// When reading from stdin, it is first assumed that the provided code is pure Kotlin instead of Kotlin script. If
// parsing fails, retry parsing at Kotlin script.
logger.warn {
"""
Can not parse input from <stdin> as Kotlin, due to error below:
${e.toKtlintCliError(code).detail}
Now, trying to read the input as Kotlin Script.
""".trimIndent()
}
return lint(
ktLintRuleEngine = ktLintRuleEngine,
code = Code.fromSnippet(code.content, script = true),
baselineLintErrors = baselineLintErrors,
)
}
return lint(
ktLintRuleEngine = ktLintRuleEngine,
code = Code.fromSnippet(code.content, script = true),
baselineLintErrors = baselineLintErrors,
)
} else {
ktlintCliErrors.add(e.toKtlintCliError(code))
tripped.set(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,28 @@ class SimpleCLITest {
}
}

@Test
fun `Issue 2379 - Given stdin input resulting in a KtLintParseException when formatted as Kotlin Script code`(
@TempDir
tempDir: Path,
) {
CommandLineTestRunner(tempDir)
.run(
testProjectName = "too-many-empty-lines",
arguments = listOf("--stdin", "--format"),
stdin =
ByteArrayInputStream(
"""
fun foo() =
""".trimIndent().toByteArray(),
),
) {
assertThat(errorOutput)
.containsLineMatching(Regex(".*Not a valid Kotlin file.*"))
.doesNotContainLineMatching(Regex(".*Now, trying to read the input as Kotlin Script.*"))
}
}

@Test
fun `Enable android code style via parameter --code-style=android_studio`(
@TempDir
Expand Down