Log created error file in debug log

KT-59451 Fixed
This commit is contained in:
Nataliya.Valtman
2023-09-01 13:01:05 +02:00
committed by Space Team
parent 496e4dd0e3
commit f46245621f
7 changed files with 29 additions and 16 deletions
@@ -283,21 +283,21 @@ class BuildReportsIT : KGPBaseTest() {
} }
""".trimIndent() """.trimIndent()
) )
build("compileKotlin") { build("compileKotlin", buildOptions = defaultBuildOptions.copy(logLevel = LogLevel.DEBUG)) {
assertTrue { projectPath.resolve(kotlinErrorPath).listDirectoryEntries().isEmpty() } assertTrue { projectPath.resolve(kotlinErrorPath).listDirectoryEntries().isEmpty() }
assertOutputDoesNotContain("errors were stored into file")
} }
val kotlinFile = kotlinSourcesDir().resolve("helloWorld.kt") val kotlinFile = kotlinSourcesDir().resolve("helloWorld.kt")
kotlinFile.modify { it.replace("ArrayList", "skjfghsjk") } kotlinFile.modify { it.replace("ArrayList", "skjfghsjk") }
buildAndFail("compileKotlin") { buildAndFail("compileKotlin", buildOptions = defaultBuildOptions.copy(logLevel = LogLevel.DEBUG)) {
val buildErrorDir = projectPath.resolve(kotlinErrorPath).toFile() assertOutputContains("errors were stored into file")
val files = buildErrorDir.listFiles() val file = projectPath.getSingleFileInDir(kotlinErrorPath)
assertTrue { files?.first()?.exists() ?: false } file.bufferedReader().use { reader ->
files?.first()?.bufferedReader().use { reader -> val kotlinVersion = reader.readLine()
val kotlinVersion = reader?.readLine()
assertTrue("kotlin version should be in the error file") { assertTrue("kotlin version should be in the error file") {
kotlinVersion != null && kotlinVersion.trim().equals("kotlin version: ${buildOptions.kotlinVersion}") kotlinVersion != null && kotlinVersion.trim().equals("kotlin version: ${buildOptions.kotlinVersion}")
} }
val errorMessage = reader?.readLine() val errorMessage = reader.readLine()
assertTrue("Error message should start with 'error message: ' to parse it on IDEA side") { assertTrue("Error message should start with 'error message: ' to parse it on IDEA side") {
errorMessage != null && errorMessage.trim().startsWith("error message:") errorMessage != null && errorMessage.trim().startsWith("error message:")
} }
@@ -314,13 +314,15 @@ class BuildReportsIT : KGPBaseTest() {
@GradleTest @GradleTest
fun testErrorsFileWithCompilationError(gradleVersion: GradleVersion) { fun testErrorsFileWithCompilationError(gradleVersion: GradleVersion) {
project("simpleProject", gradleVersion) { project("simpleProject", gradleVersion) {
build("compileKotlin") { build("compileKotlin", buildOptions = defaultBuildOptions.copy(logLevel = LogLevel.DEBUG)) {
assertTrue { projectPath.resolve(kotlinErrorPath).listDirectoryEntries().isEmpty() } assertTrue { projectPath.resolve(kotlinErrorPath).listDirectoryEntries().isEmpty() }
assertOutputDoesNotContain("errors were stored into file")
} }
val kotlinFile = kotlinSourcesDir().resolve("helloWorld.kt") val kotlinFile = kotlinSourcesDir().resolve("helloWorld.kt")
kotlinFile.modify { it.replace("ArrayList", "skjfghsjk") } kotlinFile.modify { it.replace("ArrayList", "skjfghsjk") }
buildAndFail("compileKotlin") { buildAndFail("compileKotlin", buildOptions = defaultBuildOptions.copy(logLevel = LogLevel.DEBUG)) {
assertTrue { projectPath.resolve(kotlinErrorPath).listDirectoryEntries().isEmpty() } assertTrue { projectPath.resolve(kotlinErrorPath).listDirectoryEntries().isEmpty() }
assertOutputDoesNotContain("errors were stored into file")
} }
} }
} }
@@ -130,7 +130,7 @@ internal class GradleKotlinCompilerWork @Inject constructor(
metrics.startMeasure(GradleBuildTime.RUN_COMPILATION_IN_WORKER) metrics.startMeasure(GradleBuildTime.RUN_COMPILATION_IN_WORKER)
try { try {
val gradlePrintingMessageCollector = GradlePrintingMessageCollector(log, allWarningsAsErrors) val gradlePrintingMessageCollector = GradlePrintingMessageCollector(log, allWarningsAsErrors)
val gradleMessageCollector = GradleErrorMessageCollector(gradlePrintingMessageCollector, kotlinPluginVersion = kotlinPluginVersion) val gradleMessageCollector = GradleErrorMessageCollector(log, gradlePrintingMessageCollector, kotlinPluginVersion = kotlinPluginVersion)
val (exitCode, executionStrategy) = compileWithDaemonOrFallbackImpl(gradleMessageCollector) val (exitCode, executionStrategy) = compileWithDaemonOrFallbackImpl(gradleMessageCollector)
if (incrementalCompilationEnvironment?.disableMultiModuleIC == true) { if (incrementalCompilationEnvironment?.disableMultiModuleIC == true) {
incrementalCompilationEnvironment.multiModuleICSettings.buildHistoryFile.delete() incrementalCompilationEnvironment.multiModuleICSettings.buildHistoryFile.delete()
@@ -120,7 +120,7 @@ internal fun runToolInSeparateProcess(
compilerClassName, compilerClassName,
"@${compilerOptions.absolutePath}" "@${compilerOptions.absolutePath}"
) )
val messageCollector = GradleErrorMessageCollector(createLoggingMessageCollector(logger)) val messageCollector = GradleErrorMessageCollector(logger, createLoggingMessageCollector(logger))
val process = launchProcessWithFallback(builder, DaemonReportingTargets(messageCollector = messageCollector)) val process = launchProcessWithFallback(builder, DaemonReportingTargets(messageCollector = messageCollector))
// important to read inputStream, otherwise the process may hang on some systems // important to read inputStream, otherwise the process may hang on some systems
@@ -5,6 +5,8 @@
package org.jetbrains.kotlin.gradle.logging package org.jetbrains.kotlin.gradle.logging
import org.gradle.api.logging.Logger
import org.jetbrains.kotlin.buildtools.api.KotlinLogger
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSourceLocation import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSourceLocation
import org.jetbrains.kotlin.cli.common.messages.MessageCollector import org.jetbrains.kotlin.cli.common.messages.MessageCollector
@@ -12,11 +14,19 @@ import java.io.File
import java.io.FileWriter import java.io.FileWriter
class GradleErrorMessageCollector( class GradleErrorMessageCollector(
private val logger: KotlinLogger,
private val delegate: MessageCollector? = null, private val delegate: MessageCollector? = null,
private val acceptableMessageSeverity: List<CompilerMessageSeverity> = listOf(CompilerMessageSeverity.EXCEPTION), private val acceptableMessageSeverity: List<CompilerMessageSeverity> = listOf(CompilerMessageSeverity.EXCEPTION),
private val kotlinPluginVersion: String? = null private val kotlinPluginVersion: String? = null
) : MessageCollector { ) : MessageCollector {
constructor(
logger: Logger,
delegate: MessageCollector? = null,
acceptableMessageSeverity: List<CompilerMessageSeverity> = listOf(CompilerMessageSeverity.EXCEPTION),
kotlinPluginVersion: String? = null,
) : this(GradleKotlinLogger(logger), delegate, acceptableMessageSeverity, kotlinPluginVersion)
private val errors = ArrayList<String>() private val errors = ArrayList<String>()
override fun clear() { override fun clear() {
@@ -47,7 +57,6 @@ class GradleErrorMessageCollector(
return return
} }
file.createNewFile() file.createNewFile()
println("Errors were stored into ${file.absolutePath}")
FileWriter(file).use { FileWriter(file).use {
kotlinPluginVersion?.also { version -> it.append("kotlin version: $version\n") } kotlinPluginVersion?.also { version -> it.append("kotlin version: $version\n") }
for (error in errors) { for (error in errors) {
@@ -55,6 +64,7 @@ class GradleErrorMessageCollector(
} }
it.flush() it.flush()
} }
logger.debug("${errors.count()} errors were stored into file ${file.absolutePath}")
clear() clear()
} }
} }
@@ -318,7 +318,7 @@ abstract class Kotlin2JsCompile @Inject constructor(
val gradlePrintingMessageCollector = GradlePrintingMessageCollector(logger, args.allWarningsAsErrors) val gradlePrintingMessageCollector = GradlePrintingMessageCollector(logger, args.allWarningsAsErrors)
val gradleMessageCollector = val gradleMessageCollector =
GradleErrorMessageCollector(gradlePrintingMessageCollector, kotlinPluginVersion = getKotlinPluginVersion(logger)) GradleErrorMessageCollector(logger, gradlePrintingMessageCollector, kotlinPluginVersion = getKotlinPluginVersion(logger))
val outputItemCollector = OutputItemsCollectorImpl() val outputItemCollector = OutputItemsCollectorImpl()
val compilerRunner = compilerRunner.get() val compilerRunner = compilerRunner.get()
@@ -310,7 +310,7 @@ abstract class KotlinCompile @Inject constructor(
val gradlePrintingMessageCollector = GradlePrintingMessageCollector(logger, args.allWarningsAsErrors) val gradlePrintingMessageCollector = GradlePrintingMessageCollector(logger, args.allWarningsAsErrors)
val gradleMessageCollector = val gradleMessageCollector =
GradleErrorMessageCollector( GradleErrorMessageCollector(
gradlePrintingMessageCollector, kotlinPluginVersion = getKotlinPluginVersion(logger) logger, gradlePrintingMessageCollector, kotlinPluginVersion = getKotlinPluginVersion(logger)
) )
val outputItemCollector = OutputItemsCollectorImpl() val outputItemCollector = OutputItemsCollectorImpl()
val compilerRunner = compilerRunner.get() val compilerRunner = compilerRunner.get()
@@ -29,6 +29,7 @@ import org.jetbrains.kotlin.compilerRunner.OutputItemsCollectorImpl
import org.jetbrains.kotlin.gradle.dsl.* import org.jetbrains.kotlin.gradle.dsl.*
import org.jetbrains.kotlin.gradle.internal.tasks.allOutputFiles import org.jetbrains.kotlin.gradle.internal.tasks.allOutputFiles
import org.jetbrains.kotlin.gradle.logging.GradleErrorMessageCollector import org.jetbrains.kotlin.gradle.logging.GradleErrorMessageCollector
import org.jetbrains.kotlin.gradle.logging.GradleKotlinLogger
import org.jetbrains.kotlin.gradle.logging.GradlePrintingMessageCollector import org.jetbrains.kotlin.gradle.logging.GradlePrintingMessageCollector
import org.jetbrains.kotlin.gradle.plugin.KotlinCompilerArgumentsProducer.CreateCompilerArgumentsContext import org.jetbrains.kotlin.gradle.plugin.KotlinCompilerArgumentsProducer.CreateCompilerArgumentsContext
import org.jetbrains.kotlin.gradle.plugin.KotlinCompilerArgumentsProducer.CreateCompilerArgumentsContext.Companion.create import org.jetbrains.kotlin.gradle.plugin.KotlinCompilerArgumentsProducer.CreateCompilerArgumentsContext.Companion.create
@@ -135,7 +136,7 @@ abstract class KotlinCompileCommon @Inject constructor(
taskOutputsBackup: TaskOutputsBackup? taskOutputsBackup: TaskOutputsBackup?
) { ) {
val gradlePrintingMessageCollector = GradlePrintingMessageCollector(logger, args.allWarningsAsErrors) val gradlePrintingMessageCollector = GradlePrintingMessageCollector(logger, args.allWarningsAsErrors)
val gradleMessageCollector = GradleErrorMessageCollector(gradlePrintingMessageCollector) val gradleMessageCollector = GradleErrorMessageCollector(logger, gradlePrintingMessageCollector)
val outputItemCollector = OutputItemsCollectorImpl() val outputItemCollector = OutputItemsCollectorImpl()
val compilerRunner = compilerRunner.get() val compilerRunner = compilerRunner.get()
val environment = GradleCompilerEnvironment( val environment = GradleCompilerEnvironment(