private static boolean createAndMaybeAttachNewModuleDependency(@NotNull DataNode<LibraryDependencyData> libraryDependencyDataNode,
                                                                 @NotNull ModuleLookupResult lookupResult,
                                                                 @NotNull Set<String> libraryPaths,
                                                                 boolean shouldKeepTransitiveDependencies,
                                                                 @NotNull ArrayDeque<String> unprocessedPaths,
                                                                 int classpathOrderShift, String path) {

    boolean addedNewDependency = false;
    LibraryDependencyData libraryDependencyData = libraryDependencyDataNode.getData();
    DataNode<?> libraryNodeParent = libraryDependencyDataNode.getParent();
    if (libraryNodeParent == null) {
      return addedNewDependency;
    }

    DataNode<GradleSourceSetData> targetSourceSetNode = lookupResult.sourceSetDataDataNode();
    ExternalSourceSet targetExternalSourceSet = lookupResult.externalSourceSet();
    final ModuleData targetModuleData = targetSourceSetNode.getData();

    Set<String> targetModuleOutputPaths = collectTargetModuleOutputPaths(libraryPaths,
                                                               targetSourceSetNode.getUserData(GradleProjectResolver.GRADLE_OUTPUTS));

    final ModuleData ownerModule = libraryDependencyData.getOwnerModule();
    final ModuleDependencyData moduleDependencyData = new ModuleDependencyData(ownerModule, targetModuleData);
    moduleDependencyData.setScope(libraryDependencyData.getScope());
    moduleDependencyData.setOrder(libraryDependencyData.getOrder() + classpathOrderShift + 1);
    moduleDependencyData.setModuleDependencyArtifacts(Collections.singleton(path));

    if (targetExternalSourceSet != null && isTestSourceSet(targetExternalSourceSet)) {
      moduleDependencyData.setProductionOnTestDependency(true);
    }
    final DataNode<ModuleDependencyData> found = findChild(
      libraryNodeParent, ProjectKeys.MODULE_DEPENDENCY, node -> {
        ModuleDependencyData candidateData = node.getData();

        if (!moduleDependencyData.getInternalName().equals(candidateData.getInternalName())) {
          return false;
        }
        // re-use module dependency artifacts even if the rest does not match
        moduleDependencyData.setModuleDependencyArtifacts(candidateData.getModuleDependencyArtifacts());

        final boolean result;
        // ignore provided scope during the search since it can be resolved incorrectly for file dependencies on a source set outputs
        // ignore scope if the candidate already has the largest possible scope - compile
        if (moduleDependencyData.getScope() == DependencyScope.PROVIDED ||
            candidateData.getScope() == DependencyScope.COMPILE) {
          result = isEqualIgnoringScope(moduleDependencyData, candidateData);
        }
        else {
          result = moduleDependencyData.equals(candidateData);
        }
        return result;
      });

    if (found == null) {
      DataNode<ModuleDependencyData> moduleDependencyNode =
        libraryNodeParent.createChild(ProjectKeys.MODULE_DEPENDENCY, moduleDependencyData);
      addedNewDependency = true;
      if (shouldKeepTransitiveDependencies) {
        for (DataNode<?> node : libraryDependencyDataNode.getChildren()) {
          moduleDependencyNode.addChild(node);
        }
      }
    }

    if (targetModuleOutputPaths != null) {
      unprocessedPaths.removeAll(targetModuleOutputPaths);
      libraryPaths.removeAll(targetModuleOutputPaths);
    }
    return addedNewDependency;
  }
private static boolean createAndMaybeAttachNewModuleDependency(@NotNull DataNode<LibraryDependencyData> libraryDependencyDataNode,
                                                                 @NotNull ModuleLookupResult lookupResult,
                                                                 @NotNull Set<String> libraryPaths,
                                                                 boolean shouldKeepTransitiveDependencies,
                                                                 @NotNull ArrayDeque<String> unprocessedPaths,
                                                                 int classpathOrderShift, String path)