Skip to content

feat(readlink): add -f, -e, -m canonicalize modes and -n - #220

Merged
ferdingler merged 2 commits into
strands-agents:mainfrom
ferdingler:feat/readlink-canonicalize
Nov 12, 2026
Merged

ferdingler merged 2 commits into
strands-agents:mainfrom
ferdingler:feat/readlink-canonicalize

Conversation

@ferdingler

@ferdingler ferdingler commented Nov 6, 2026 •

Copy link
Copy Markdown
Collaborator

Description

readlink only printed a link's target and rejected every option, so the usual way to get an absolute path or a script's directory, dirname "$(readlink -f "$0")", failed:

$ readlink -f /tmp/run
strands-shell: readlink: invalid option '-f'

Now readlink supports GNU's -f, -e and -m (follow every symlink, with all but the last part, all parts or no part required to exist) and -n. Relative paths resolve against the shell's cwd, and plain readlink link works as before. With the change the example prints /tmp/proj/bin/run. I didn't add multiple operands or -v/-q, and I didn't touch the direct-bind code, so this shouldn't overlap with #194.

I compared output and exit codes with GNU readlink on Linux for about 80 cases: symlink chains (including ones longer than 40 links), links to directories, .., missing parts, links whose target ends in a slash, and loops. All of them matched. The one difference I know of is a link that keeps growing the path (g -> g/x) under -m. GNU prints a garbled path there, and we exit 1.

Related Issues

Part of #207

Documentation PR

Type of Change

New feature

Testing

How have you tested the change? Verify that the changes do not break functionality or introduce new warnings.

  • I ran the relevant test suites for the bindings I touched (cargo test --workspace --all-targets, pytest tests/python, npm test)
  • If I touched Rust, I ran cargo fmt and cargo clippy

Checklist

  • I have read the CONTRIBUTING document
  • I have reviewed and understand every line of code in this PR, including any generated by AI tools, and I can explain why it works
  • My change is focused and reasonably small; I have split unrelated work into separate PRs
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@ferdingler
ferdingler force-pushed the feat/readlink-canonicalize branch from a33336f to 7a54f67 Compare November 6, 2026 06:01
@ferdingler
ferdingler force-pushed the feat/readlink-canonicalize branch from 7a54f67 to 46d8e21 Compare November 12, 2026 20:13
@ferdingler
ferdingler marked this pull request as ready for review November 12, 2026 20:13
@ferdingler
ferdingler requested a review from a team as a code owner November 12, 2026 20:13
@ferdingler

Copy link
Copy Markdown
Collaborator Author

@strandly-the-agent Please review this PR

@strandly-the-agent strandly-the-agent left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Changes requested, one blocker: -f/-e/-m don't follow any symlink under a [[bind]] mode = "direct" mount, and they exit 0 with the wrong path.

@ferdingler On the in-memory VFS the walker matches GNU readlink 9.1 everywhere I looked, and the full suite is green. The bind issue is a single-function fix, with a suggestion inline. Under a direct bind, readlink -f ld prints /tmp/bx/ld instead of /tmp/bx/d, and readlink -f loop1 prints a path with exit 0 where GNU exits 1. The root cause is older: VfsKernel::lstat follows links on host paths, filed as #254. The walker is the first caller that depends on is_symlink there, so the PR needs a fix or a workaround before merge.

With the suggested lookup() change, my bind differential goes from 90 differences to 4 out of 248 cases. Those 4 are plain readlink x/ with a trailing slash and behave the same on main. The readlink and bind tests still pass (60).

✅ What I verified
  • Head 46d8e21 (base main d4c57a7). cargo test --workspace --all-targets: 2246 passed, 0 failed.
  • Differential against GNU readlink 9.1, 62 paths × (none, -f, -e, -m) = 248 cases. In-memory VFS: 4 differences, all the pre-existing trailing-slash case. Direct bind: 90 differences, 86 of them from -f/-e/-m. With the suggested fix: 4 differences on both.
  • Adversarial run on the VFS, 339 cases (/.., //x, x/.. over files, missing paths and dangling links, links to / and .. chains, '', -, --, loops, long chains, -n, combined flags): everything matched except the two ⚪ below. Nothing hung: the worst crafted ..-expansion link set takes about 0.1 s and exits 1.
  • The new hand-written walker is justified. vfs.canonicalize_path collapses .. before it follows links (lsub/.. gives /tmp/fx where GNU gives /tmp/fx/d), and Kernel::canonicalize only covers the -e case, so neither can back -f/-m.
  • Repeated flags: the last one wins, as in GNU. A failure exits 1 with no message, which matches GNU without -v. For a growing link (g -> g/x), GNU 9.1 actually hangs rather than printing a garbled path, so exiting 1 is the better outcome.
Questions (non-blocking)
  • Would you add one direct-bind case next to bind_direct_readlink_prints_stored_link_text (tests/shell_integration.rs:6877)? All the new tests build their tree under /tmp/rc in the in-memory VFS, which is how this got past the 80-case comparison.
Appendix: non-blocking (3)
  • ⚪ readlink.rs:12: "no component must exist" can be read as "components must not exist". GNU says "without requirements on components existence". Something like "components need not exist" is clearer, and agents do read --help.
  • ⚪ MAX_SYMLINKS = 256 (readlink.rs:30): a chain of 256 or more links exits 1, while GNU resolves it. The kernel can't open such a path anyway (its limit is 40), so this is fine to leave.
  • ⚪ Under -m, a loop reached through another link (lviala -> d/sub/../../la, with la and lb pointing at each other) stops at la where GNU stops at lb. The -m result for a loop doesn't mean anything either way.
  • Pre-existing, filed as #254: lstat follows links on direct binds, so test -L and [ -h ] are also wrong there.
Reading order for human reviewers

canonicalize (readlink.rs:81-200), then lookup (:207), then the readlink_links_case! tests in tests/shell_integration.rs.

Evidence (portal): strands-agents/shell/pr/220/{cargo-test.log, differential-harness.txt, differential-results.txt, adversarial-divergences.txt}

Comment thread src/commands/readlink.rs Resolved
@ferdingler

Copy link
Copy Markdown
Collaborator Author

@strandly-the-agent Please review this PR

@strandly-the-agent strandly-the-agent left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Approve. The 🔴 from my last review is fixed at c8dc96a, and the fix holds up.

@ferdingler Status of earlier findings:

  • 🔴 symlinks not followed under a direct bind: fixed. My 248-case comparison against GNU readlink 9.1 now shows the same 4 differences on the in-memory VFS and on a direct bind (down from 90 on the bind). All 4 are plain readlink x/ with a trailing slash, which behaves the same on main.
  • 🟡 missing direct-bind test: fixed, by bind_direct_readlink_canonicalizes_symlinks.
  • ⚪ -m help wording: fixed. ⚪ 256-link cap and ⚪ -m loop endpoint: kept on purpose, and that's fine.
✅ What I verified
  • Head c8dc96a. cargo test --workspace --all-targets: 2247 passed, 0 failed.
  • Attacked the fix itself:
    • Bind confinement: host links that point outside the bind (absolute /tmp/cf/outside, and relative ../outside and ../outside/secret) resolve in the shell's own namespace and exit 1 under -e/-f. Nothing about the host leaks.
    • -m /b/hrel/../b/lin → /b/in, which is correct.
  • Keeping st.is_symlink || link_target.is_some() is right. If a link's target can't be read, st.link_target? (readlink.rs:180) fails the lookup instead of returning a half-resolved path.
  • In-memory VFS spot checks (ld/../d through a link, -e ld/, -m ld/zz/..) match GNU.
  • #254 (lstat following links on binds, so test -L is wrong there) is still open and is still the root fix. This PR's workaround is local to readlink and doesn't depend on it.

Evidence (portal): strands-agents/shell/pr/220/differential-results-c8dc96a.txt, cargo-test-c8dc96a.log

@ferdingler
ferdingler merged commit 668dce9 into strands-agents:main Nov 12, 2026
31 checks passed
@ferdingler
ferdingler deleted the feat/readlink-canonicalize branch November 12, 2026 23:01
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.

2 participants