[KLIB Resolver] Report KLIB resolver issues as compiler messages
The reason of this change is to make messages (especially warnings) that are reported by the KLIB resolver become visible to the end user. This can be achieved to forwarding such messages to the appropriate compiler's components such as `org.jetbrains.kotlin.cli.common.messages.MessageCollector` and `org.jetbrains.kotlin.ir.util.IrMessageLogger`. Also: The default `DummyLogger` should be used as minimal as possible. Because it just forwards messages to the standard output (console) where they can remain unattended. When the compiler is executed from the Gradle plugin such messages appear only in DEBUG Gradle's log. ^KT-63573
This commit is contained in:
committed by
Space Team
parent
8430be39c9
commit
46081f968d
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.cli.common.messages
|
||||
|
||||
import org.jetbrains.kotlin.analyzer.CompilationErrorException
|
||||
import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys
|
||||
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity.*
|
||||
import org.jetbrains.kotlin.config.CompilerConfiguration
|
||||
import org.jetbrains.kotlin.util.Logger
|
||||
|
||||
/**
|
||||
* An adapter for the [Logger] interface that reports all messages to compiler's [MessageCollector].
|
||||
*
|
||||
* @property messageCollector - The destination for the messages.
|
||||
* @property treatWarningsAsErrors - Whether to treat warnings as errors. Can be used in tests.
|
||||
*/
|
||||
private class CompilerLoggerAdapter(
|
||||
private val messageCollector: MessageCollector,
|
||||
private val treatWarningsAsErrors: Boolean
|
||||
) : Logger {
|
||||
override fun log(message: String) = messageCollector.report(LOGGING, message, null)
|
||||
override fun warning(message: String) = messageCollector.report(if (treatWarningsAsErrors) ERROR else STRONG_WARNING, message, null)
|
||||
override fun error(message: String) = messageCollector.report(ERROR, message, null)
|
||||
|
||||
override fun fatal(message: String): Nothing {
|
||||
error(message)
|
||||
(messageCollector as? GroupingMessageCollector)?.flush()
|
||||
throw CompilationErrorException()
|
||||
}
|
||||
}
|
||||
|
||||
fun MessageCollector.toLogger(treatWarningsAsErrors: Boolean = false): Logger =
|
||||
CompilerLoggerAdapter(this, treatWarningsAsErrors)
|
||||
|
||||
fun CompilerConfiguration.getLogger(treatWarningsAsErrors: Boolean = false): Logger =
|
||||
getNotNull(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY).toLogger(treatWarningsAsErrors)
|
||||
@@ -30,7 +30,7 @@ import org.jetbrains.kotlin.psi.KtFile
|
||||
import org.jetbrains.kotlin.text
|
||||
import org.jetbrains.kotlin.util.Logger
|
||||
import java.io.File
|
||||
import kotlin.system.exitProcess
|
||||
import org.jetbrains.kotlin.cli.common.messages.toLogger as toLoggerNew
|
||||
|
||||
fun incrementalCompilationIsEnabled(arguments: CommonCompilerArguments): Boolean {
|
||||
return arguments.incrementalCompilation ?: IncrementalCompilation.isEnabledForJvm()
|
||||
@@ -127,23 +127,9 @@ fun <PathProvider : Any> getLibraryFromHome(
|
||||
return null
|
||||
}
|
||||
|
||||
fun MessageCollector.toLogger(): Logger =
|
||||
object : Logger {
|
||||
override fun error(message: String) {
|
||||
report(CompilerMessageSeverity.ERROR, message)
|
||||
}
|
||||
|
||||
override fun fatal(message: String): Nothing {
|
||||
report(CompilerMessageSeverity.ERROR, message)
|
||||
exitProcess(1)
|
||||
}
|
||||
|
||||
override fun warning(message: String) {
|
||||
report(CompilerMessageSeverity.WARNING, message)
|
||||
}
|
||||
|
||||
override fun log(message: String) {
|
||||
report(CompilerMessageSeverity.LOGGING, message)
|
||||
}
|
||||
}
|
||||
|
||||
@Deprecated(
|
||||
"Use org.jetbrains.kotlin.cli.common.messages.toLogger() instead",
|
||||
ReplaceWith("toLogger()", "org.jetbrains.kotlin.cli.common.messages.toLogger"),
|
||||
DeprecationLevel.ERROR
|
||||
)
|
||||
fun MessageCollector.toLogger(): Logger = toLoggerNew()
|
||||
|
||||
+1
-1
@@ -23,7 +23,7 @@ import org.jetbrains.kotlin.cli.common.checkKotlinPackageUsageForPsi
|
||||
import org.jetbrains.kotlin.cli.common.config.addKotlinSourceRoot
|
||||
import org.jetbrains.kotlin.cli.common.fir.FirDiagnosticsCompilerResultsReporter
|
||||
import org.jetbrains.kotlin.cli.common.messages.AnalyzerWithCompilerReport
|
||||
import org.jetbrains.kotlin.cli.common.toLogger
|
||||
import org.jetbrains.kotlin.cli.common.messages.toLogger
|
||||
import org.jetbrains.kotlin.cli.jvm.compiler.FirKotlinToJvmBytecodeCompiler.runFrontend
|
||||
import org.jetbrains.kotlin.cli.jvm.compiler.pipeline.convertToIrAndActualizeForJvm
|
||||
import org.jetbrains.kotlin.cli.jvm.config.*
|
||||
|
||||
@@ -11,6 +11,7 @@ import org.jetbrains.kotlin.analyzer.common.CommonPlatformAnalyzerServices
|
||||
import org.jetbrains.kotlin.backend.common.CommonKLibResolver
|
||||
import org.jetbrains.kotlin.cli.common.*
|
||||
import org.jetbrains.kotlin.cli.common.fir.FirDiagnosticsCompilerResultsReporter
|
||||
import org.jetbrains.kotlin.cli.common.messages.toLogger
|
||||
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
|
||||
import org.jetbrains.kotlin.cli.jvm.compiler.VfsBasedProjectEnvironment
|
||||
import org.jetbrains.kotlin.cli.jvm.compiler.createContextForIncrementalCompilation
|
||||
@@ -43,7 +44,6 @@ import org.jetbrains.kotlin.library.metadata.KlibMetadataProtoBuf
|
||||
import org.jetbrains.kotlin.modules.TargetId
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.platform.CommonPlatforms
|
||||
import org.jetbrains.kotlin.util.DummyLogger
|
||||
import java.io.File
|
||||
|
||||
internal class FirMetadataSerializer(
|
||||
@@ -78,7 +78,7 @@ internal class FirMetadataSerializer(
|
||||
.filterIsInstance<JvmClasspathRoot>()
|
||||
.filter { it.file.isDirectory || it.file.extension == "klib" }
|
||||
.map { it.file.absolutePath }
|
||||
val resolvedLibraries = CommonKLibResolver.resolve(klibFiles, DummyLogger).getFullResolvedList()
|
||||
val resolvedLibraries = CommonKLibResolver.resolve(klibFiles, messageCollector.toLogger()).getFullResolvedList()
|
||||
|
||||
val outputs = if (isLightTree) {
|
||||
val projectEnvironment = environment.toAbstractProjectEnvironment() as VfsBasedProjectEnvironment
|
||||
|
||||
@@ -11,6 +11,7 @@ import org.jetbrains.kotlin.analyzer.common.CommonPlatformAnalyzerServices
|
||||
import org.jetbrains.kotlin.backend.common.serialization.metadata.KlibMetadataMonolithicSerializer
|
||||
import org.jetbrains.kotlin.builtins.DefaultBuiltIns
|
||||
import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys
|
||||
import org.jetbrains.kotlin.cli.common.messages.getLogger
|
||||
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
|
||||
import org.jetbrains.kotlin.cli.jvm.config.JvmClasspathRoot
|
||||
import org.jetbrains.kotlin.cli.jvm.config.JvmContentRoot
|
||||
@@ -34,6 +35,7 @@ import org.jetbrains.kotlin.storage.LockBasedStorageManager
|
||||
import org.jetbrains.kotlin.storage.StorageManager
|
||||
import org.jetbrains.kotlin.utils.keysToMap
|
||||
import java.io.File
|
||||
import org.jetbrains.kotlin.konan.file.File as KFile
|
||||
|
||||
internal class K2MetadataKlibSerializer(
|
||||
configuration: CompilerConfiguration,
|
||||
@@ -78,8 +80,8 @@ private class KlibMetadataDependencyContainer(
|
||||
val klibFiles = classpathFiles
|
||||
.filter { it.extension == "klib" || it.isDirectory }
|
||||
|
||||
// TODO: need to move K2Metadata to SearchPathResolver.
|
||||
klibFiles.map { resolveSingleFileKlib(org.jetbrains.kotlin.konan.file.File(it.absolutePath)) }
|
||||
val logger = configuration.getLogger()
|
||||
klibFiles.map { resolveSingleFileKlib(KFile(it.absolutePath), logger) }
|
||||
}
|
||||
|
||||
private val friendPaths = configuration.get(K2MetadataConfigurationKeys.FRIEND_PATHS).orEmpty().toSet()
|
||||
|
||||
Reference in New Issue
Block a user