Repository navigation
fix(trace): resolve optional deps by walking node_modules when exports omits package.json - #48
Conversation
…e.json` is not in exports Some platform-bindings packages — notably `@img/sharp-libvips-*@1.3+` — ship a restrictive `exports` map that exposes `./lib`, `./package`, and `./versions` but not `./package.json` (or `.`). The optionalDependencies fallback added in the auto-trace block at `trace.ts:142` was hardcoded to `resolveModulePath(depName + "/package.json", ...)`, which silently returns `undefined` against those exports, the `continue` fires, and the dylib package is dropped from the trace output. Downstream effect: any Nitro / TanStack Start production build that imports `sharp` 0.35+ fails at runtime with `ERR_DLOPEN_FAILED: ... @rpath/libvips-cpp.*.dylib`, because the prebuilt `@img/sharp-<platform>/lib/*.node` links `libvips-cpp` via `@rpath` and the sibling `@img/sharp-libvips-<platform>` package is no longer in the output tree. Reachability via the JS graph worked on sharp <=0.34 and stopped on 0.35, which is why this only surfaced now. Both `depName + "/package.json"` and `depName + "/package"` resolve to the same `package.json` file when either succeeds, so `readJSON(resolved)` and `dirname(resolved)` downstream are unchanged. The fallback only fires when the canonical path can't be resolved, so existing packages keep their current behaviour. Closes unjs#47.
|
Warning Review limit reached
Next review available in: 42 minutes Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. How can I continue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews. How do review limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window. Please refer docs for additional details. Review details⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: ⛔ Files ignored due to path filters (6)
📒 Files selected for processing (4)
📝 WalkthroughWalkthroughIn ChangesOptional dependency resolution fallback
Estimated code review effort🎯 2 (Simple) | ⏱️ ~5 minutes Possibly related issues
Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
/package when /package.json is not exportedReplace the hardcoded `<dep>/package` fallback with a `resolvePackageDir` helper that walks up `node_modules` and reads `package.json` from disk, bypassing the `exports` map entirely. This handles any package with a restrictive `exports` (e.g. `@img/sharp-libvips-*`, which omits both `.` and `./package.json`) instead of relying on a coincidental `./package` alias. Add a regression test with a libvips-style fixture: an optional dep with no JS import and an `exports` map omitting `.` and `./package.json`. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #48 +/- ##
==========================================
+ Coverage 73.74% 77.20% +3.45%
==========================================
Files 5 5
Lines 419 465 +46
Branches 128 141 +13
==========================================
+ Hits 309 359 +50
- Misses 76 77 +1
+ Partials 34 29 -5 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
Can this be backported to nitro v2? From what I understand this was shipped in https://github.com/unjs/nf3/releases/tag/v0.3.18 and nf3 is a extract from nitro: Line 19 in a81bf79 but it is not included in nitro v2 (https://github.com/nitrojs/nitro/blob/v2/package.json) while it is in nitro v3: Nitro v2 seems to still be maintained https://github.com/nitrojs/nitro/commits/v2/. |
Summary
Fixes #47.
The
optionalDependenciesauto-detection intraceNodeModules(src/trace.ts) locates platform-binding packages that aren't reachable through the JS graph by calling:This goes through the dependency's
exportsmap, so it silently returnsundefinedfor packages that ship a restrictiveexports— most notably@img/sharp-libvips-<platform>@1.3+:There is no
./package.jsonand no.entry, so the resolve returnsundefined, thecontinuefires, and the dylib package gets silently dropped from the trace output.Why this matters now
sharp <=0.34reached@img/sharp-libvips-*through its JS graph, so@vercel/nfttraced it directly and this path never had to fire.sharp 0.35+removed the JS edge — the only reference is the@rpathlink inside the prebuilt addon (@img/sharp-<platform>/lib/*.node→libvips-cpp.*.dylib).ERR_DLOPEN_FAILED: ... Library not loaded: @rpath/libvips-cpp.*.dylib.The fix
Rather than hardcoding a
depName + "/package"fallback (which only works because libvips happens to expose a./packagealias — a brittle convention assumption), this stops relying on theexportsmap to locate the package at all.A new
resolvePackageDir(name, from)helper walks upnode_modulesfrom the parent package, mirroring Node.js resolution (correct for nested, hoisted, and pnpm layouts), and readspackage.jsonstraight from disk:resolveModulePath("<dep>/package.json")stays as the primary path — unchanged behavior for every package that resolves today.This handles any restrictive-
exportspackage generically, including ones that expose neither./package.jsonnor./package.Test
Adds a regression test (
test/trace.test.ts) with a fixture (@fixture/native-libvips) that mirrors libvips exactly: declared only as anoptionalDependency(no JS import), shipping anexportsmap that omits both.and./package.json, plus a dummy native binary. The test asserts the manifest and the.nodebinary land in the trace output.Verified it fails without the fix (
ENOENT … native-libvips/package.json) and passes with it. Full suite (lint + tsgo + vitest) green.