Repository navigation
chore(build): fix empty napi-binding.d.ts on subsequent builds - #14027
Conversation
`rspack_binding_api/build.rs` did not call `napi_build::setup()`, so it never declared `cargo::rerun-if-env-changed=NAPI_FORCE_BUILD_RSPACK_BINDING_API`. Because `crates/node_binding/scripts/build.js` always passes `--no-dts-cache`, the napi CLI creates a fresh `NAPI_TYPE_DEF_TMP_FOLDER` on every run and bumps `NAPI_FORCE_BUILD_<crate>` with a new timestamp to force the `#[napi]` proc macros to re-emit type-def files. Without the rerun-if-env-changed directive, cargo treated `rspack_binding_api` as up-to-date on the second build, skipped recompilation, and emitted nothing into the new tmp folder. `generateTypeDef` then returned an empty dts, overwriting `napi-binding.d.ts` with 0 bytes. This regressed in #10770 (split `rspack_node` into `rspack_binding_api`), which dropped the `napi_build::setup()` call added in #9162. Re-add it via `rspack_binding_build::setup()` to match the pattern used by `node_binding/build.rs` and `rspack_binding_builder_testing/build.rs`.
There was a problem hiding this comment.
Pull request overview
Fixes a regression in the N-API type-definition generation pipeline where subsequent pnpm build:binding:dev runs could overwrite crates/node_binding/napi-binding.d.ts with an empty (0-byte) file. This restores the expected Cargo build-script behavior so #[napi] proc macros rerun when the napi CLI forces rebuilds under --no-dts-cache.
Changes:
- Call
rspack_binding_build::setup()fromcrates/rspack_binding_api/build.rsto register thererun-if-env-changedhooks used by napi’s forced rebuild mechanism. - Add
rspack_binding_buildas a build-dependency ofrspack_binding_api. - Update
Cargo.lockaccordingly.
Reviewed changes
Copilot reviewed 2 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| crates/rspack_binding_api/Cargo.toml | Adds rspack_binding_build under [build-dependencies] so the build script can call setup(). |
| crates/rspack_binding_api/build.rs | Invokes rspack_binding_build::setup() to ensure Cargo reruns when napi updates NAPI_FORCE_BUILD_RSPACK_BINDING_API. |
| Cargo.lock | Records the new build-dependency edge for rspack_binding_api. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
📦 Binary Size-limit
🙈 Size remains the same at 61.93MB |
Rsdoctor Bundle Diff AnalysisFound 6 projects in monorepo, 0 projects with changes. 📊 Quick Summary
Generated by Rsdoctor GitHub Action |
Merging this PR will not alter performance
Comparing Footnotes
|
Summary
Re-add
napi_build::setup()torspack_binding_api/build.rsso cargo correctly reruns the#[napi]proc macros when the napi CLI bumpsNAPI_FORCE_BUILD_RSPACK_BINDING_APIunder--no-dts-cache.Why
Running
pnpm build:binding:deva second time would overwritecrates/node_binding/napi-binding.d.tswith a 0-byte file.Root cause chain:
crates/node_binding/scripts/build.jsalways passes--no-dts-cachetonapi build.--no-dts-cache, the napi CLI creates a fresh timestampedtarget/napi-rs/rspack_node-<hash>_<ts>/directory for type-def output on every run, and setsNAPI_FORCE_BUILD_<CRATE>=<new-ts>for every workspace crate depending onnapi-derive, to force the proc macros to re-emit type-def files into the new directory.build.rsmust registercargo::rerun-if-env-changed=NAPI_FORCE_BUILD_<CRATE>— exactly whatnapi_build::setup()does.rspack_binding_api/build.rsdid not call it (onlycargo::rustc-check-cfg=cfg(tokio_unstable)), so cargo considered the crate up-to-date on the 2nd run, skipped recompilation, and emitted nothing into the new tmp folder. Sincenode_binding/src/lib.rsitself has zero#[napi]items and ~all of them live inrspack_binding_api, the folder ended up empty.generateTypeDefreturned{ exports: [], dts: '' }and the empty dts was written to disk; becauseexports.length === 0, it was also not pushed tooutputs, so the--pipe "node dts-header.js"step did not run to prepend the banner either.Evidence before the fix — the first run's tmp folder is populated, every subsequent run is empty:
After the fix, every run's folder contains
rspack_binding_api/.Regression history
--no-dts-cacheby default and addednapi_build::setup()torspack_binding_values/build.rsto pair with it.rspack_nodeinto separaterspack_binding_api#10770 splitrspack_nodeintorspack_binding_api, but the newrspack_binding_api/build.rsdid not carry over thenapi_build::setup()call. This PR restores the equivalent.Changes
crates/rspack_binding_api/build.rs: callrspack_binding_build::setup()(which callsnapi_build::setup()), matching the pattern innode_binding/build.rsandrspack_binding_builder_testing/build.rs.crates/rspack_binding_api/Cargo.toml: add[build-dependencies] rspack_binding_build = { workspace = true }.Test plan
cargo check -p rspack_binding_apisucceeds.rm -rf target/napi-rs && pnpm build:binding:dev && pnpm build:binding:devproduces a non-emptycrates/node_binding/napi-binding.d.tsafter the second run, and eachtarget/napi-rs/rspack_node-*folder containsrspack_binding_api/.