This is an automated email from the ASF dual-hosted git repository.
cstamas pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-resolver.git
The following commit(s) were added to refs/heads/master by this push:
new c24827bf2 Add CycleAwareDependencyGraphDumper to visualize cycles
(#2241)
c24827bf2 is described below
commit c24827bf20c3c7500be1306b24fab9f507d4f975
Author: Arturo Bernal <[email protected]>
AuthorDate: Tue Nov 3 20:33:47 2026 +0100
Add CycleAwareDependencyGraphDumper to visualize cycles (#2241)
DependencyGraphDumper recurses forever on a graph containing a cycle, and
TreeDependencyVisitor avoids that crash only by hiding every node already
seen anywhere in the graph, cycle or not, so a normal diamond dependency
disappears from the output along with the actual cycle.
CycleAwareDependencyGraphDumper only tracks the path from the root to the
node currently being visited: it prints a node and marks it '(cycle)'
once it reaches an ancestor of itself, instead of descending into it, and
otherwise leaves diamonds fully visible.
Fixing this also exposed a latent bug in formatIndentation(): it picked
out the node currently being formatted by comparing it against
nodes.peekFirst() with ==, which breaks once the same node instance
appears twice in the path, i.e. exactly in a cycle. Comparing by position
in the walk instead fixes it for both dumpers.
---
.../visitor/CycleAwareDependencyGraphDumper.java | 85 ++++++++++++++++++++++
.../util/graph/visitor/DependencyGraphDumper.java | 20 ++++-
.../CycleAwareDependencyGraphDumperTest.java | 70 ++++++++++++++++++
3 files changed, 172 insertions(+), 3 deletions(-)
diff --git
a/maven-resolver-util/src/main/java/org/eclipse/aether/util/graph/visitor/CycleAwareDependencyGraphDumper.java
b/maven-resolver-util/src/main/java/org/eclipse/aether/util/graph/visitor/CycleAwareDependencyGraphDumper.java
new file mode 100644
index 000000000..0a2be8186
--- /dev/null
+++
b/maven-resolver-util/src/main/java/org/eclipse/aether/util/graph/visitor/CycleAwareDependencyGraphDumper.java
@@ -0,0 +1,85 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.eclipse.aether.util.graph.visitor;
+
+import java.util.Collection;
+import java.util.function.Consumer;
+import java.util.function.Function;
+
+import org.eclipse.aether.graph.DependencyNode;
+
+/**
+ * A {@link DependencyGraphDumper} that is safe to use on a graph containing
cycles: a dependency graph in
+ * {@code FULL} verbosity mode may contain nodes that (directly or
transitively) depend on an ancestor of
+ * themselves, which makes the plain {@link DependencyGraphDumper} recurse
forever. Wrapping it in
+ * {@link TreeDependencyVisitor} avoids the stack overflow but also hides
every node that was already visited
+ * anywhere else in the graph, so a node reached a second time through a
different path (a perfectly normal
+ * diamond dependency, not a cycle) is silently dropped from the output as
well.
+ * <p>
+ * This dumper only checks the path from the root to the node currently being
visited: a node is only treated as
+ * a cycle when it is already one of its own ancestors. Diamonds are still
printed in full, and a true cycle is
+ * printed once, marked, with its subtree cut off instead of being recursed
into.
+ *
+ * @since 2.0.25
+ */
+public class CycleAwareDependencyGraphDumper extends DependencyGraphDumper {
+
+ /**
+ * Creates instance with given consumer.
+ *
+ * @param consumer the string consumer, must not be {@code null}
+ */
+ public CycleAwareDependencyGraphDumper(Consumer<String> consumer) {
+ super(consumer);
+ }
+
+ /**
+ * Creates instance with given consumer and decorators.
+ *
+ * @param consumer the string consumer, must not be {@code null}
+ * @param decorators the decorators to apply, must not be {@code null}
+ */
+ public CycleAwareDependencyGraphDumper(
+ Consumer<String> consumer, Collection<Function<DependencyNode,
String>> decorators) {
+ super(consumer, decorators);
+ }
+
+ @Override
+ public boolean visitEnter(DependencyNode node) {
+ if (isOnPath(node)) {
+ nodes.push(node);
+ consumer.accept(formatLine(nodes) + " (cycle)");
+ return false;
+ }
+ return super.visitEnter(node);
+ }
+
+ /**
+ * Returns {@code true} if {@code node} is already one of its own
ancestors on the path from the root to the
+ * node currently being visited, i.e. descending into it would recurse
forever.
+ */
+ private boolean isOnPath(DependencyNode node) {
+ for (DependencyNode ancestor : nodes) {
+ if (ancestor == node) {
+ return true;
+ }
+ }
+ return false;
+ }
+}
diff --git
a/maven-resolver-util/src/main/java/org/eclipse/aether/util/graph/visitor/DependencyGraphDumper.java
b/maven-resolver-util/src/main/java/org/eclipse/aether/util/graph/visitor/DependencyGraphDumper.java
index 2d639f1da..761618252 100644
---
a/maven-resolver-util/src/main/java/org/eclipse/aether/util/graph/visitor/DependencyGraphDumper.java
+++
b/maven-resolver-util/src/main/java/org/eclipse/aether/util/graph/visitor/DependencyGraphDumper.java
@@ -236,11 +236,22 @@ public class DependencyGraphDumper implements
DependencyVisitor {
return result;
}
- private final Consumer<String> consumer;
+ /**
+ * The consumer of the formatted lines.
+ *
+ * @since 2.0.25
+ */
+ protected final Consumer<String> consumer;
private final List<Function<DependencyNode, String>> decorators;
- private final Deque<DependencyNode> nodes = new ArrayDeque<>();
+ /**
+ * The path from the root to the node currently being visited, with the
current node at the head. Exposed so
+ * subclasses (such as a cycle-aware dumper) can inspect the current path
without having to duplicate it.
+ *
+ * @since 2.0.25
+ */
+ protected final Deque<DependencyNode> nodes = new ArrayDeque<>();
/**
* Creates instance with given consumer.
@@ -289,7 +300,10 @@ public class DependencyGraphDumper implements
DependencyVisitor {
DependencyNode child = iter.hasNext() ? iter.next() : null;
while (parent != null && child != null) {
boolean lastChild =
parent.getChildren().get(parent.getChildren().size() - 1) == child;
- boolean end = child == nodes.peekFirst();
+ // Position in the walk, not identity: the same node instance can
appear more than once in the
+ // path when the graph has a cycle, in which case comparing child
against nodes.peekFirst() by
+ // identity would wrongly match an ancestor occurrence instead of
the one actually being visited.
+ boolean end = !iter.hasNext();
String indent;
if (end) {
indent = lastChild ? "\\- " : "+- ";
diff --git
a/maven-resolver-util/src/test/java/org/eclipse/aether/util/graph/visitor/CycleAwareDependencyGraphDumperTest.java
b/maven-resolver-util/src/test/java/org/eclipse/aether/util/graph/visitor/CycleAwareDependencyGraphDumperTest.java
new file mode 100644
index 000000000..7c0b1aa4d
--- /dev/null
+++
b/maven-resolver-util/src/test/java/org/eclipse/aether/util/graph/visitor/CycleAwareDependencyGraphDumperTest.java
@@ -0,0 +1,70 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.eclipse.aether.util.graph.visitor;
+
+import java.time.Duration;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import org.eclipse.aether.graph.DependencyNode;
+import org.eclipse.aether.internal.test.util.DependencyGraphParser;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;
+
+public class CycleAwareDependencyGraphDumperTest {
+
+ private DependencyNode parse(String resource) throws Exception {
+ return new
DependencyGraphParser("visitor/ordered-list/").parseResource(resource);
+ }
+
+ @Test
+ void dumpCyclesDoesNotRecurseForever() throws Exception {
+ DependencyNode root = parse("cycles.txt");
+ List<String> lines = new ArrayList<>();
+ assertTimeoutPreemptively(
+ Duration.ofSeconds(10), () -> root.accept(new
CycleAwareDependencyGraphDumper(lines::add)));
+
+ assertEquals(
+ Arrays.asList(
+ "gid:a:jar:1",
+ "+- gid:b:jar:1",
+ "| \\- gid:c:jar:1",
+ "| \\- gid:b:jar:1 (cycle)",
+ "\\- gid:d:jar:1",
+ " +- gid:b:jar:1",
+ " | \\- gid:c:jar:1",
+ " | \\- gid:b:jar:1 (cycle)",
+ " \\- gid:e:jar:1"),
+ lines);
+ }
+
+ @Test
+ void dumpSimpleMatchesPlainDumper() throws Exception {
+ DependencyNode root = parse("simple.txt");
+ List<String> plain = new ArrayList<>();
+ List<String> cycleAware = new ArrayList<>();
+ root.accept(new DependencyGraphDumper(plain::add));
+ root.accept(new CycleAwareDependencyGraphDumper(cycleAware::add));
+
+ assertEquals(plain, cycleAware);
+ }
+}