Compiling Kotlin files using CompilerManager fails
I am writing a plugin for IntelliJ IDEA and I need to compile a test file in a user project.
Among the CompilerManager and ProjectTaskManager compilation tools, I chose CompilerManager, as I need to get all the compilation error messages.
I am trying to trigger the compilation as follows:
CompilerManager.getInstance(project).compile(arrayOf(virtualFile), callback)
While there are no problems compiling Java, when compiling Kotlin, there are Unresolved reference problems that should not occur.
For example, for a test class of the following form:
package org.springframework.samples.petclinic.owner
class OwnerTest {
val owner = Owner()
}
I get the error:
Kotlin: Unresolved reference: Owner
This error should not occur, since Owner is located in the package org.springframework.samples.petclinic.owner
Example project: spring-petclinic-kotlin
In addition to passing arrayOf(virtualFile) as a parameter to CompilerManager, I have tried passing different implementations of CompileScope, such as FileIndexCompileScope, OneProjectItemCompileScope, FileSetCompileScope, ModuleCompileScope.
I was able to achieve successful compilation only when using ModuleCompileScope, however, I would like to compile only one virtualFile, not an entire module.
Could you please suggest what solutions for this problem exist?
Please sign in to leave a comment.
First of all, there is an important difference between CompilerManager and ProjectTaskManager APIs.
The former is an interface to the built-in compiler and build system (aka JPS), while the latter is an abstraction over any build system, including Gradle in delegated mode (which is enabled by default). So unless you are absolutely sure that you need to use the built-in compiler even against the current IDEA configuration, you should use the `ProjectTaskManager` API.
To get access to all compilation error messages, you can register your BuildProgessListener with BuildViewManager service, e.g.: