Skip to content
This repository was archived by the owner on Sep 1, 2026. It is now read-only.

Fix nondeterministic Realpath on darwin for hardlinked files - #4578

Closed
Edmund King (eddking) wants to merge 1 commit into
microsoft:mainfrom
eddking:fix/realpath-hardlink-determinism
Closed

Edmund King (eddking) wants to merge 1 commit into
microsoft:mainfrom
eddking:fix/realpath-hardlink-determinism

Conversation

@eddking

Copy link
Copy Markdown

Fixes #4262

Realpath on darwin resolves paths with open() + fcntl(F_GETPATH). F_GETPATH answers from the kernel vnode name cache, and a regular file with nlink > 1 has several equally valid names, if a concurrent open() of a hardlink sibling lands between our open() and the fcntl(), 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.ts files (export * from './lib';), which a content addressed store collapses to one inode with nlink=325. Under parallel file loading, resolution then intermittently returns a different submodule's path. From a failing --traceResolution run:

Resolving real path for '.../library/assertions/index.d.ts',
  result '.../library/shared/index.d.ts'

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, fstat the fd. Directories and nlink == 1 files 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 to filepath.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 own open() touches the cache. The test instead churns opens of the sibling from a goroutine while asserting 200 Realpath calls 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.

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

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 fstat and avoid F_GETPATH for nlink > 1 non-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.EvalSymlinks for 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.

@eddking

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

@jakebailey

Copy link
Copy Markdown
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 EvalSymlinks.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Non-deterministic TS2305 "has no exported member" errors across project references with --emitDeclarationOnly

3 participants