diff --git a/detekt-core/src/main/kotlin/io/gitlab/arturbosch/detekt/core/ProcessingSettings.kt b/detekt-core/src/main/kotlin/io/gitlab/arturbosch/detekt/core/ProcessingSettings.kt index c1369d6d173..fbe727b7667 100644 --- a/detekt-core/src/main/kotlin/io/gitlab/arturbosch/detekt/core/ProcessingSettings.kt +++ b/detekt-core/src/main/kotlin/io/gitlab/arturbosch/detekt/core/ProcessingSettings.kt @@ -32,7 +32,7 @@ class ProcessingSettings( Closeable, LoggingAware by LoggingFacade(spec.loggingSpec), PropertiesAware by PropertiesFacade(), - EnvironmentAware by EnvironmentFacade(spec.projectSpec, spec.compilerSpec), + EnvironmentAware by EnvironmentFacade(spec.projectSpec, spec.compilerSpec, spec.loggingSpec), ClassloaderAware by ExtensionFacade(spec.extensionsSpec.plugins), SetupContext { diff --git a/detekt-core/src/main/kotlin/io/gitlab/arturbosch/detekt/core/settings/EnvironmentAware.kt b/detekt-core/src/main/kotlin/io/gitlab/arturbosch/detekt/core/settings/EnvironmentAware.kt index 7f671a9741f..f741aceed93 100644 --- a/detekt-core/src/main/kotlin/io/gitlab/arturbosch/detekt/core/settings/EnvironmentAware.kt +++ b/detekt-core/src/main/kotlin/io/gitlab/arturbosch/detekt/core/settings/EnvironmentAware.kt @@ -3,6 +3,7 @@ package io.gitlab.arturbosch.detekt.core.settings import io.github.detekt.parser.createCompilerConfiguration import io.github.detekt.parser.createKotlinCoreEnvironment import io.github.detekt.tooling.api.spec.CompilerSpec +import io.github.detekt.tooling.api.spec.LoggingSpec import io.github.detekt.tooling.api.spec.ProjectSpec import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment import org.jetbrains.kotlin.com.intellij.openapi.Disposable @@ -11,6 +12,8 @@ import org.jetbrains.kotlin.config.JvmTarget import org.jetbrains.kotlin.config.LanguageVersion import java.io.Closeable import java.io.File +import java.io.OutputStream +import java.io.PrintStream interface EnvironmentAware { @@ -21,7 +24,8 @@ interface EnvironmentAware { internal class EnvironmentFacade( private val projectSpec: ProjectSpec, - private val compilerSpec: CompilerSpec + private val compilerSpec: CompilerSpec, + private val loggingSpec: LoggingSpec, ) : AutoCloseable, Closeable, EnvironmentAware { override val disposable: Disposable = Disposer.newDisposable() @@ -36,7 +40,11 @@ internal class EnvironmentFacade( compilerSpec.parseJvmTarget(), compilerSpec.jdkHome, ) - createKotlinCoreEnvironment(compilerConfiguration, disposable) + createKotlinCoreEnvironment( + compilerConfiguration, + disposable, + if (loggingSpec.debug) loggingSpec.errorChannel.asPrintStream() else NullPrintStream, + ) } override fun close() { @@ -61,3 +69,26 @@ internal fun CompilerSpec.parseJvmTarget(): JvmTarget { " must be one of ${JvmTarget.values().map(JvmTarget::description)}" } } + +private object NullPrintStream : PrintStream( + object : OutputStream() { + override fun write(b: Int) { + // no-op + } + } +) + +private fun Appendable.asPrintStream(): PrintStream { + val appendable = this + return if (appendable is PrintStream) { + appendable + } else { + PrintStream( + object : OutputStream() { + override fun write(b: Int) { + appendable.append(b.toChar()) + } + } + ) + } +} diff --git a/detekt-parser/src/main/kotlin/io/github/detekt/parser/KotlinEnvironmentUtils.kt b/detekt-parser/src/main/kotlin/io/github/detekt/parser/KotlinEnvironmentUtils.kt index 19370df9ae6..51a320de7c0 100644 --- a/detekt-parser/src/main/kotlin/io/github/detekt/parser/KotlinEnvironmentUtils.kt +++ b/detekt-parser/src/main/kotlin/io/github/detekt/parser/KotlinEnvironmentUtils.kt @@ -23,6 +23,7 @@ import org.jetbrains.kotlin.config.LanguageVersion import org.jetbrains.kotlin.config.LanguageVersionSettings import org.jetbrains.kotlin.config.LanguageVersionSettingsImpl import java.io.File +import java.io.PrintStream import java.net.URLClassLoader import java.nio.file.Path @@ -30,15 +31,31 @@ import java.nio.file.Path * Creates an environment instance which can be used to compile source code to KtFile's. * This environment also allows to modify the resulting AST files. */ +@Deprecated( + "You should pass a printStream", + ReplaceWith("createKotlinCoreEnvironment(configuration, disposable, System.err)") +) fun createKotlinCoreEnvironment( configuration: CompilerConfiguration = CompilerConfiguration(), disposable: Disposable = Disposer.newDisposable() +): KotlinCoreEnvironment { + return createKotlinCoreEnvironment(configuration, disposable, System.err) +} + +/** + * Creates an environment instance which can be used to compile source code to KtFile's. + * This environment also allows to modify the resulting AST files. + */ +fun createKotlinCoreEnvironment( + configuration: CompilerConfiguration = CompilerConfiguration(), + disposable: Disposable = Disposer.newDisposable(), + printStream: PrintStream, ): KotlinCoreEnvironment { // https://github.com/JetBrains/kotlin/commit/2568804eaa2c8f6b10b735777218c81af62919c1 setIdeaIoUseFallback() configuration.put( CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY, - PrintingMessageCollector(System.err, MessageRenderer.PLAIN_FULL_PATHS, false) + PrintingMessageCollector(printStream, MessageRenderer.PLAIN_FULL_PATHS, false) ) configuration.put(CommonConfigurationKeys.MODULE_NAME, "detekt") diff --git a/detekt-parser/src/main/kotlin/io/github/detekt/parser/KtCompiler.kt b/detekt-parser/src/main/kotlin/io/github/detekt/parser/KtCompiler.kt index 4115c53f179..b4b5463a0f3 100644 --- a/detekt-parser/src/main/kotlin/io/github/detekt/parser/KtCompiler.kt +++ b/detekt-parser/src/main/kotlin/io/github/detekt/parser/KtCompiler.kt @@ -11,7 +11,7 @@ import java.nio.file.Files import java.nio.file.Path open class KtCompiler( - protected val environment: KotlinCoreEnvironment = createKotlinCoreEnvironment() + protected val environment: KotlinCoreEnvironment = createKotlinCoreEnvironment(printStream = System.err) ) { protected val psiFileFactory = KtPsiFactory(environment.project, markGenerated = false)