JetBrains/intellij-community · error · ConfigurationException

module.paths.validation.source.root.belongs.to.another.module.error

module.paths.validation.source.root.belongs.to.another.module.error

Error message

Source root ''{0}''
cannot be defined in module ''{1}'' because it belongs to content of nested module ''{2}''

What it means

Thrown by the Project Structure dialog when a source root configured in one module physically lives inside a content root owned by another, nested module. validateSourceRootsAcrossModules walks up from each source root to its declared content root and fails if any intermediate directory is registered as another module's content. IntelliJ rejects this because two modules would then claim the same files, breaking compilation and indexing.

Source

Thrown at java/idea-ui/src/com/intellij/openapi/roots/ui/configuration/ModulesConfigurator.java:359

    );
  }

  /**
   * Validates that source roots belong to the same module as their corresponding content root.
   */
  private static void validateSourceRootsAcrossModules(@NotNull Map<VirtualFile, VirtualFile> srcRootsToContentRootMap,
                                                       @NotNull Map<VirtualFile, String> contentRootToModuleNameMap) throws ConfigurationException {
    for (Map.Entry<VirtualFile, VirtualFile> entry : srcRootsToContentRootMap.entrySet()) {
      final VirtualFile srcRoot = entry.getKey();
      final VirtualFile correspondingContent = entry.getValue();
      final String expectedModuleName = contentRootToModuleNameMap.get(correspondingContent);

      for (VirtualFile candidateContent = srcRoot;
           candidateContent != null && !candidateContent.equals(correspondingContent);
           candidateContent = candidateContent.getParent()) {
        final String moduleName = contentRootToModuleNameMap.get(candidateContent);
        if (moduleName != null && !moduleName.equals(expectedModuleName)) {
          throw new ConfigurationException(
            JavaUiBundle.message("module.paths.validation.source.root.belongs.to.another.module.error", srcRoot.getPresentableUrl(), expectedModuleName, moduleName)
          );
        }
      }
    }
  }

  /**
   * Validates that all module editors can be applied.
   */
  private void validateModuleEditors() throws ConfigurationException {
    for (ModuleEditor moduleEditor : myModuleEditors.values()) {
      moduleEditor.canApply();
    }
  }

  /**
   * Creates a mapping from modified SDKs to original SDKs.

View on GitHub (pinned to be881553f2)

Solutions

  1. In Project Structure > Modules, open the offending module and remove or re-path the source root so it is no longer inside another module's content root
  2. Check the nested module (name in the error's third placeholder) and exclude the overlapping directory from its content entries
  3. If the modules were imported, re-import the Maven/Gradle project so roots are regenerated from the build files
  4. Inspect the two .iml files and fix the <content> / <sourceFolder> url attributes to not overlap

Example fix

<!-- before: module A .iml claims src that lives under module B's content root -->
<content url="file://$MODULE_DIR$/subproject/src" rootFolder="true">
<!-- after: remove the overlapping root from A; let B own it -->
<content url="file://$MODULE_DIR$/subproject" exported="">
  <excludeFolder url="file://$MODULE_DIR$/subproject/src" />
</content>
Defensive patterns

Strategy: validation

Validate before calling

// Before apply(), verify no source root is nested in another module's content root
Map<VirtualFile, String> contentToModule = collectContentRootToModuleName(modulesModel);
for (Module module : modulesModel.getModules()) {
  for (ContentEntry entry : ModuleRootManager.getInstance(module).getContentEntries()) {
    for (SourceFolder src : entry.getSourceFolders()) {
      for (VirtualFile p = src.getFile(); p != null && !p.equals(entry.getFile()); p = p.getParent()) {
        String owner = contentToModule.get(p);
        if (owner != null && !owner.equals(module.getName())) {
          // report: source root belongs to nested module 'owner'
        }
      }
    }
  }
}

Try / catch

try { configurator.apply(); } catch (ConfigurationException e) { /* show e.getMessage() near module tree */ }

Prevention

When it happens

Trigger: Calling apply/OK in Settings > Project Structure when module A declares a source root whose path is under a directory that is a content root of module B. Occurs after importing a project where nested modules overlap (e.g. a parent module whose content root contains a submodule's content root), or after manually editing .iml content/source entries so they overlap.

Common situations: Maven/Gradle imports producing overlapping module roots; monorepo layouts where an outer module was given a content root covering inner modules; duplicated content roots after moving directories without updating module configuration; hand-edited .iml files.

Related errors


AI-assisted analysis of JetBrains/intellij-community@be881553f2 (2026-08-14). Data as JSON: /api/errors/35baa795963a703c. Report an issue: GitHub.