private void workspaceModelCommit() {
    ProjectRootManagerEx.getInstanceEx(myProject).mergeRootsChangesDuring(() -> {
      if (ExternalProjectsWorkspaceImpl.isDependencySubstitutionEnabled()) {
        updateSubstitutions();
      }
      LibraryTable.ModifiableModel projectLibrariesModel = getModifiableProjectLibrariesModel();
      for (Map.Entry<Library, Library.ModifiableModel> entry: myModifiableLibraryModels.entrySet()) {
        Library fromLibrary = entry.getKey();
        String libraryName = fromLibrary.getName();
        Library.ModifiableModel modifiableModel = entry.getValue();

        // Modifiable model for the new library which was disposed via ModifiableModel.removeLibrary should also be disposed
        // Modifiable model for the old library which was removed from ProjectLibraryTable should also be disposed
        if ((fromLibrary instanceof LibraryEx && ((LibraryEx)fromLibrary).isDisposed())
            || (fromLibrary.getTable() != null && libraryName != null && projectLibrariesModel.getLibraryByName(libraryName) == null)
            || (getModifiableWorkspace() != null && getModifiableWorkspace().isSubstituted(fromLibrary.getName()))) {
          Disposer.dispose(modifiableModel);
        }
        else {
          ((LibraryModifiableModelBridge)modifiableModel).prepareForCommit();
        }
      }
      ((ProjectModifiableLibraryTableBridge)projectLibrariesModel).prepareForCommit();

      ModifiableRootModel[] rootModels;
      if (myModifiableModuleModel != null) {
        Module[] modules = myModifiableModuleModel.getModules();
        for (Module module : modules) {
          module.putUserData(MODIFIABLE_MODELS_PROVIDER_KEY, null);
        }
        Set<Module> existingModules = Set.of(modules);
        rootModels = myModifiableRootModels.entrySet().stream().filter(entry -> existingModules.contains(entry.getKey())).map(Map.Entry::getValue).toArray(ModifiableRootModel[]::new);
        ((ModifiableModuleModelBridge)myModifiableModuleModel).prepareForCommit();
      }
      else {
        rootModels = myModifiableRootModels.values().toArray(new ModifiableRootModel[0]);
      }

      for (ModifiableRootModel model : rootModels) {
        assert !model.isDisposed() : "Already disposed: " + model;
      }

      for (ModifiableRootModel model : rootModels) {
        ((ModifiableRootModelBridge)model).prepareForCommit();
      }

      for (Map.Entry<Module, String> entry: myProductionModulesForTestModules.entrySet()) {
        TestModuleProperties testModuleProperties = TestModuleProperties.getInstance(entry.getKey());
        if (testModuleProperties instanceof TestModulePropertiesBridge bridge) {
          bridge.setProductionModuleNameToBuilder(entry.getValue(),
                                                  myModifiableModuleModel.getActualName(entry.getKey()),
                                                  getActualStorageBuilder());
        } else {
          testModuleProperties.setProductionModuleName(entry.getValue());
        }
      }

      for (Map.Entry<Module, ModifiableFacetModel> each: myModifiableFacetModels.entrySet()) {
        if (!each.getKey().isDisposed()) {
          ((ModifiableFacetModelBridge)each.getValue()).prepareForCommit();
        }
      }
      myModifiableModels.values().forEach(ModifiableModel::commit);
      WorkspaceModel.getInstance(myProject).updateProjectModel("External system: commit model", builder -> {
        MutableEntityStorage storageBuilder = getActualStorageBuilder();
        if (LOG.isTraceEnabled()) {
          LOG.trace("Apply builder in ModifiableModels commit. builder: " + storageBuilder);
        }
        builder.applyChangesFrom(storageBuilder);
        return null;
      });

      for (ModifiableRootModel model : rootModels) {
        ((ModifiableRootModelBridge)model).postCommit();
      }
    });
    myUserData.clear();
  }
private void workspaceModelCommit()