From 421a9e8c9ea896d37949edf8d05e1e21aca03a94 Mon Sep 17 00:00:00 2001 From: Vaidas Pilkauskas Date: Tue, 17 Oct 2023 15:07:44 +0300 Subject: [PATCH] Fix handling of cycles in AST traversal in Dependency Analyzer (#1519) --- .../dependencyanalyzer/AstUsedJarFinder.scala | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/third_party/dependency_analyzer/src/main/io/bazel/rulesscala/dependencyanalyzer/AstUsedJarFinder.scala b/third_party/dependency_analyzer/src/main/io/bazel/rulesscala/dependencyanalyzer/AstUsedJarFinder.scala index 3282678a0..6de69068d 100644 --- a/third_party/dependency_analyzer/src/main/io/bazel/rulesscala/dependencyanalyzer/AstUsedJarFinder.scala +++ b/third_party/dependency_analyzer/src/main/io/bazel/rulesscala/dependencyanalyzer/AstUsedJarFinder.scala @@ -1,5 +1,6 @@ package io.bazel.rulesscala.dependencyanalyzer +import scala.collection.mutable import scala.reflect.io.AbstractFile import scala.tools.nsc.Global @@ -10,6 +11,7 @@ class AstUsedJarFinder( def findUsedJars: Map[AbstractFile, Global#Position] = { val jars = collection.mutable.Map[AbstractFile, global.Position]() + val visitedTrees = mutable.Set.empty[Tree] def recordUse(source: AbstractFile, pos: Position): Unit = { // We prefer to report locations which have information (e.g. @@ -94,12 +96,7 @@ class AstUsedJarFinder( tree.attachments .get[global.treeChecker.MacroExpansionAttachment] .foreach { attach => - // When we explore the original, the original also has - // this attachment. So we should not examine the original - // again if so. - if (attach.expandee != tree) { - fullyExploreTree(attach.expandee) - } + fullyExploreTree(attach.expandee) } val shouldExamine = @@ -133,7 +130,11 @@ class AstUsedJarFinder( } } - tree.foreach(visitNode) + // handle possible cycles in macro expandees + if (!visitedTrees.contains(tree)) { + visitedTrees += tree + tree.foreach(visitNode) + } } currentRun.units.foreach { unit =>