Skip to content

fix: keep alias script-refs resolvable across symlinked paths - #2806

Open
maxandersen-agent wants to merge 1 commit into
jbangdev:mainfrom
maxandersen-agent:2796-alias-realpath
Open

maxandersen-agent wants to merge 1 commit into
jbangdev:mainfrom
maxandersen-agent:2796-alias-realpath

Conversation

@maxandersen-agent

@maxandersen-agent maxandersen-agent commented Dec 4, 2026 •

Copy link
Copy Markdown
Collaborator

Catalog.relativize mixed a base path going through a symlink (macOS /tmp -> /private/tmp) with a fully resolved script path, storing ../.. refs that don't resolve.

reworked after discussion: blanket toRealPath() would also flatten intentional symlinks (alias add /apps/current/tool.java pinning to v2). so now:

  • Util.getCwd() prefers the logical path from $PWD (symlinks intact) when its realpath matches the JVM's physical cwd (verified, not trusted - stale/absent/windows $PWD falls back to user.dir as before)
  • relativize() computes the ref with the paths as given first; only when the result doesn't lead back to the script (checked via the file system - link/.. must not be collapsed textually) it redoes the math with symlinks resolved

tests: regression for the broken /tmp case (red without fix), a test asserting an intentional current->v2 style symlink survives in the stored ref (fails against the blanket-realpath approach), and unit tests for the $PWD validation. original repro verified e2e: stored ref is now ../aliased.java and runs.

Fixes #2796

@maxandersen-agent maxandersen-agent changed the title fix: resolve symlinks before relativizing alias script-refs fix: keep alias script-refs resolvable across symlinked paths Dec 4, 2026
Catalog.relativize() could mix a base path going through a symbolic
link (e.g. macOS /tmp, which links to /private/tmp) with a fully
resolved script path, storing ../.. script-refs that walk the wrong
way and don't resolve.

Two-part fix that keeps intentional symlinks (e.g. current -> v2) alive
in stored refs:

- Util.getCwd() now prefers the logical path from $PWD (symbolic links
  intact), but only when it verifiably points at the same directory as
  the JVM's physical working directory; a missing, stale or unusable
  $PWD falls back to user.dir as before.
- relativize() first computes the ref with the paths as given
  (preserving symlinks); only when the result doesn't actually lead
  back to the script (checked via the file system, not textually) it
  redoes the calculation with symlinks resolved on both sides.

Fixes jbangdev#2796

Co-authored-by: maxandersen-agent <362451386+maxandersen-agent@users.noreply.github.com>
@maxandersen

Copy link
Copy Markdown
Collaborator

this is an edge case of figuring out path for alias relative to a catalog - we could get absolute path for both and it would work but paths would then be some longwinded relative or absolute path that is not related to what the user actually see/use when creating it. Thus going to change jbang's cwd to honor PWD env if and only if it matches the absolute path of current working dir of java. That should ensure we use relative/logical path's when things "fits" and otherwise fall back to relative/physical paths.

@maxandersen
maxandersen marked this pull request as ready for review December 4, 2026 13:23
@maxandersen

Copy link
Copy Markdown
Collaborator

@quintesse wdyt?

@maxandersen
maxandersen requested a review from quintesse December 4, 2026 13:23
@coderabbitai

coderabbitai Bot commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

Walkthrough

getCwd() now validates $PWD against the physical working directory and retains it when both paths resolve to the same directory. Catalog.relativize() first tries the supplied paths, then retries with real paths if the candidate does not resolve. Tests cover working-directory validation and alias paths involving symbolic links.

Severity of issue fixed: Medium

Merge Risk: Moderate · up to f2faf

Alias creation with a symlinked catalog directory can now store a reference that fails to resolve when the symlink target lives under a different parent than the script. This worked before the change, so it should be fixed before merging. The working-directory edge case and the test fixture gap are smaller follow-ups.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments

Major 1
  1. Keep the fallback compatible with runtime path resolution.
Minor 2
  1. Validate the normalized path before returning it.
  2. Check that the candidate identifies the selected script.
Nitpick 1
  1. Make the regression fixture exercise the retry.
    src/test/java/dev/jbang/cli/TestAlias.java, lines 135 · 📐 Maintainability & Code Quality · ⚡ Quick win
    realcat and linkcat are siblings. Therefore, ../scripts/test.java resolves correctly before the retry, and this fixture does not exercise the changed fallback.
    Place the target under a different parent, such as real/deep. Also resolve the saved alias through the runtime resource resolver. The existing filesystem assertion misses the later normalization that breaks that layout. (raw.githubusercontent.com)

// symlinks fully resolved on both sides.
// NB: deliberately not normalize()d, "link/.." must be resolved
// by the file system, not textually
if (!Files.exists(base.resolve(scriptRef))) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Minor · Data Integrity & Integration · Quick win

Check that the candidate identifies the selected script.

With /work/linkcat -> /work/real/deep, the candidate for /work/scripts/test.java is ../scripts/test.java. If /work/real/scripts/test.java also exists, this guard skips the retry. Alias.resolve() and alias listing then expose a filesystem reference to that other file. Runtime normalization happens to mask this case, but the returned reference is still incorrect. (raw.githubusercontent.com)

Compare the candidate with script using filesystem identity. Preserve the logical reference only when both identify the same file.

// NB: deliberately not normalize()d, "link/.." must be resolved
// by the file system, not textually
if (!Files.exists(base.resolve(scriptRef))) {
scriptRef = relativizeIn(toRealPathSafe(base), toRealPathSafe(script));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Major · Data Integrity & Integration · Quick win

Keep the fallback compatible with runtime path resolution.

With /work/linkcat -> /work/real/deep and script /work/scripts/test.java, this retry stores ../../scripts/test.java. Alias.resolve() expands it under /work/linkcat. The runtime FileResourceResolver then normalizes that path to /scripts/test.java, so the alias fails to resolve. The previous ../scripts/test.java normalized to the correct script. (raw.githubusercontent.com)

Use an absolute real script path on this fallback, or update the runtime consumer to preserve filesystem traversal. Keep the successful logical-path branch unchanged.

try {
Path logical = Paths.get(pwdEnv);
if (logical.isAbsolute() && logical.toRealPath().equals(physical.toRealPath())) {
return logical.normalize();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Minor · Functional Correctness · Quick win

Validate the normalized path before returning it.

If /work/link points to /work/real/child, PWD=/work/link/.. passes validation against physical cwd /work/real. This return changes the path to /work, so subsequent relative file access uses another directory. normalize() can change filesystem identity across symlinks. (docs.oracle.com)

Normalize the candidate before the comparison, then return that same validated candidate. If it no longer matches, return physical.

@github-actions

github-actions Bot commented Dec 4, 2026

Copy link
Copy Markdown

📦 PR Build Available

Install this PR build:

curl -sL https://github.com/jbangdev/jbang-pr-builds/releases/download/pr-2806/jbang.tar | tar xf - && ./jbang/bin/jbang version

Release
| Built from f2faf10

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

alias add stores unresolvable ../.. script-ref when cwd is under a symlinked path

2 participants