Skip to content

Conversation

@sambsnyd
Copy link
Member

@sambsnyd sambsnyd commented Feb 3, 2026

Gradle doesn't allow cyclic dependencies... usually. But we've found at least one case where, with some creative usage of composite builds, it is possible.

This breaks cycles in the dependency representation, preserving the evidence in the GradleConfiguration's exception fields.

Gradle doesn't allow cyclic dependencies... usually. But we've found at least one case where, with some creative usage of composite builds, it is possible.

This breaks cycles in the dependency representation, preserving the evidence in the GradleConfiguration's exception fields.

import java.util.*;

import static org.assertj.core.api.Assertions.assertThat;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import static org.assertj.core.api.Assertions.assertThat;
import static java.util.Collections.emptySet;
import static org.assertj.core.api.Assertions.assertThat;

if (resolvedDependency == null) {
org.openrewrite.maven.tree.Dependency requested = gaToRequested.getOrDefault(ga, dependency(resolved));
// Track the traversal path to detect cycles at any depth
List<GroupArtifactVersion> ancestorPath = new ArrayList<>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A HashSet would probably be a bit quicker and gives the same uniqueness constraint while also having the helpful boolean add(T) method.

I'm also not sure if we actually need to track version, but it won't hurt. Gradle's dependency tree puts the max version everywhere by the time we get to traverse it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HashSet loses order so we couldn't reproduce the A -> B -> C -> A as easily for meaningful error message. Could use LinkedHashSet, though

Copy link
Contributor

@shanman190 shanman190 Feb 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, I see. It probably isn't worth it then in that case.

sambsnyd and others added 2 commits February 2, 2026 21:26
…te/gradle/marker/GradleProjectBuilderTest.java

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
@sambsnyd sambsnyd merged commit 3d5a392 into main Feb 3, 2026
2 checks passed
@sambsnyd sambsnyd deleted the no-dependency-cycles branch February 3, 2026 05:48
@github-project-automation github-project-automation bot moved this from In Progress to Done in OpenRewrite Feb 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants