Skip to content

Keep relative node:fs symlink targets as written - #8463

Merged
littledivy merged 1 commit into
mainfrom
littledivy/2026-11-13-fs-symlink-relative
Nov 18, 2026
Merged

littledivy merged 1 commit into
mainfrom
littledivy/2026-11-13-fs-symlink-relative

Conversation

@littledivy

@littledivy littledivy commented Nov 14, 2026 •

Copy link
Copy Markdown
Member

symlinkSync(target, path) resolved a relative target against process.cwd() (/bundle by default) and stored the absolute result. Node stores the target as written and resolves it from the link's directory. So readlinkSync returned /bundle/sub/x.txt for sub/x.txt, and reading through the link failed unless the target happened to exist under the cwd. The callback and fs/promises forms behaved the same. Fixes #7679.

In this patch:

  • A relative target resolves against the directory that holds the link, .. included, and SymbolicLink keeps the string or Buffer target as written next to its absolute path.
  • readlink returns the target as written. realpath still returns the absolute path.
  • cp of a symlink writes the absolute target, as Node does without verbatimSymlinks, so a copy in another directory still points at the same file.
  • The target is resolved once, when the link is created. Renaming a relative link, or a directory above it, to another directory keeps it pointing at the old location, where Node would resolve it again. The VFS directories don't know their own path, so resolving on every follow would need a larger change.

No compat flag: until now a relative link only worked when the target existed under the cwd, links can only be created in /tmp, which doesn't persist across requests, and earlier node:fs parity fixes (#7393, #7394) changed behaviour the same way. A Worker that used a relative target to reach a /bundle file now needs an absolute target, as it would on Node.

@littledivy
littledivy requested review from a team as code owners November 14, 2026 04:39

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Superseded by the review of a350e23e.

This review, of 3b85145b

💬 Approved with comments — The fix is correct for the paths it targets. One WARNING remains: readlink on a relative symlink copied with cp returns the target as written, which no longer matches where the copy points.

Findings: 1 warning (1 is an inline comment on the diff)

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.

🔧 Fix with your agent

Paste the following into your coding agent to investigate and fix these issues.

Fix the following code review issues found in pull request #8463.

Before making changes, review each issue and present a brief summary table:
- For each finding, state whether you agree, disagree, or need clarification
- If you disagree (e.g., the fix would require disproportionate changes
  for minimal ROI, or the finding is factually incorrect), explain why
- If you need to clarify something before deciding, ask those questions
- Then share your plan for which issues to tackle and in what order

## WARNING (1)

### [Code Quality] readlink on a relative symlink copied with cp/cpSync returns the relative target as written, but the copy still points at the original location, so resolving that string from the copy's directory gives the wrong path.
- **File:** src/workerd/api/filesystem.c++:354

📋 Code Quality: ⚠️ 1 warning

Relative symlinks copied with cp report a readlink target that disagrees with where they point.

Recommendation: 📝 Minor Comments

  • [WARNING] readlink on a relative symlink copied with cp/cpSync returns the relative target as written, but the copy still points at the original location, so resolving that string from the copy's directory gives the wrong path. (src/workerd/api/filesystem.c++:354)
View full review

Copied relative symlinks report a misleading target (src/workerd/api/filesystem.c++:353-355, handleCpLink at src/workerd/api/filesystem.c++:1364-1408)

handleCpLink copies a symlink by adding the same kj::Rc<SymbolicLink> at the destination (line 1408). The copy therefore shares both rawTarget and the resolved absolute targetPath.

Example: copy /tmp/r/link -> 'sub/x.txt' to /tmp/other/link. readlinkSync('/tmp/other/link') returns 'sub/x.txt', but the copy still resolves to /tmp/r/sub/x.txt. Code that runs path.resolve(path.dirname(link), fs.readlinkSync(link)) gets the wrong path.

Node's cp avoids this by default (verbatimSymlinks: false): it rewrites the copy's target as an absolute path. Two possible fixes:

  • Have handleCpLink create a new SymbolicLink with no raw target, so readlink on the copy returns the absolute path.
  • Document this limitation alongside the rename one.

The updated cpSync/cp comments say a copy resolves to the same absolute path as the source. That is true for resolution, but not for what readlink returns. A test that copies a relative symlink and checks readlink would pin whichever behaviour you choose.


✅ All other checks passed

🔒 Security: ✅ LGTM

No security issues found.


📖 AGENTS.md: ✅ LGTM

This pull request does not change any AGENTS.md file. It records the decision to ship without a compatibility flag in the commit message, as the repository's backward-compatibility guidance requires.


⚡ Performance: ✅ LGTM

The added work is a little string handling each time a symlink is created or read, which costs nothing measurable.


📝 Documentation: ✅ LGTM

The new comments match the behaviour.


Reviewed commit: 3b85145b

Comment thread src/workerd/api/filesystem.c++ Resolved
@littledivy
littledivy force-pushed the littledivy/2026-11-13-fs-symlink-relative branch from 3b85145 to a350e23 Compare November 14, 2026 04:45

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Superseded by the review of bd09d545.

This review, of a350e23e

💬 Approved with comments — The fix is correct, and the earlier problem with copied relative symlinks is now resolved and tested. One INFO note remains about a header comment.

Findings: 1 info (1 is an inline comment on the diff)

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


📝 Documentation: 💬 1 info

The SymbolicLink header comment says the written target is always kept, which is not true for copied relative links.

Recommendation: 💬 Optional Nits

  • [INFO] The SymbolicLink comment says the written target is always kept, but copies of relative links made with cp do not keep it. (src/workerd/io/worker-fs.h:500)
View full review
  • src/workerd/io/worker-fs.h:499-500 says the target as written is kept separately for readlink. handleCpLink drops it for copies of relative links, so readlink on those copies returns the resolved absolute path.

✅ All other checks passed

🔒 Security: ✅ LGTM

No security issues found.


📖 AGENTS.md: ✅ LGTM

No AGENTS.md changes. The commit message records the decision to ship without a compatibility flag, as the repository's backward-compatibility guidance asks.


📋 Code Quality: ✅ LGTM

handleCpLink (src/workerd/api/filesystem.c++:1407-1415) now creates a new SymbolicLink from srcLink->getTargetUrl() and keeps the written target only when it is absolute. So readlink on a copy of a relative link returns the absolute path, as Node's cp does without verbatimSymlinks. fs-link-test.js checks this. The other paths are consistent:

  • symlinkSync, the callback form and fs/promises resolve a relative target against the link's directory.
  • realpath still returns the absolute path.
  • A URL target stores no written target.

⚡ Performance: ✅ LGTM

The only added work is a little string handling when a symlink is created, read or copied.


Reviewed commit: a350e23e

Comment thread src/workerd/io/worker-fs.h Resolved
@littledivy
littledivy force-pushed the littledivy/2026-11-13-fs-symlink-relative branch from a350e23 to bd09d54 Compare November 14, 2026 04:50

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Superseded by the review of 22113b7d.

This review, of bd09d545

✅ Approved — The header comment now matches how cp copies relative links, which resolves the only open note. No other issues were found.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

No security issues found.


📖 AGENTS.md: ✅ LGTM

No AGENTS.md changes. The commit message records the decision to ship without a compatibility flag.


📋 Code Quality: ✅ LGTM

The updated SymbolicLink comment matches handleCpLink. Both callers of newSymbolicLink pass the new target argument. A relative target resolves from the link's directory.


⚡ Performance: ✅ LGTM

The only added work is a little string handling when a symlink is created or read, which is not a hot path.


📝 Documentation: ✅ LGTM

src/workerd/io/worker-fs.h:499-501 now says that cp copies of relative links keep no written target, which matches the code.


Reviewed commit: bd09d545

@littledivy
littledivy force-pushed the littledivy/2026-11-13-fs-symlink-relative branch from bd09d54 to 22113b7 Compare November 14, 2026 05:29

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Superseded by the review of a8179315.

This review, of 22113b7d

✅ Approved — The changes since the last review keep relative symlink targets as written and resolve them from the link's directory, and no issues were found.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

Relative targets are normalized in kj::Path against the virtual filesystem root, so they cannot escape it. The target as written is only stored and returned by readlink; it is never used to resolve the link. A cp copy is built from the source's absolute target.


📖 AGENTS.md: ✅ LGTM

No AGENTS.md changes. The commit message records the decision to ship without a compatibility flag.


📋 Code Quality: ✅ LGTM

Every caller of newSymbolicLink and of the SymbolicLink constructor passes the new target argument or uses the kj::none default. readlink returns the target as written and realpath returns the absolute path. A relative target resolves against the link's URL, so .. works. A Buffer target is converted to a string, and a URL target falls back to the absolute path. The new tests cover the sync, callback and promises forms, cp, and chdir.


⚡ Performance: ✅ LGTM

The only added work is a little string handling when a symlink is created, read or copied, which is not a hot path.


📝 Documentation: ✅ LGTM

None of the comments changed since the last review need updating.


Reviewed commit: 22113b7d

`symlinkSync(target, path)` resolved a relative `target` against `process.cwd()` (`/bundle` by default) and stored the absolute result. Node stores the target as written and resolves it from the link's directory. So `readlinkSync` returned `/bundle/sub/x.txt` for `sub/x.txt`, and reading through the link failed unless the target happened to exist under the cwd. The callback and `fs/promises` forms behaved the same. Fixes #7679.

In this patch:

- A relative target resolves against the directory that holds the link, `..` included, and `SymbolicLink` keeps the string or Buffer target as written next to its absolute path.
- `readlink` returns the target as written. `realpath` still returns the absolute path.
- `cp` of a symlink writes the absolute target, as Node does without `verbatimSymlinks`, so a copy in another directory still points at the same file.
- The target is resolved once, when the link is created. Renaming a relative link, or a directory above it, to another directory keeps it pointing at the old location, where Node would resolve it again. The VFS directories don't know their own path, so resolving on every follow would need a larger change.

No compat flag: until now a relative link only worked when the target existed under the cwd, links can only be created in `/tmp`, which doesn't persist across requests, and earlier `node:fs` parity fixes (#7393, #7394) changed behaviour the same way. A Worker that used a relative target to reach a `/bundle` file now needs an absolute target, as it would on Node.
@littledivy
littledivy force-pushed the littledivy/2026-11-13-fs-symlink-relative branch from 22113b7 to a817931 Compare November 17, 2026 18:35

@clanky-the-clanker clanky-the-clanker Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✅ Approved — The changes since the last review keep relative symlink targets as written and resolve them from the link's directory, and no issues were found.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

Relative targets are normalized in kj::Path against the virtual filesystem root, so they cannot escape it. The target as written is only stored and returned by readlink; it is never used to resolve the link. A cp copy is built from the source's absolute target.


📖 AGENTS.md: ✅ LGTM

No AGENTS.md changes. The commit message records the decision to ship without a compatibility flag.


📋 Code Quality: ✅ LGTM

Every caller of newSymbolicLink and of the SymbolicLink constructor passes the new target argument or uses the kj::none default. readlink returns the target as written and realpath returns the absolute path. A relative target resolves against the link's URL, so .. works. A Buffer target is converted to a string, and a URL target falls back to the absolute path. The new tests cover the sync, callback and promises forms, cp, and chdir.


⚡ Performance: ✅ LGTM

The only added work is a little string handling when a symlink is created, read or copied, which is not a hot path.


📝 Documentation: ✅ LGTM

None of the comments changed since the last review need updating.


Reviewed commit: a8179315 — same changes as 22113b7d, so this is that commit's review (no new review was run; line numbers are as of 22113b7d)

@littledivy
littledivy merged commit 8e223fe into main Nov 18, 2026
36 of 38 checks passed
@littledivy
littledivy deleted the littledivy/2026-11-13-fs-symlink-relative branch November 18, 2026 03:55
littledivy added a commit that referenced this pull request Nov 18, 2026
`fs.readdirSync(dir, { recursive: true })` named each entry by its absolute path with the leading slash dropped, so reading `/tmp/d` returned `tmp/d/sub/b.txt` where Node.js returns `sub/b.txt`. With `withFileTypes`, and from a recursive `opendir`, `Dirent.name` carried the same path where Node.js gives the entry's own name. Code that joins each entry back onto `dir` got paths like `/tmp/d/tmp/d/sub/b.txt` and ENOENT. Fixes #7678.

In this patch the binding names recursive entries by their path relative to the directory being read, and Dirents take the last segment of it. `parentPath` was already right, and non-recursive reads are unchanged. This ships without a compatibility flag, as the node:fs fixes in #8463 and #7393 did: we accept the risk to a Worker that relies on the old names, since Node.js code written against these APIs expects the relative names. The callback and promises variants go through `readdirSync`, so they are fixed too. The existing fs-dir-test assertions that pinned the old names (`bundle/worker`, `dev/null`) now expect Node.js's (`worker`, `null`), and the new `readdirRecursiveNamesAreRelative` case, with expected values taken from Node.js, fails without the fix.
littledivy added a commit that referenced this pull request Nov 19, 2026
`fs.readdirSync(dir, { recursive: true })` named each entry by its absolute path with the leading slash dropped, so reading `/tmp/d` returned `tmp/d/sub/b.txt` where Node.js returns `sub/b.txt`. With `withFileTypes`, and from a recursive `opendir`, `Dirent.name` carried the same path where Node.js gives the entry's own name. Code that joins each entry back onto `dir` got paths like `/tmp/d/tmp/d/sub/b.txt` and ENOENT.

In this patch the binding names recursive entries by their path relative to the directory being read, and Dirents take the last segment of it. `parentPath` was already right, and non-recursive reads are unchanged. This ships without a compatibility flag, as the node:fs fixes in #8463 and #7393 did: we accept the risk to a Worker that relies on the old names, since Node.js code written against these APIs expects the relative names. The callback and promises variants go through `readdirSync`, so they are fixed too. The fs-dir-test assertions that pinned the old names (`bundle/worker`, `dev/null`) now expect Node.js's (`worker`, `null`).
littledivy added a commit that referenced this pull request Nov 21, 2026
`fs.readdirSync(dir, { recursive: true })` named each entry by its absolute path with the leading slash dropped, so reading `/tmp/d` returned `tmp/d/sub/b.txt` where Node.js returns `sub/b.txt`. With `withFileTypes`, and from a recursive `opendir`, `Dirent.name` carried the same path where Node.js gives the entry's own name. Code that joins each entry back onto `dir` got paths like `/tmp/d/tmp/d/sub/b.txt` and ENOENT.

In this patch the binding names recursive entries by their path relative to the directory being read, and Dirents take the last segment of it. `parentPath` was already right, and non-recursive reads are unchanged. This ships without a compatibility flag, as the node:fs fixes in #8463 and #7393 did: we accept the risk to a Worker that relies on the old names, since Node.js code written against these APIs expects the relative names. The callback and promises variants go through `readdirSync`, so they are fixed too. The fs-dir-test assertions that pinned the old names (`bundle/worker`, `dev/null`) now expect Node.js's (`worker`, `null`).
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.

node:fs: symlink stores a relative target resolved against process.cwd()

2 participants