Skip to content

fix(realpath): resolve symlinks anywhere in the path - #2957

Merged
chaliy merged 3 commits into
mainfrom
claude/project-thread-sx9opx
Nov 19, 2026
Merged

chaliy merged 3 commits into
mainfrom
claude/project-thread-sx9opx

Conversation

@chaliy

@chaliy chaliy commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Requested by Михайло · project thread

What changed

realpath now resolves symlinks anywhere in the path, using the same bounded walk as readlink -f (40-hop cap, path limits). The --relative-to/--relative-base paths are resolved the same way, and both now follow GNU: --relative-to prints ..-style paths (--relative-to=/a /b/c gives ../b/c, was /b/c), and --relative-base keeps paths outside DIR absolute.

  • -s keeps the old lexical-only behavior.
  • A symlink loop prints realpath: X: Too many levels of symbolic links and exits 1.
  • -e on a missing path reports the operand as given and moves on to the next one, like GNU (it used to stop at the first failure).

Why

realpath only cleaned . and .., so scripts that use it to find a script's real location or to compare paths got the symlinked path back. I found it while writing the #2933 separator tests.

Before / After

With /l -> /a/b:

$ realpath /l/c
before: /l/c
after:  /a/b/c        # GNU realpath: /a/b/c

New spec case realpath_follows_intermediate_symlink covers plain, -s, -e, --relative-to and cwd-inside-a-symlink cases. Its expected output matches real bash 5 / GNU coreutils. 7 new unit tests (the relative cases use GNU realpath -m output as expected values). Lib path:: 33 pass, integration 1932 pass.

Risk

  • Low. Output only changes for paths that go through a symlink. Default mode still doesn't require the parent to exist (unchanged, -E = -m here).

Checklist

  • Tests added or updated
  • Backward compatibility considered

Generated by Claude Code

realpath only cleaned . and .. lexically, so /l/c with /l -> /a/b printed
/l/c. Share readlink -f's bounded symlink walk for operands and the
--relative-to/--relative-base path; -s keeps lexical mode; loops report
ELOOP; -e failures continue to the next operand like GNU.
@chaliy chaliy self-assigned this Nov 19, 2026
@bashkit-pr-reviewer

Copy link
Copy Markdown

Review verdict: approve direction, fix --relative-to/--relative-base before merge

realpath sharing readlink -f's bounded walk (40-hop cap, VFS path limits, TM-DOS-013 guard) is the right fix for intermediate symlinks, and per-operand -e continue + loop message match GNU. No sandbox escape (stays on ctx.fs VFS calls) and no panic/DoS path I could see. submit_github_pull_request_review returned 422 twice (even for a probe comment on an added line), so findings are recorded here instead of inline.

Findings

  • [P1] --relative-to / --relative-base conflated (crates/bashkit/src/builtins/path.rs, base_path = relative_to.as_ref().or(relative_base.as_ref()) plus strip_prefix-only relativization): generated help says --relative-to = "print the resolved path relative to DIR" vs --relative-base = "print absolute paths unless paths below DIR". Two divergences: (1) realpath --relative-to=/a /b/c should yield a ..-style relative path, but strip_prefix failure keeps it absolute; (2) with both flags, the --relative-base gate is ignored. Fix with a real relative-path computation gated by --relative-base when present.
  • [P2] Stale Realpath struct doc (~L171-180: "Symlink resolution stays disabled per L-FS-001 ... VFS does not model symlinks"): contradicts this change and the actual L-FS-001 row (limitations.md:56, "Symlinks are followed, but .. ... resolves lexically"). The new decision comment at the canonical closure is good; update the struct doc in the same change per AGENTS.md.
  • [P2] Lossy strip_prefix (Path::new(&printed).strip_prefix(base_path.to_string_lossy().as_ref())): round-trips two PathBufs through lossy strings and borrows a temporary Cow as prefix. Use resolved.strip_prefix(&base_path) and only lossy-encode for output (non-UTF-8 VFS paths).
  • [P2] Base-loop failure ignores -q: base None path always prints "Too many levels..." while per-operand errors respect quiet. Gate the base warning on !quiet too (still exit 1).

Context read

Beyond the diff: Realpath::execute + follow_readlink_symlinks/canonicalize_readlink_path/readlink_path_within_limits, new unit tests (test_realpath_*) and path.test.sh:realpath_follows_intermediate_symlink, generated/realpath_args.rs help texts, AGENTS.md, knowledge/operations/limitations.md (L-FS-001), knowledge/log.md. No build/test run per sandbox rules (reasoned from code).

--relative-to prints ../-style relative paths; --relative-base prints
paths under DIR relative and others absolute, and turns --relative-to off
when DIR is outside it. -q silences a base loop error. Fix stale doc.
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
bashkit c04e096 Commit Preview URL

Branch Preview URL
Nov 19 2026, 06:10 PM

@chaliy
chaliy merged commit ce1334b into main Nov 19, 2026
67 checks passed
@chaliy
chaliy deleted the claude/project-thread-sx9opx branch November 19, 2026 18:56
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.

1 participant