This repository was archived by the owner on Sep 1, 2026. It is now read-only.
Repository navigation
Fix nondeterministic Realpath on darwin for hardlinked files - #4578
Closed
Edmund King (eddking) wants to merge 1 commit into
Closed
Edmund King (eddking) wants to merge 1 commit into
Edmund King (eddking) wants to merge 1 commit into
Conversation
F_GETPATH answers from the vnode name cache, so for a file with nlink > 1 a concurrent open of a hardlink sibling can make it return the sibling's path. pnpm and Nix stores hardlink-dedupe identical files, so parallel file loading nondeterministically resolves modules to the wrong file. Resolve hardlinked files via their parent directory (directories cannot be hardlinked) and re-attach the final component. Fixes microsoft#4262
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a macOS (darwin) nondeterminism in nativepath.Realpath when resolving hardlinked files under concurrency by avoiding F_GETPATH on nlink > 1 regular files and instead canonicalizing the parent directory and re-attaching the final component (with bounded symlink handling).
Changes:
- Detect hardlinked targets via
fstatand avoidF_GETPATHfornlink > 1non-directories, reconstructing the path deterministically from the real parent directory. - Add bounded symlink resolution for the final component in the hardlink path reconstruction (with fallback to
filepath.EvalSymlinksfor unsplittable paths). - Add a darwin-only regression test that reproduces the prior flake via concurrent vnode-cache churn and verifies deterministic results.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| internal/nativepath/realpath_darwin.go | Adds hardlink-aware darwin Realpath implementation that avoids vnode name cache nondeterminism for nlink > 1 files. |
| internal/nativepath/realpath_darwin_test.go | Adds a concurrency-based darwin regression test covering hardlinks, symlinks-to-hardlinks, and fast-path cases. |
Author
|
@microsoft-github-policy-service agree |
Member
|
Honestly, I think we might just be better off deleting the darwin special case code entirely. When I measured it, it didn't seem like it even helped much. If you're able to test this, maybe you should also verify that it's no worse just to fall back to |
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
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
Fixes #4262
Realpathon darwin resolves paths withopen()+fcntl(F_GETPATH). F_GETPATH answers from the kernel vnode name cache, and a regular file withnlink > 1has several equally valid names, if a concurrentopen()of a hardlink sibling lands between ouropen()and thefcntl(), the kernel returns the sibling's path.Content addressed package stores (e.g. pnpm) hardlink deduplicate identical files. One of our dependencies, for example, ships hundreds of byte-identical
index.d.tsfiles (export * from './lib';), which a content addressed store collapses to one inode withnlink=325. Under parallel file loading, resolution then intermittently returns a different submodule's path. From a failing--traceResolutionrun:That produces TS2305 errors naming the wrong module, and occasionally drops whole import subtrees (This explains the varying
Files:counts in the issue).The fix: after opening,
fstatthe fd. Directories andnlink == 1files keep the O(1) F_GETPATH path. Hardlinked regular files resolve their parent directory via F_GETPATH instead (directories cannot be hardlinked, so that answer is unambiguous) and re-attach the final component, resolving it manually if it's a symlink (bounded at MAXSYMLINKS=40; unsplittable paths fall back tofilepath.EvalSymlinks).The regression test needs concurrency: the vnode's cached name follows the most recent lookup, so a sequential two-hardlink check will pass since
Realpath's ownopen()touches the cache. The test instead churns opens of the sibling from a goroutine while asserting 200Realpathcalls on the other link.Validation on the monorepo where we found this: a build of one affected package failed 17-18/20 runs unpatched, 0/140 patched across load conditions. Emitted output is byte-identical to stock tsc, and there's no measurable perf change (0.458s vs 0.460s on the repro build).
go test ./internal/nativepath/... ./internal/vfs/...pass with and without-race; the new test fails 10/10 on unpatched main.Disclosure per CONTRIBUTING.md: this was investigated and authored with substantial AI assistance, operated and reviewed by me. I chose this issue, I've read and understand the patch, and I'll respond to review feedback myself.