close
Skip to content

Migrate napi-rs bindings from v2 to v3 - #95412

Merged
jimmyhmiller merged 24 commits into
canaryfrom
jimmym/napi-v3-migration
Aug 13, 2026
Merged

Migrate napi-rs bindings from v2 to v3#95412
jimmyhmiller merged 24 commits into
canaryfrom
jimmym/napi-v3-migration

Conversation

@jimmyhmiller

Copy link
Copy Markdown
Contributor

Draft PR for testing

@github-actions

github-actions Bot commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Tests Passed

Commit: 90d08aa

@github-actions

github-actions Bot commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Stats from current PR

✅ No significant changes detected

📊 All Metrics
📖 Metrics Glossary

Dev Server Metrics:

  • Listen = TCP port starts accepting connections
  • First Request = HTTP server returns successful response
  • Cold = Fresh build (no cache)
  • Warm = With cached build artifacts

Build Metrics:

  • Fresh = Clean build (no .next directory)
  • Cached = With existing .next directory

Change Thresholds:

  • Time: Changes < 50ms AND < 10%, OR < 2% are insignificant
  • Size: Changes < 1KB AND < 1% are insignificant
  • All other changes are flagged to catch regressions

⚡ Dev Server

Metric Canary PR Change Trend
Cold (Listen) 812ms 812ms ▂███▁
Cold (Ready in log) 787ms 778ms ▁██▇▁
Cold (First Request) 1.296s 1.283s ▁██▇▁
Warm (Listen) 813ms 810ms ▂██▇▂
Warm (Ready in log) 782ms 782ms ▁██▇▁
Warm (First Request) 1.276s 1.253s ▁██▇▁
📦 Dev Server (Webpack) (Legacy)

📦 Dev Server (Webpack)

Metric Canary PR Change Trend
Cold (Listen) 813ms 812ms ▁▂█▂▁
Cold (Ready in log) 786ms 781ms ▁▁█▁▁
Cold (First Request) 3.442s 3.384s ▁▁█▁▁
Warm (Listen) 813ms 812ms ▁▂█▂▂
Warm (Ready in log) 796ms 780ms ▁▁█▁▁
Warm (First Request) 3.446s 3.470s ▁▁█▂▁

⚡ Production Builds

Metric Canary PR Change Trend
Fresh Build 5.699s 5.626s ▁██▇▁
Cached Build 2.938s 2.936s ▁██▇▁
📦 Production Builds (Webpack) (Legacy)

📦 Production Builds (Webpack)

Metric Canary PR Change Trend
Fresh Build 24.359s 24.009s ▁▁█▁▁
Cached Build 24.665s 24.370s ▁▁█▁▁
node_modules Size 553 MB 553 MB █████
📦 Bundle Sizes

Bundle Sizes

⚡ Turbopack

Client

Main Bundles
Canary PR Change
0-qgm0nju2p_g.js gzip 65.6 kB N/A -
03lzpt71kg1qg.js gzip 47 kB N/A -
050icza-xjz0i.js gzip 5.73 kB N/A -
07jdby0ue616s.js gzip 450 B N/A -
09h-pq0amxnxn.js gzip 155 B N/A -
0bjdc8muo74n5.js gzip 8.71 kB N/A -
0cz1d0mv5g_q7.js gzip 39.4 kB 39.4 kB
0d96sdjnk6_q2.js gzip 160 B N/A -
0h65-ko13bi2v.js gzip 154 B N/A -
0l9a54rsx_muy.js gzip 169 B N/A -
0q36apx7b1q_e.js gzip 7.54 kB N/A -
0rci1f3or1a19.js gzip 13.3 kB N/A -
0s56j06e963lv.js gzip 157 B N/A -
0vh9avwq65kiy.js gzip 156 B N/A -
1_2x714--ii1i.js gzip 8.76 kB N/A -
1-3y752pkth5-.js gzip 10 kB N/A -
10l611mm67gac.js gzip 71.6 kB N/A -
1elt1qium-r2m.css gzip 115 B 115 B
1k6dd7su4cfei.js gzip 3.57 kB N/A -
1kpy40fd-pusb.js gzip 154 B N/A -
1tf1phijqlx9j.js gzip 220 B 220 B
1uzabyd1120a1.js gzip 8.71 kB N/A -
2_uglbwqpe26b.js gzip 156 B N/A -
2-ufv8lc-g7gg.js gzip 10.6 kB N/A -
21kmjy_10x14f.js gzip 8.81 kB N/A -
28dhc6t85q1_p.js gzip 8.78 kB N/A -
29vdii-hrinxo.js gzip 154 B N/A -
2f-ilvczue-tp.js gzip 9.46 kB N/A -
2f1u17u5c8iny.js gzip 8.79 kB N/A -
2ikltg_8iegxw.js gzip 10.3 kB N/A -
2vxi673cz1-t4.js gzip 8.79 kB N/A -
38-q43pzktqhs.js gzip 1.46 kB N/A -
3dpw0m5_58h1u.js gzip 160 B N/A -
3gq5gsxwg_yl5.js gzip 151 B N/A -
3mrwxzed0ylgv.js gzip 13.1 kB N/A -
3nbojhxiy1qv_.js gzip 13.7 kB N/A -
3p8f-2kolq65a.js gzip 157 B N/A -
3qmtac2p5up_2.js gzip 156 B N/A -
41u5s3oe2-erp.js gzip 2.29 kB N/A -
445s_9hf8o7ao.js gzip 8.76 kB N/A -
turbopack-0d..9ejy.js gzip 3.74 kB 3.74 kB
0-6bcj16ji2wf.js gzip N/A 10.6 kB -
00i5e2bmvcp3b.js gzip N/A 155 B -
07ryk0jced-sc.js gzip N/A 8.78 kB -
0jwho9fkrb_t3.js gzip N/A 2.29 kB -
0k32-8va-4evm.js gzip N/A 155 B -
0rd26mgng5fzz.js gzip N/A 153 B -
0roh390ijzxa5.js gzip N/A 8.75 kB -
0vex9w55ursqj.js gzip N/A 8.79 kB -
0xnnl2qx2x7bz.js gzip N/A 158 B -
17oe55cu76cd7.js gzip N/A 450 B -
17vqy5qzu8dz5.js gzip N/A 167 B -
1drww5xikb-c-.js gzip N/A 9.46 kB -
1g60xde_dv17t.js gzip N/A 8.79 kB -
1kp8dhziabfib.js gzip N/A 154 B -
1tk8-jw7z6hlb.js gzip N/A 149 B -
1uzv47btzam64.js gzip N/A 5.73 kB -
1vy7n7wxv_rh5.js gzip N/A 8.81 kB -
2_uqyv_-jqnh5.js gzip N/A 65.6 kB -
2-0i7pl900-ou.js gzip N/A 8.71 kB -
2-kcbngm7ik7y.js gzip N/A 8.75 kB -
26zt1nty--7dt.js gzip N/A 155 B -
29p5-xa4jmdhg.js gzip N/A 13.7 kB -
2hzkpmnvy0nyg.js gzip N/A 71.6 kB -
2lpk5_hknut8q.js gzip N/A 13.1 kB -
2o4cibp3awtbw.js gzip N/A 3.56 kB -
2rvrvr5m-ct0q.js gzip N/A 155 B -
2ugc69z0t0ypz.js gzip N/A 1.46 kB -
3-_0ow7671xjh.js gzip N/A 7.55 kB -
36dn8i-_3dnq5.js gzip N/A 10 kB -
3bvjff6jbedao.js gzip N/A 155 B -
3fljpmwcjxqhx.js gzip N/A 10.3 kB -
3fula_ahie2iv.js gzip N/A 160 B -
3gz44skqdlsrb.js gzip N/A 8.71 kB -
3he_ygd-gghew.js gzip N/A 153 B -
3nk46g6u37ekv.js gzip N/A 47 kB -
3t-wn5cs8eke0.js gzip N/A 155 B -
3y9tus7kb5su0.js gzip N/A 13.3 kB -
Total 401 kB 401 kB ✅ -4 B

Server

Middleware
Canary PR Change
middleware-b..fest.js gzip 1.05 kB 1.06 kB
Total 1.05 kB 1.06 kB ⚠️ +2 B
Build Details
Build Manifests
Canary PR Change
_buildManifest.js gzip 876 B 874 B
Total 876 B 874 B ✅ -2 B
Build Cache
Canary PR Change
00000001.sst gzip 14.3 MB 14.7 MB 🔴 +459 kB (+3%)
00000002.sst gzip 13.2 MB 14.1 MB 🔴 +977 kB (+7%)
00000003.sst gzip 12.2 MB 11.2 MB 🟢 949 kB (-8%)
00000004.sst gzip 15.5 MB 15 MB 🟢 435 kB (-3%)
00000005.sst gzip 2.8 MB 10.2 MB 🔴 +7.41 MB (+265%)
00000006.sst gzip 10.2 MB 2.8 MB 🟢 7.4 MB (-73%)
00000007.sst gzip 59 B 59 B
00000008.meta gzip 89 B 89 B
00000009.meta gzip 298 kB 298 kB
00000010.meta gzip 298 kB 298 kB
00000011.meta gzip 298 kB 298 kB
00000012.sst gzip 51.9 kB 51.1 kB 🟢 835 B (-2%)
00000013.sst gzip 1.79 MB 1.79 MB 🔴 +7.97 kB (+0%)
00000014.sst gzip 59 B 59 B
00000015.meta gzip 116 B 116 B
00000016.meta gzip 326 kB 326 kB
00000017.meta gzip 405 kB 405 kB
00000018.sst gzip 52.6 kB 51.7 kB 🟢 816 B (-2%)
00000019.sst gzip 1.38 MB 1.38 MB
00000020.sst gzip 59 B 59 B
00000021.meta gzip 116 B 116 B
00000022.meta gzip 326 kB 326 kB
00000023.meta gzip 372 kB 372 kB
00000024.sst gzip 52.6 kB 51.8 kB 🟢 832 B (-2%)
00000025.sst gzip 1.38 MB 1.38 MB 🔴 +2.79 kB (+0%)
00000026.sst gzip 59 B 59 B
00000027.meta gzip 116 B 116 B
00000028.meta gzip 326 kB 326 kB
00000029.meta gzip 372 kB 372 kB
00000030.sst gzip 52.1 kB 51.3 kB 🟢 726 B (-1%)
00000031.sst gzip 1.38 MB 1.38 MB 🔴 +2.8 kB (+0%)
00000032.sst gzip 59 B 59 B
00000033.meta gzip 116 B 116 B
00000034.meta gzip 326 kB 326 kB
00000035.meta gzip 372 kB 372 kB
00000036.sst gzip 52.4 kB 51.6 kB 🟢 754 B (-1%)
00000037.sst gzip 1.38 MB 1.38 MB 🔴 +2.83 kB (+0%)
00000038.sst gzip 59 B 59 B
00000039.meta gzip 116 B 116 B
00000040.meta gzip 326 kB 326 kB
00000041.meta gzip 372 kB 372 kB
CURRENT gzip 91 B 94 B 🔴 +3 B (+3%)
LOG gzip 673 B 673 B
Total 80.1 MB 80.1 MB ⚠️ +75.5 kB

📦 Webpack

Client

Main Bundles
Canary PR Change
3322-HASH.js gzip 66.2 kB N/A -
4191.HASH.js gzip 169 B N/A -
7920-HASH.js gzip 4.67 kB N/A -
9784-HASH.js gzip 5.63 kB N/A -
b1ad9f4c-HASH.js gzip 63.2 kB N/A -
framework-HASH.js gzip 59.7 kB 59.7 kB
main-app-HASH.js gzip 252 B 253 B
main-HASH.js gzip 40.1 kB 40.1 kB
webpack-HASH.js gzip 1.68 kB 1.68 kB
3577.HASH.js gzip N/A 168 B -
578-HASH.js gzip N/A 66.8 kB -
8590-HASH.js gzip N/A 5.61 kB -
9750-HASH.js gzip N/A 4.68 kB -
a8984546-HASH.js gzip N/A 63.2 kB -
Total 242 kB 242 kB ⚠️ +620 B
Polyfills
Canary PR Change
polyfills-HASH.js gzip 39.4 kB 39.4 kB
Total 39.4 kB 39.4 kB
Pages
Canary PR Change
_app-HASH.js gzip 194 B 193 B
_error-HASH.js gzip 181 B 182 B
css-HASH.js gzip 334 B 331 B
dynamic-HASH.js gzip 1.81 kB 1.81 kB
edge-ssr-HASH.js gzip 255 B 253 B
head-HASH.js gzip 349 B 351 B
hooks-HASH.js gzip 382 B 384 B
image-HASH.js gzip 581 B 582 B
index-HASH.js gzip 260 B 259 B
link-HASH.js gzip 2.48 kB 2.48 kB
routerDirect..HASH.js gzip 317 B 318 B
script-HASH.js gzip 384 B 386 B
withRouter-HASH.js gzip 316 B 315 B
1afbb74e6ecf..834.css gzip 106 B 106 B
Total 7.95 kB 7.96 kB ⚠️ +7 B

Server

Edge SSR
Canary PR Change
edge-ssr.js gzip 129 kB 129 kB
page.js gzip 294 kB 294 kB
Total 423 kB 423 kB
Middleware
Canary PR Change
middleware-b..fest.js gzip 618 B 616 B
middleware-r..fest.js gzip 156 B 156 B
middleware.js gzip 45.9 kB 45.4 kB 🟢 534 B (-1%)
edge-runtime..pack.js gzip 842 B 842 B
Total 47.5 kB 47 kB ✅ -536 B
Build Details
Build Manifests
Canary PR Change
_buildManifest.js gzip 717 B 719 B
Total 717 B 719 B ⚠️ +2 B
Build Cache
Canary PR Change
0.pack gzip 4.81 MB 4.8 MB 🟢 7.61 kB (0%)
index.pack gzip 121 kB 122 kB
index.pack.old gzip 122 kB 122 kB
Total 5.05 MB 5.05 MB ✅ -6.42 kB

🔄 Shared (bundler-independent)

Runtimes
Canary PR Change
app-page-exp...dev.js gzip 375 kB 375 kB
app-page-exp..prod.js gzip 207 kB 207 kB
app-page-tur...dev.js gzip 374 kB 374 kB
app-page-tur..prod.js gzip 207 kB 207 kB
app-page-tur...dev.js gzip 371 kB 371 kB
app-page-tur..prod.js gzip 204 kB 204 kB
app-page.run...dev.js gzip 371 kB 371 kB
app-page.run..prod.js gzip 205 kB 205 kB
app-route-ex...dev.js gzip 82.9 kB 82.9 kB
app-route-ex..prod.js gzip 56.1 kB 56.1 kB
app-route-tu...dev.js gzip 83 kB 83 kB
app-route-tu..prod.js gzip 56.1 kB 56.1 kB
app-route-tu...dev.js gzip 82.5 kB 82.5 kB
app-route-tu..prod.js gzip 55.9 kB 55.9 kB
app-route.ru...dev.js gzip 82.5 kB 82.5 kB
app-route.ru..prod.js gzip 55.8 kB 55.8 kB
dev-validati...dev.js gzip 134 kB 134 kB
dev-validati...dev.js gzip 134 kB 134 kB
dev-validati...dev.js gzip 131 kB 131 kB
dev-validati...dev.js gzip 131 kB 131 kB
dist_client_...dev.js gzip 324 B 324 B
dist_client_...dev.js gzip 326 B 326 B
dist_client_...dev.js gzip 318 B 318 B
dist_client_...dev.js gzip 317 B 317 B
pages-api-tu...dev.js gzip 46.4 kB 46.4 kB
pages-api-tu..prod.js gzip 34.6 kB 34.6 kB
pages-api.ru...dev.js gzip 46.4 kB 46.4 kB
pages-api.ru..prod.js gzip 34.5 kB 34.5 kB
pages-turbo....dev.js gzip 55.2 kB 55.2 kB
pages-turbo...prod.js gzip 40.1 kB 40.1 kB
pages.runtim...dev.js gzip 55.1 kB 55.1 kB
pages.runtim..prod.js gzip 40.1 kB 40.1 kB
server.runti..prod.js gzip 67.1 kB 67.1 kB
use-cache-pr...dev.js gzip 72.6 kB 72.6 kB
use-cache-pr...dev.js gzip 72.6 kB 72.6 kB
use-cache-pr...dev.js gzip 70.8 kB 70.8 kB
use-cache-pr...dev.js gzip 70.8 kB 70.8 kB
Total 4.11 MB 4.11 MB ⚠️ +5 B
📝 Changed Files (2 files)

Files with changes:

  • pages-api.ru..time.prod.js
  • pages.runtime.prod.js
View diffs
pages-api.ru..time.prod.js

Diff too large to display

pages.runtime.prod.js

Diff too large to display

📎 Tarball URL
https://vercel-packages.vercel.app/next/commits/90d08aa87821ec9f51cb51586c122594cac69eae/next

Commit: 90d08aa

@jimmyhmiller
jimmyhmiller force-pushed the jimmym/napi-v3-migration branch 2 times, most recently from 5b37996 to 9453258 Compare July 10, 2026 15:49
@jimmyhmiller
jimmyhmiller marked this pull request as ready for review July 10, 2026 16:45
@jimmyhmiller
jimmyhmiller requested a review from a team July 10, 2026 16:45
Comment on lines +105 to +109
# Newest @napi-rs/cli that runs on Node.js 20.9.0 (NODE_LTS_VERSION below):
# 3.5.0+ depends on @inquirer/prompts 8, whose @inquirer/core 11 requires
# `util.styleText` (Node >= 20.12) and crashes at CLI startup. The CLI here
# only orchestrates cargo and names artifacts (types are committed), so
# drifting from the workspace devDependency (3.7.2) is acceptable.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Asking here: https://vercel.slack.com/archives/C04KC8A53T7/p1783709048113789

If we have to do this, let's make everything use version 3.4.1 of the CLI? I don't like the idea of mixing these versions between jobs if there's not a really good reason we can't just pin on 3.4.1?

Comment thread crates/next-napi-bindings/src/css/mod.rs Outdated
Comment thread packages/next/src/build/swc/generated-native.d.ts Outdated
Comment thread crates/next-napi-bindings/src/next_api/endpoint.rs Outdated
Comment thread crates/next-napi-bindings/src/next_api/endpoint.rs Outdated
Comment thread crates/next-napi-bindings/src/next_api/endpoint.rs Outdated
Comment thread crates/next-napi-bindings/src/next_api/utils.rs Outdated
Comment thread crates/next-napi-bindings/src/next_api/project.rs Outdated
Comment thread crates/next-napi-bindings/src/next_api/project.rs Outdated
Comment thread crates/next-napi-bindings/src/next_api/project.rs Outdated
Comment thread crates/next-napi-bindings/src/next_api/endpoint.rs Outdated
Comment thread crates/next-napi-bindings/src/next_api/utils.rs
Comment thread crates/next-napi-bindings/src/next_api/project.rs Outdated
Comment thread crates/next-napi-bindings/src/next_api/project.rs
Comment thread crates/next-napi-bindings/src/next_api/project.rs Outdated
Comment thread crates/next-napi-bindings/src/next_api/project.rs
Comment thread crates/next-napi-bindings/src/next_api/project.rs Outdated
Comment thread crates/next-napi-bindings/src/transform.rs Outdated
jimmyhmiller added a commit that referenced this pull request Jul 27, 2026
…Ref types (#95875)

## Summary

Follow-up to two review comments on #95412:

- **Type the subscribe callbacks.** The seven `*Subscribe` / `*Changed`
bindings declared their callback as `(...args: any[]) => any`. They now
use error-first typed signatures, e.g. `(err: Error, value:
TurbopackResult<UpdateMessage>) => void`. (bgw: _"clean up these
typescript `any` types wherever we reasonably can"_.)
- **Tighten the Rust `FunctionRef` value types.**
`project_update_info_subscribe` and
`project_compilation_events_subscribe` still used
`FunctionRef<Unknown<'static>>`; they now use their concrete mapper
output (`NapiUpdateMessage` / `NapiCompilationEvent`).
`project_hmr_events` intentionally keeps `Unknown` since it emits a
dynamic `to_js_value` client/server union. (bgw: _"get rid of `Unknown`
here ... that'll involve updating callsites too"_.)

`generated-native.d.ts` was regenerated from these Rust changes via
`pnpm swc-build-native`. That regeneration also refreshes a handful of
lines that were stale in the committed copy (a few `Promise<undefined>`
→ `Promise<void>` and one doc comment) — these reflect what the repo's
pinned CLI emits today, not hand edits.

## Verification

- `pnpm --filter=next types`

<!-- NEXT_JS_LLM_PR -->
Comment thread crates/next-napi-bindings/src/next_api/project.rs Outdated
Comment thread crates/next-napi-bindings/src/next_api/turbopack_ctx.rs Outdated
Comment thread crates/next-napi-bindings/src/next_api/turbopack_ctx.rs Outdated
Comment thread crates/next-napi-bindings/src/next_api/turbopack_ctx.rs Outdated
Comment thread crates/next-napi-bindings/src/next_api/turbopack_ctx.rs Outdated
Comment thread crates/next-napi-bindings/src/lib.rs
Comment thread packages/next/src/build/swc/types.ts Outdated
Comment thread turbopack/crates/turbopack-node/src/worker_pool/worker_thread.rs Outdated
Comment thread turbopack/crates/turbopack-node/src/worker_pool/worker_thread.rs Outdated
Comment thread package.json Outdated
@bgw

bgw commented Aug 11, 2026

Copy link
Copy Markdown
Member

Maybe hold off on merging this until we figure out if 16.3.x is going to get cut from canary or from the next-16-3 release branch. https://vercel.slack.com/archives/C02CDC2ALJH/p1786483656374079?thread_ts=1786372888.906399&cid=C02CDC2ALJH

jimmyhmiller added a commit that referenced this pull request Aug 12, 2026
…Ref types

Addresses two follow-up review comments from #95412:

- Replace the `(...args: any[]) => any` `ts_arg_type` on the seven
  subscribe/changed callbacks with error-first typed signatures
  (`(err: Error, value: TurbopackResult<...>) => void`).
- Tighten the Rust `FunctionRef` value types that were still `Unknown`
  to the concrete mapper output (`NapiUpdateMessage`,
  `NapiCompilationEvent`). `project_hmr_events` keeps `Unknown` because
  it emits a dynamic `to_js_value` union.

Regenerating generated-native.d.ts with the repo's pinned CLI also
refreshes a few lines that were stale in the committed copy.
@jimmyhmiller
jimmyhmiller force-pushed the jimmym/napi-v3-migration branch from f814bb9 to 3b307ed Compare August 12, 2026 13:52
Bump the workspace to napi 3.10.0 / napi-derive 3.5.8 / @napi-rs/cli 3.7.2
and port all first-party bindings to the modern (compat-mode-free) API:
ThreadsafeFunction builder, Object/Function/Unknown value types,
FunctionRef::borrow_back, External/ExternalRef, and #[napi(object)] outputs.

Drop the napi-2-pinned lightningcss-napi crate and vendor it in-repo as
turbopack-lightningcss-napi, rewritten to the modern napi 3 API. This also
fixes the visitor bridge, which silently no-op'd on v3 because the compat
JsFunction::try_from(Unknown) path failed (breaking CSS url()/@import
rewriting and CSS-module handling).

Update the @napi-rs/cli v3 build config (binaryName/targets,
--manifest-path/--no-js/-o) and align @emnapi/* to 1.11.1 for the wasi build.
The v2 migration changed the build-native* scripts to v3 CLI flags
(--manifest-path/--no-js/-o), but CI still installed @napi-rs/cli 2.18.4,
which rejects --manifest-path. Bump the pinned CLI to 3.7.2 in the native
builder Dockerfile and the NAPI_CLI_VERSION env across the build/release
workflows so it matches the workspace devDependency.
napi v3's External<T> is !Send, so the async #[napi] fns that took an
External needed an unsafe Send/Sync wrapper (SendableExternalRef) to satisfy
spawn_future's Send-future bound. Instead, make those functions synchronous:
take &External, extract the Send data (turbopack ctx / container / endpoint
op) up front, and return env.spawn_future(async move { ... }) as PromiseRaw.
The !Send reference stays confined to the sync body and never crosses into
the future, so the unsafe wrapper is deleted. JS behavior is unchanged
(still returns a Promise).
Correctness/robustness:
- Drop return_if_invalid from project_new so invalid JS arguments throw a
  TypeError again instead of silently resolving to undefined.
- Take the project exit receiver inside the spawned future (after
  stop_and_wait in project_shutdown), restoring the v2 ordering: a panic in
  stop_and_wait no longer consumes the receiver, so exit handlers survive
  for the retry/fallback onExit call. Share the take+run logic between
  project_on_exit and project_shutdown via an Arc<std::sync::Mutex<..>>.
- Restore lockfile_unlock's async offload: release the lock on the blocking
  pool via env.spawn_future + spawn_blocking instead of the JS thread.

Latent hazards:
- Replace the Object::from_raw 'static lifetime detach in
  project_compilation_events_subscribe with a NapiCompilationEvent object
  struct, matching the NapiUpdateMessage pattern.
- Tie the vendored lightningcss crate's 'env lifetimes to the &Env argument
  so returned Unknowns are actually scope-bound.
- Replace deprecated ThreadsafeFunction::unref with the Weak const generic
  (same napi_unref_threadsafe_function call, no deprecation warnings).

Cleanup:
- Re-add the opt-level="s" release override for the vendored
  turbopack-lightningcss-napi crate (lost with the lightningcss-napi dep).
- Restore &'static str fields on output-only object structs (NapiRoute,
  NapiUpdateMessage, NapiSourceDiagnostic), dropping per-message allocs.
- Add a provenance README to the vendored crate (upstream version, local
  modifications, drop criteria).

Types:
- Regenerate generated-native.d.ts with the v3 CLI. Add ts_arg_type/
  ts_return_type overrides where v3 typegen is weaker than the runtime
  contract (subscribe callbacks, worker scheduler tsfns, Option returns,
  entrypoints results), drop the manual header's stale lightningCss
  declarations (typegen emits them now), and correct the css.lightning
  facade types to reflect that the native transforms are synchronous.
@napi-rs/cli 3.x (via @inquirer/core) imports util.styleText, which was
added in Node.js 20.12.0. The build_reusable jobs ran Node 20.9.0, so every
build-native job crashed at CLI startup with 'does not provide an export
named styleText'. All other workflows already float on Node 20/22.
Reverts the NODE_LTS_VERSION bump. @napi-rs/cli 3.5.0+ depends on
@inquirer/prompts 8, whose @inquirer/core 11 imports util.styleText
(Node >= 20.12) and crashes at CLI startup on the pinned Node 20.9.0.
3.4.1 is the newest CLI on the @inquirer/prompts 7 line (engines >=18);
verified locally under Node 20.9.0: 3.7.2 reproduces the exact CI crash,
3.4.1 builds the addon and generates types successfully.

Only build_reusable.yml runs the CLI under NODE_LTS_VERSION; the other
workflows and the native-builder image resolve Node 20 latest and stay
on 3.7.2, matching the workspace devDependency used for typegen.
… JS thread

napi v2 permanently entered its tokio runtime context on the addon's main
thread (a leaked EnterGuard at module init). Both next-napi-bindings and
turbo-tasks (e.g. PriorityRunner::spawn) call tokio::spawn from synchronous
N-API entrypoints and relied on that ambient context. napi v3 removed the
permanent enter, so any 'next dev' aborted with 'there is no reactor
running' as soon as a subscription or turbo-tasks work was scheduled from
the JS thread (first surfaced in projectCompilationEventsSubscribe, then in
turbo-tasks' priority_runner).

Restore the v2 behavior in module_init: capture the custom runtime's handle
via within_runtime_if_available (which also forces napi to adopt the custom
runtime) and leak an EnterGuard for the lifetime of the thread. Also wrap
the two bare tokio::spawn calls in the subscribe entrypoints in
within_runtime_if_available for defense in depth.

Verified locally: 'next dev' on an app-dir fixture serves HTTP 200 with no
panic; previously it aborted at startup.
Use the re-exported napi::Unknown instead of spelling out the full
napi::bindgen_prelude::Unknown path.
There are many napi types with both Js-prefixed and unprefixed variants,
so aliasing Unknown to JsUnknown was confusing. Use the plain name.
Drop the JsUnknown alias and thread the concrete napi value type V
through subscribe() and its FunctionRef, instead of erasing the
callback argument to Unknown.
External<T> is Send, so these don't need env.spawn_future or an unsafe
SendableExternalRef wrapper. endpoint_write_to_disk goes back to a plain
async fn taking &External, the two subscribe entrypoints take &External
instead of ExternalRef, FunctionRef is imported, JsUnknown is dropped,
and the subscribe callbacks get concrete value types.
Same as the endpoint change: replace the env.spawn_future wrappers with
plain async fns taking &External<ProjectInstance>. Also use the already
imported PromiseRaw, drop the JsUnknown alias and the migration
meta-comments, and give the subscribe callbacks concrete value types.
- Drop leftover clones in endpoint_write_to_disk, project_update,
  project_on_exit/project_shutdown, and the exit_receiver field (no
  longer needs Arc now nothing spawns a 'static future off it).
- Revert trivially-inferred Ok::<_, napi::Error>(()) annotations.
- Inline project_write_all_entrypoints_to_disk_inner, which had only
  one caller.
- Move TransformOutputResult (and its only builder, complete_output)
  into transform.rs where they're used; re-export from lib.rs.
- Fix rspack.rs FinderTask::resolve to name its unused env param
  instead of a dead `let _ = env;` statement.
- Generalize the take_lockfile_inner poison message; it's reached from
  both lockfile_unlock_sync and lockfile_unlock now.
- Import Mutex directly in project.rs instead of writing
  std::sync::Mutex three times (no tokio::sync::Mutex left to
  disambiguate against).
- Drop redundant type annotations/turbofish in turbopack_ctx.rs and
  utils.rs that are already fully inferred.
- Match the rest of turbopack_ctx.rs by using the imported env::var
  instead of std::env::var.
- Trim a doc comment referencing "the previous manual object
  construction," which no longer exists in the codebase.
- Drop the two stale #[allow(dead_code)] on the pub-exported
  lightningCssTransform/lightningCssTransformStyleAttribute wrappers;
  keep it on the private mask fn, which really does need it (verified
  with clippy --all-targets: it's only reachable via napi macro
  registration, invisible to the test-build target).
- e.to_string() instead of format!("{}", e) in the same file.
- Match Ok(()) instead of Ok(_) in throw_turbopack_internal_error,
  since call_async's Return generic here is unit.
- Sink env_wrapper into the branch that actually uses it in the
  TurbopackResult ToNapiValue impl.
- Fix a double space in a comment.
…Ref types

Addresses two follow-up review comments from #95412:

- Replace the `(...args: any[]) => any` `ts_arg_type` on the seven
  subscribe/changed callbacks with error-first typed signatures
  (`(err: Error, value: TurbopackResult<...>) => void`).
- Tighten the Rust `FunctionRef` value types that were still `Unknown`
  to the concrete mapper output (`NapiUpdateMessage`,
  `NapiCompilationEvent`). `project_hmr_events` keeps `Unknown` because
  it emits a dynamic `to_js_value` union.

Regenerating generated-native.d.ts with the repo's pinned CLI also
refreshes a few lines that were stale in the committed copy.
- NapiCompilationEvent::type_name is &'static str instead of an
  allocated String; CompilationEvent::type_name already returns
  &'static str and napi handles it fine.
- Use expect_err instead of a match for the internal-error callback,
  which must always throw.
- Annotate the ThreadsafeFunction const generics in turbopack_ctx.rs
  with /* CalleeHandled */ and /* Weak */ comments, and let inference
  handle the two local bindings whose builder chains already pin those
  values.
- Drop the napi v2 -> v3 narration from worker_thread.rs.
- lightningcss transform/transformStyleAttr have always been
  synchronous; drop the stale await in the minify plugin and the
  comment excusing it.
- Note that leaking the tokio runtime guard keeps the runtime alive for
  the process lifetime, to be fixed in a follow-up.
- Remove the @emnapi/* pnpm overrides. Nothing in our napi build reads
  them; they only repinned @rspack/binding-wasm32-wasi's emnapi deps.
This plugin has never been instantiated. It arrived in #61327, which
forked fz6m/lightningcss-loader; that package ships both a loader and a
minifier plugin, and Next only ever wired up the loader
(webpack/config/blocks/css/loaders/{global,modules}.ts). CSS
minification goes through CssMinimizerPlugin for webpack and rspack's
own LightningCssMinimizerRspackPlugin, neither of which touches this
file. No commit in the repo's history constructs it, and the only
references were its own definition plus the re-export in index.ts.

Also drops ECacheKey.minify, which only the plugin used.

Note this removes the class from the compiled
next/dist/build/webpack/loaders/lightningcss-loader/src/index.js. That
is a build-internals deep import, never exported from the package root
or documented, so it is not a supported surface.
@jimmyhmiller
jimmyhmiller force-pushed the jimmym/napi-v3-migration branch from d1458bd to 90d08aa Compare August 12, 2026 17:11
@jimmyhmiller
jimmyhmiller merged commit e928f23 into canary Aug 13, 2026
274 of 282 checks passed
@jimmyhmiller
jimmyhmiller deleted the jimmym/napi-v3-migration branch August 13, 2026 15:35
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