Skip to content

fix(core): prefer current repo when resolving amber home path#4263

Open
carloea2 wants to merge 7 commits intoapache:mainfrom
carloea2:fix/amber_home_path
Open

fix(core): prefer current repo when resolving amber home path#4263
carloea2 wants to merge 7 commits intoapache:mainfrom
carloea2:fix/amber_home_path

Conversation

@carloea2
Copy link
Contributor

@carloea2 carloea2 commented Mar 7, 2026

What changes were proposed in this PR?

This PR fixes an issue in amberHomePath resolution where the previous implementation could select the wrong repo when multiple sibling directories under the same parent satisfied isAmberHomePath(...).

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 {
throw new RuntimeException(
"Finding texera home path failed. Current working directory is " + currentWorkingDirectory
)
}
}
}

Previously, if the current working directory was not itself recognized as the Amber home path, the code would walk the parent directory and return findAny() from matching paths. In environments with multiple Texera/Amber repos under the same parent, this could resolve to an unrelated sibling repo instead of the repo the process was launched from.

This PR changes the logic to:

  • keep returning the current working directory immediately when it is already a valid Amber home path
  • guard against searching from filesystem root when there is no parent
  • prefer the closest valid match associated with the current working directory
  • fall back to any valid match only when no closer candidate is found
  • normalize returned paths with toRealPath()
  • close Files.walk(...) safely using Using.resource(...)

This makes Amber home path detection more deterministic and prevents accidentally selecting the wrong engine/repo in multi-repo local setups.

Any related issues, documentation, discussions?

Closes #4262

How was this PR tested?

Manually tested with a local directory layout where multiple sibling repos satisfy isAmberHomePath(...).

Example setup:

E:\texera\
  ├─ texera\
  └─ a\

Was this PR authored or co-authored using generative AI tooling?

No.

@carloea2
Copy link
Contributor Author

carloea2 commented Mar 7, 2026

@chenlica Can you assign a reviewer? Thanks.

@chenlica
Copy link
Contributor

chenlica commented Mar 7, 2026

@aglinxinyuan @Xiao-zhen-Liu Please review this PR.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Improves Utils.amberHomePath resolution to be more deterministic in environments where multiple sibling repositories may contain an amber/ directory, reducing the chance of selecting the wrong repo when launched from a non-amber working directory.

Changes:

  • Adds a two-pass search strategy to prefer a “closest” match before falling back to any match.
  • Adds safeguards for filesystem-root cases and normalizes returned paths via toRealPath().
  • Ensances resource safety by closing Files.walk(...) via Using.resource(...).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@carloea2
Copy link
Contributor Author

carloea2 commented Mar 9, 2026

Just curious, who requested copilot review?

@aglinxinyuan
Copy link
Contributor

Just curious, who requested copilot review?

I requested it. Please address the comments from Copilot before I have a pass.

@carloea2
Copy link
Contributor Author

@aglinxinyuan done.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +63 to +67
// Preserve the current behavior by preferring an amber directory discovered under the CWD.
amberCandidates
.filter(_.startsWith(realCurrentWorkingDirectory))
.maxByOption(_.getNameCount)
.orElse(amberCandidates.headOption)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I prefer the current way since the match is deeper, I think both options can be, it depends in the design decision. @aglinxinyuan what is your preference?

Copy link
Contributor

Choose a reason for hiding this comment

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

Could you provide an example of a user who would prefer the deeper one instead of the closer one? From my perspective, if I run a command in the current working directory, I would expect the binary in the closest folder to be used rather than the one in the deepest folder.

Comment on lines +32 to +35
val siblingRepo = Files.createDirectory(tempDirectory.resolve("sibling-repo"))
val preferredAmber = Files.createDirectories(preferredRepo.resolve("amber"))
Files.createDirectories(siblingRepo.resolve("amber"))

@aglinxinyuan aglinxinyuan requested review from aicam and removed request for Xiao-zhen-Liu March 16, 2026 06:27
Copy link
Contributor

@aglinxinyuan aglinxinyuan left a comment

Choose a reason for hiding this comment

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

LGTM! Please address Copilot comments.

I only tested on local. Since it might affect our servers, we might need to test it on the server before merging it. @carloea2, please coordinate with @aicam to test this change on the server environment or Ubuntu.

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Signed-off-by: carloea2 <carloea2@uci.edu>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Amber home path detection may select the wrong repo when sibling repos exist

4 participants