Skip to content

chore(build): fix empty napi-binding.d.ts on subsequent builds - #14027

Merged
stormslowly merged 1 commit into
mainfrom
chore/binding-api-rerun-env-dts
May 13, 2026
Merged

stormslowly merged 1 commit into
mainfrom
chore/binding-api-rerun-env-dts

Conversation

@stormslowly

Copy link
Copy Markdown
Contributor

Summary

Re-add napi_build::setup() to rspack_binding_api/build.rs so cargo correctly reruns the #[napi] proc macros when the napi CLI bumps NAPI_FORCE_BUILD_RSPACK_BINDING_API under --no-dts-cache.

Why

Running pnpm build:binding:dev a second time would overwrite crates/node_binding/napi-binding.d.ts with a 0-byte file.

Root cause chain:

  1. crates/node_binding/scripts/build.js always passes --no-dts-cache to napi build.
  2. With --no-dts-cache, the napi CLI creates a fresh timestamped target/napi-rs/rspack_node-<hash>_<ts>/ directory for type-def output on every run, and sets NAPI_FORCE_BUILD_<CRATE>=<new-ts> for every workspace crate depending on napi-derive, to force the proc macros to re-emit type-def files into the new directory.
  3. For cargo to honor that env-var change, the crate's build.rs must register cargo::rerun-if-env-changed=NAPI_FORCE_BUILD_<CRATE> — exactly what napi_build::setup() does.
  4. rspack_binding_api/build.rs did not call it (only cargo::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. Since node_binding/src/lib.rs itself has zero #[napi] items and ~all of them live in rspack_binding_api, the folder ended up empty.
  5. generateTypeDef returned { exports: [], dts: '' } and the empty dts was written to disk; because exports.length === 0, it was also not pushed to outputs, 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:

target/napi-rs/rspack_node-f9407922_1778608021042/rspack_binding_api/   ← 1st run, populated
target/napi-rs/rspack_node-f9407922_1778608354939/                      ← empty
target/napi-rs/rspack_node-f9407922_1778608401353/                      ← empty
target/napi-rs/rspack_node-f9407922_1778608420143/                      ← empty

After the fix, every run's folder contains rspack_binding_api/.

Regression history

Changes

  • crates/rspack_binding_api/build.rs: call rspack_binding_build::setup() (which calls napi_build::setup()), matching the pattern in node_binding/build.rs and rspack_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_api succeeds.
  • rm -rf target/napi-rs && pnpm build:binding:dev && pnpm build:binding:dev produces a non-empty crates/node_binding/napi-binding.d.ts after the second run, and each target/napi-rs/rspack_node-* folder contains rspack_binding_api/.

`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`.
Copilot AI review requested due to automatic review settings May 13, 2026 01:57

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

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() from crates/rspack_binding_api/build.rs to register the rerun-if-env-changed hooks used by napi’s forced rebuild mechanism.
  • Add rspack_binding_build as a build-dependency of rspack_binding_api.
  • Update Cargo.lock accordingly.

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.

@github-actions

Copy link
Copy Markdown
Contributor

📦 Binary Size-limit

Comparing 034aafd to chore: release version 2.0.3 (#14015) by harpsealjs

🙈 Size remains the same at 61.93MB

@github-actions

Copy link
Copy Markdown
Contributor

Rsdoctor Bundle Diff Analysis

Found 6 projects in monorepo, 0 projects with changes.

📊 Quick Summary
Project Total Size Change
popular-libs 1.7 MB 0
react-10k 5.7 MB 0
react-1k 826.3 KB 0
react-5k 2.7 MB 0
rome 1.6 MB 0
ui-components 4.8 MB 0

Generated by Rsdoctor GitHub Action

@codspeed

codspeed Bot commented May 13, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 34 untouched benchmarks
⏩ 25 skipped benchmarks1


Comparing chore/binding-api-rerun-env-dts (034aafd) with main (dfcccd6)

Open in CodSpeed

Footnotes

  1. 25 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@stormslowly
stormslowly requested a review from hardfist May 13, 2026 02:37
@stormslowly
stormslowly enabled auto-merge (squash) May 13, 2026 02:54
@stormslowly
stormslowly merged commit 400d3d7 into main May 13, 2026
58 of 60 checks passed
@stormslowly
stormslowly deleted the chore/binding-api-rerun-env-dts branch May 13, 2026 05:53
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.

3 participants

⌂ 换网址