Need help with understanding how to import Maven project on IntelliJ project open

I am experimenting with plugin DevKit.

What I want to do:

  • On IntelliJ project open check if folder contains configuration file with specific filename; if it does then read that file and import referenced Maven projects as external model modules.

What I did:

import com.intellij.ide.actions.ImportModuleAction

internal class ProjectOpenStartupActivity : ProjectActivity, DumbAware {

    override suspend fun execute(project: Project) {

        val configuredPaths: List<String> = readWellKnownConfig()

        val mavenProvider = ImportModuleAction.getProviders(null)
            .firstOrNull { it.name == "Maven" }

        writeAction {
            for (mavenProjectPath in configuredPaths) { // /path/to/package-one/pom.xml

                val pomFile = LocalFileSystem.getInstance()
                    .refreshAndFindFileByPath(mavenProjectPath) // file:///path/to/package-one/pom.xml

                if (!mavenProvider.canImport(pomFile, project)) {
                    continue
                }

                val wizard = ImportModuleAction.createImportWizard(project, null, pomFile, mavenProvider)!!

                ImportModuleAction.createFromWizard(project, wizard)
            }
        }
    }
}

What I observe:

  • never ending progress bar, looks like importer got the intention but is blocked/waiting forever.

  • No exceptions are reported

  • if cancelled, the same project could be imported in a few seconds from UI (File → New → Module from existing sources → import module from external model

So question is: what could be amiss? And where should I look for any leads on what went wrong? Looks like waiting on (write?) lock, but if so then what is a proper way to invoke ImportModuleAction?

Any help is much appreciated.

General environment details:

platformType = IC
platformVersion = 2024.2.5
platformBundledPlugins = org.jetbrains.idea.maven, com.intellij.java, org.jetbrains.idea.maven.model, org.jetbrains.idea.maven.server.api

I did a bit of brute-forcing different options and it started to work as expected (whether it is the right way of doing things or not… who knows):

@@ -9,7 +9,7 @@
         val mavenProvider = ImportModuleAction.getProviders(null)
             .firstOrNull { it.name == "Maven" }
 
-        writeAction {
+        withContext(Dispatchers.Main) {
             for (mavenProjectPath in configuredPaths) { // /path/to/package-one/pom.xml
 
                 val pomFile = LocalFileSystem.getInstance()

Presumably Dispatchers.EDT should be available (doc) but for some reason it is not in IdeaIC.2024.2.5 so Main covers for it.