You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fix: keep alias script-refs resolvable across symlinked paths - #2806
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.
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.
Fixesjbangdev#2796
Co-authored-by: maxandersen-agent <362451386+maxandersen-agent@users.noreply.github.com>
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.
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.
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)
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.
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.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
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