void (final VirtualFile newSourceRoot) { ApplicationManager.getApplication().runWriteAction( () -> { final ModifiableRootModel rootModel = ModuleRootManager.getInstance(myModule).getModifiableModel(); final ContentEntry[] content = rootModel.getContentEntries(); boolean contentToChangeFound = false; for (ContentEntry contentEntry : content) { final SourceFolder[] sourceFolders = contentEntry.getSourceFolders(); for (SourceFolder sourceFolder : sourceFolders) { contentEntry.removeSourceFolder(sourceFolder); } final VirtualFile contentRoot = contentEntry.getFile(); if (contentRoot != null && VfsUtilCore.isAncestor(contentRoot, newSourceRoot, false)) { contentEntry.addSourceFolder(newSourceRoot, false); contentToChangeFound = true; } } assertTrue(contentToChangeFound); rootModel.commit(); } ); }