Module (@NotNull Project project, @NotNull ModuleType type, @NotNull String name, @NotNull VirtualFile root) { return WriteCommandAction.writeCommandAction(project).compute(() -> { String moduleName; ModifiableModuleModel moduleModel = ModuleManager.getInstance(project).getModifiableModel(); try { moduleName = moduleModel.newModule(root.toNioPath().resolve(name + ".iml"), type.getId()).getName(); moduleModel.commit(); } catch (Throwable t) { moduleModel.dispose(); throw t; } Module dep = ModuleManager.getInstance(project).findModuleByName(moduleName); assert dep != null : moduleName; ModifiableRootModel model = ModuleRootManager.getInstance(dep).getModifiableModel(); try { model.addContentEntry(root).addSourceFolder(root, false); model.commit(); } catch (Throwable t) { model.dispose(); throw t; } return dep; }); }