From df17bea05d2c547ccbb399c147a9b08484e79e6c Mon Sep 17 00:00:00 2001 From: Carlos Ernesto Alvarez Berumen Date: Fri, 6 Mar 2026 23:16:17 -0600 Subject: [PATCH] v1 --- .../texera/amber/engine/common/Utils.scala | 50 ++++++++++++++----- 1 file changed, 38 insertions(+), 12 deletions(-) diff --git a/amber/src/main/scala/org/apache/texera/amber/engine/common/Utils.scala b/amber/src/main/scala/org/apache/texera/amber/engine/common/Utils.scala index dc074c1094d..49d45cc7624 100644 --- a/amber/src/main/scala/org/apache/texera/amber/engine/common/Utils.scala +++ b/amber/src/main/scala/org/apache/texera/amber/engine/common/Utils.scala @@ -22,7 +22,6 @@ package org.apache.texera.amber.engine.common import com.typesafe.scalalogging.LazyLogging import org.apache.texera.amber.engine.architecture.rpc.controlreturns.WorkflowAggregatedState -import java.nio.file.{Files, Path, Paths} import java.util.concurrent.locks.Lock import scala.annotation.tailrec @@ -36,25 +35,52 @@ object Utils extends LazyLogging { * * @return the real absolute path to amber home directory */ + + import java.nio.file.{Files, Path, Paths} + import scala.jdk.CollectionConverters._ + import scala.util.Using + lazy val amberHomePath: Path = { val currentWorkingDirectory = Paths.get(".").toRealPath() - // check if the current directory is the amber home path + if (isAmberHomePath(currentWorkingDirectory)) { currentWorkingDirectory } else { - // from current path's parent directory, search its children to find amber home path - // current max depth is set to 2 (current path's siblings and direct children) - val searchChildren = Files - .walk(currentWorkingDirectory.getParent, 2) - .filter((path: Path) => isAmberHomePath(path)) - .findAny - if (searchChildren.isPresent) { - searchChildren.get - } else { + val parent = Option(currentWorkingDirectory.getParent).getOrElse { throw new RuntimeException( - "Finding texera home path failed. Current working directory is " + currentWorkingDirectory + s"Cannot search for texera home from filesystem root: $currentWorkingDirectory" ) } + + // Pass 1: prefer the closest prefix (deepest ancestor) of currentWorkingDirectory + val closestPrefix: Option[Path] = + Using.resource(Files.walk(parent, 2)) { stream => + stream + .iterator() + .asScala + .filter(path => isAmberHomePath(path)) + .map(_.toRealPath()) // normalize after filtering + .filter(path => path.startsWith(currentWorkingDirectory)) + .maxByOption(_.getNameCount) // deepest prefix = closest ancestor + } + + closestPrefix.getOrElse { + // Pass 2: fallback to any valid match + val anyMatch = + Using.resource(Files.walk(parent, 2)) { stream => + stream + .filter((path: Path) => isAmberHomePath(path)) + .findAny() + } + + if (anyMatch.isPresent) { + anyMatch.get().toRealPath() + } else { + throw new RuntimeException( + s"Finding texera home path failed. Current working directory is $currentWorkingDirectory" + ) + } + } } } val AMBER_HOME_FOLDER_NAME = "amber";