How to mark Modules when opening a project

Answered

The project I need to work on has such a directory structure, where `moduleA` and `moduleB` are directories that need to be marked as module.

root
- projects/
  - moduleA
    - package.toml
  - moduleB
    - package.toml
- workspace.toml

I hope that when I open this directory for the first time (no `.idea/` or `*.iml` is generated), I hope to detect whether it belongs to my framework, and if so, mark modules.

I'm going to use

ModuleManager.getInstance(project).newModule(path, myModuleType)

to mark module, but I don't know which endpoint should be used to listen for the event of opening the project for the first time.

0
4 comments

Write actions are not a problem, try WriteCommandAction.runWriteCommandAction(project) { }

1

Hi,

Try to wrap it into `com.intellij.openapi.application.CoroutinesKt#writeAction`.

Write command action suggested by Marcin is for running actions that are undoable, which I believe you don't want here.

1

You might use postStartupActivity extension and then maybe use project.isNewProject(). But I didn't test that.

0

It seems that newModule cannot be called in ProjectActivity, because Write access is allowed inside write-action only.

 

Impliments:

class MyProjectActivityListener : ProjectActivity {
    override suspend fun execute(project: Project) {
        ModuleManager.getInstance(project).newModule("test.iml", "WEB_MODULE")
    }
}

Error Message:

2024-04-25 17:35:50,214 [   1631]   WARN - #c.i.i.s.p.i.BundledSharedIndexProvider - Bundled shared index is not found at: C:\Users\Dell\.gradle\caches\modules-2\files-2.1\com.jetbrains.intellij.idea\ideaIC\LATEST-EAP-SNAPSHOT\7c27d09201acdfbd78592ba4376bd3ce2c7a4d9e\ideaIC-LATEST-EAP-SNAPSHOT\jdk-shared-indexes
2024-04-25 17:35:55,011 [   6428] SEVERE - #c.i.o.a.i.CoroutineExceptionHandlerImpl - Unhandled exception in [ComponentManager(ProjectImpl@593485938), CoroutineName(run activity), run activity, CoroutineId(3982), "run activity#3982":StandaloneCoroutine{Cancelling}@1778f25, Dispatchers.Default]
com.intellij.openapi.diagnostic.RuntimeExceptionWithAttachments: Write access is allowed inside write-action only (see Application.runWriteAction()); see https://jb.gg/ij-platform-threading for details
Current thread: Thread[DefaultDispatcher-worker-5 @run activity#3982,5,main] 1285337309 (EventQueue.isDispatchThread()=false)
SystemEventQueueThread: Thread[AWT-EventQueue-0,6,main] 1188971994
    at com.intellij.util.concurrency.ThreadingAssertions.createThreadAccessException(ThreadingAssertions.java:180)
    at com.intellij.util.concurrency.ThreadingAssertions.throwThreadAccessException(ThreadingAssertions.java:174)
    at com.intellij.util.concurrency.ThreadingAssertions.assertWriteAccess(ThreadingAssertions.java:169)
    at com.intellij.openapi.application.impl.ApplicationImpl.assertWriteAccessAllowed(ApplicationImpl.java:1003)
    at com.intellij.workspaceModel.ide.impl.legacyBridge.module.ModifiableModuleModelBridgeImpl.prepareForCommit(ModifiableModuleModelBridgeImpl.kt:242)
    at com.intellij.workspaceModel.ide.impl.legacyBridge.module.ModifiableModuleModelBridgeImpl.collectChanges(ModifiableModuleModelBridgeImpl.kt:247)
    at com.intellij.workspaceModel.ide.impl.legacyBridge.module.ModifiableModuleModelBridgeImpl.commit(ModifiableModuleModelBridgeImpl.kt:234)
    at com.intellij.workspaceModel.ide.impl.legacyBridge.module.ModuleManagerBridgeImpl.newModule(ModuleManagerBridgeImpl.kt:274)
    at valkyrie.project.modules.MyProjectActivityListener.execute(MyProjectActivityListener.kt:10)
    at com.intellij.ide.startup.impl.StartupManagerImplKt$launchActivity$1.invokeSuspend(StartupManagerImpl.kt:473)
    at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
    at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:108)
    at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:584)
    at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:793)
    at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:697)
    at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:684)
2024-04-25 17:35:55,079 [   6496] SEVERE - #c.i.o.a.i.CoroutineExceptionHandlerImpl - IntelliJ IDEA 2024.1.1 RC  Build #IC-241.15989.69
2024-04-25 17:35:55,080 [   6497] SEVERE - #c.i.o.a.i.CoroutineExceptionHandlerImpl - JDK: 17.0.10; VM: OpenJDK 64-Bit Server VM; Vendor: JetBrains s.r.o.
2024-04-25 17:35:55,080 [   6497] SEVERE - #c.i.o.a.i.CoroutineExceptionHandlerImpl - OS: Windows 11
2024-04-25 17:35:55,080 [   6497] SEVERE - #c.i.o.a.i.CoroutineExceptionHandlerImpl - Plugin to blame: Valkyrie Language version: 0.1.0
2024-04-25 17:35:55,081 [   6498] SEVERE - #c.i.o.a.i.CoroutineExceptionHandlerImpl - Last Action: 
Exception in thread "DefaultDispatcher-worker-5 @run activity#3982" com.intellij.openapi.diagnostic.RuntimeExceptionWithAttachments: Write access is allowed inside write-action only (see Application.runWriteAction()); see https://jb.gg/ij-platform-threading for details
Current thread: Thread[DefaultDispatcher-worker-5 @run activity#3982,5,main] 1285337309 (EventQueue.isDispatchThread()=false)
SystemEventQueueThread: Thread[AWT-EventQueue-0,6,main] 1188971994
    at com.intellij.util.concurrency.ThreadingAssertions.createThreadAccessException(ThreadingAssertions.java:180)
    at com.intellij.util.concurrency.ThreadingAssertions.throwThreadAccessException(ThreadingAssertions.java:174)
    at com.intellij.util.concurrency.ThreadingAssertions.assertWriteAccess(ThreadingAssertions.java:169)
    at com.intellij.openapi.application.impl.ApplicationImpl.assertWriteAccessAllowed(ApplicationImpl.java:1003)
    at com.intellij.workspaceModel.ide.impl.legacyBridge.module.ModifiableModuleModelBridgeImpl.prepareForCommit(ModifiableModuleModelBridgeImpl.kt:242)
    at com.intellij.workspaceModel.ide.impl.legacyBridge.module.ModifiableModuleModelBridgeImpl.collectChanges(ModifiableModuleModelBridgeImpl.kt:247)
    at com.intellij.workspaceModel.ide.impl.legacyBridge.module.ModifiableModuleModelBridgeImpl.commit(ModifiableModuleModelBridgeImpl.kt:234)
    at com.intellij.workspaceModel.ide.impl.legacyBridge.module.ModuleManagerBridgeImpl.newModule(ModuleManagerBridgeImpl.kt:274)
    at valkyrie.project.modules.MyProjectActivityListener.execute(MyProjectActivityListener.kt:10)
    at com.intellij.ide.startup.impl.StartupManagerImplKt$launchActivity$1.invokeSuspend(StartupManagerImpl.kt:473)
    at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
    at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:108)
    at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:584)
    at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:793)
    at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:697)
    at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:684)
    Suppressed: kotlinx.coroutines.internal.DiagnosticCoroutineContextException: [ComponentManager(ProjectImpl@593485938), CoroutineName(run activity), run activity, CoroutineId(3982), "run activity#3982":StandaloneCoroutine{Cancelling}@1778f25, Dispatchers.Default]
2024-04-25 17:36:48,869 [  60286]   WARN - #c.i.u.x.Binding - No accessors for com.intellij.platform.feedback.impl.state.DontShowAgainFeedbackState. This means that state class cannot be serialized properly. Please see https://jb.gg/ij-psoc
2024-04-25 17:36:48,869 [  60286]   WARN - #c.i.u.x.Binding - No accessors for com.intellij.platform.feedback.impl.state.CommonFeedbackSurveysState. This means that state class cannot be serialized properly. Please see https://jb.gg/ij-psoc
2024-04-25 17:37:26,692 [  98109]   WARN - #c.i.u.x.Binding - No accessors for org.jetbrains.kotlin.cli.common.arguments.InternalArgument. This means that state class cannot be serialized properly. Please see https://jb.gg/ij-psoc

 

0

Please sign in to leave a comment.