Skip to content

fix(require): fallback to transpilation when tryNative fails#413

Merged
pi0 merged 6 commits into
unjs:mainfrom
kricsleo:fix/try-native-with-fallback
Feb 27, 2026
Merged

fix(require): fallback to transpilation when tryNative fails#413
pi0 merged 6 commits into
unjs:mainfrom
kricsleo:fix/try-native-with-fallback

Conversation

@kricsleo

@kricsleo kricsleo commented Oct 19, 2025

Copy link
Copy Markdown
Member

resolves #386

Summary by CodeRabbit

  • Bug Fixes

    • Improved error handling for native module imports with automatic fallback mechanism that retries failed imports
  • Tests

    • Updated test fixtures and added new test scenarios to verify native import functionality and fallback recovery behavior

@pi0

pi0 commented Oct 19, 2025

Copy link
Copy Markdown
Member

Can you please check the failing CI?

@kricsleo

Copy link
Copy Markdown
Member Author

My bad 🤣, now it works.

@coderabbitai

coderabbitai Bot commented Feb 27, 2026

Copy link
Copy Markdown

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 4deba16 and cdb3474.

⛔ Files ignored due to path filters (1)
  • test/__snapshots__/fixtures.test.ts.snap is excluded by !**/*.snap
📒 Files selected for processing (4)
  • src/require.ts
  • test/fixtures/native/foo.ts
  • test/fixtures/native/index.js
  • test/fixtures/native/index.mjs
💤 Files with no reviewable changes (1)
  • test/fixtures/native/index.js

📝 Walkthrough

Walkthrough

This PR fixes the tryNative option to properly fall back to transpilation when native imports fail. Previously, async native import errors were not caught, causing failures for unsupported file types. The fix adds error handling with fallback retry logic. Test fixtures added to verify the behavior.

Changes

Cohort / File(s) Summary
Error Handling in Native Imports
src/require.ts
Added .catch() handler to async native import chain. On error, logs debug message and recursively calls jitiRequire with tryNative: false to attempt transpilation fallback, recovering from native import failures.
Test Fixtures
test/fixtures/native/foo.ts, test/fixtures/native/index.mjs, test/fixtures/native/index.js
Added test fixture module foo.ts and updated test harness in index.mjs to exercise native loading with fallback behavior. Removed unused dynamic import from index.js.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Suggested reviewers

  • pi0

Poem

🐰 A hopping fix for imports gone wrong,
When native fails, we carry on,
Transpile as backup, swift and true,
No TypeScript file left in the blue! ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The PR title accurately describes the main change: adding a fallback to transpilation when native import fails, which is the core fix implemented in src/require.ts.
Linked Issues check ✅ Passed The PR implements the proposed fix from issue #386 by adding a .catch handler to the async nativeImport promise in src/require.ts that logs the error and recursively retries with tryNative disabled, enabling transpilation fallback.
Out of Scope Changes check ✅ Passed All changes are in scope: src/require.ts implements the core fix, test fixtures validate the fallback behavior when native import fails, and package.json updates support the changes.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@pi0 pi0 left a comment

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.

Thnx ❤️

@pi0 pi0 merged commit a3e705d into unjs:main Feb 27, 2026
8 checks passed
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.

tryNative doesn't fallback to transpilation on error

2 participants