void () { ModifiableWorkspace workspace = getModifiableWorkspace(); if (workspace == null) return; final List<String> oldModules = ContainerUtil.map(ModuleManager.getInstance(myProject).getModules(), module -> module.getName()); final List<String> newModules = ContainerUtil.map(myModifiableModuleModel.getModules(), module -> module.getName()); final Collection<String> removedModules = new HashSet<>(oldModules); removedModules.removeAll(newModules); Map<String, String> toSubstitute = new HashMap<>(); ProjectDataManager projectDataManager = ProjectDataManager.getInstance(); for (ExternalSystemManager<?, ?, ?, ?, ?> manager: ExternalSystemManager.EP_NAME.getIterable()) { Collection<ExternalProjectInfo> projectsData = projectDataManager.getExternalProjectsData(myProject, manager.getSystemId()); for (ExternalProjectInfo projectInfo: projectsData) { if (projectInfo.getExternalProjectStructure() == null) { continue; } Collection<DataNode<LibraryData>> libraryNodes = ExternalSystemApiUtil.findAll(projectInfo.getExternalProjectStructure(), ProjectKeys.LIBRARY); for (DataNode<LibraryData> libraryNode: libraryNodes) { String substitutionModuleCandidate = findModuleByPublication(libraryNode.getData()); if (substitutionModuleCandidate != null) { toSubstitute.put(libraryNode.getData().getInternalName(), substitutionModuleCandidate); } } } } for (Module module: getModules()) { ModifiableRootModel modifiableRootModel = getModifiableRootModel(module); boolean changed = false; OrderEntry[] entries = modifiableRootModel.getOrderEntries(); for (int i = 0, length = entries.length; i < length; i++) { OrderEntry orderEntry = entries[i]; if (orderEntry instanceof ModuleOrderEntry) { String workspaceModule = ((ModuleOrderEntry)orderEntry).getModuleName(); if (removedModules.contains(workspaceModule)) { DependencyScope scope = ((ModuleOrderEntry)orderEntry).getScope(); if (workspace.isSubstitution(module.getName(), workspaceModule, scope)) { String libraryName = workspace.getSubstitutedLibrary(workspaceModule); if (libraryName != null) { Library library = getLibraryByName(libraryName); if (library != null) { modifiableRootModel.removeOrderEntry(orderEntry); entries[i] = modifiableRootModel.addLibraryEntry(library); changed = true; workspace.removeSubstitution(module.getName(), workspaceModule, libraryName, scope); } } } } } if (!(orderEntry instanceof LibraryOrderEntry libraryOrderEntry)) continue; if (!libraryOrderEntry.isModuleLevel() && libraryOrderEntry.getLibraryName() != null) { String workspaceModule = toSubstitute.get(libraryOrderEntry.getLibraryName()); if (workspaceModule != null) { Module ideModule = findIdeModule(workspaceModule); if (ideModule != null) { ModuleOrderEntry moduleOrderEntry = modifiableRootModel.addModuleOrderEntry(ideModule); moduleOrderEntry.setScope(libraryOrderEntry.getScope()); modifiableRootModel.removeOrderEntry(orderEntry); entries[i] = moduleOrderEntry; changed = true; workspace.addSubstitution(module.getName(), workspaceModule, libraryOrderEntry.getLibraryName(), libraryOrderEntry.getScope()); } } } } if (changed) { modifiableRootModel.rearrangeOrderEntries(entries); } } workspace.commit(); }