-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
fix: auto-detect native binary distributor packages as externals #3286
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
+33
−0
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -402,6 +402,14 @@ function createExternalsCollector( | |
| return markExternal("binding.gyp exists"); | ||
| } | ||
|
|
||
| // Check if the package distributes platform-specific native binaries | ||
| // via optionalDependencies (common pattern for Rust/napi-rs packages). | ||
| // These packages use createRequire(import.meta.url).resolve() at runtime | ||
| // to locate the correct platform binary, which breaks when bundled. | ||
| if (hasPlatformSpecificOptionalDeps(packageJson)) { | ||
| return markExternal("has platform-specific optionalDependencies"); | ||
| } | ||
|
|
||
| // Cache the negative result | ||
| isExternalCache.set(packageRoot, false); | ||
|
|
||
|
|
@@ -655,3 +663,28 @@ async function findNearestPackageJson( | |
| cache.set(baseDir, packageJsonPath); | ||
| return packageJsonPath; | ||
| } | ||
|
|
||
| // Matches platform/arch identifiers commonly found in native binary package names | ||
| // e.g. @secure-exec/v8-darwin-arm64, @rollup/rollup-linux-x64-gnu, esbuild-windows-64 | ||
| const platformPattern = | ||
| /[-.](darwin|linux|win32|windows|freebsd|android|macos|sunos|openbsd|aix)/i; | ||
|
Comment on lines
+669
to
+670
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Expand delimiter matching in Line 670 only matches platform tokens preceded by 💡 Proposed fix-const platformPattern =
- /[-.](darwin|linux|win32|windows|freebsd|android|macos|sunos|openbsd|aix)/i;
+const platformPattern =
+ /(?:^|[-./_])(darwin|linux|win32|windows|freebsd|android|macos|sunos|openbsd|aix)(?:$|[-./_])/i;🤖 Prompt for AI Agents |
||
|
|
||
| function hasPlatformSpecificOptionalDeps(packageJson: Record<string, unknown>): boolean { | ||
| const optionalDeps = packageJson.optionalDependencies; | ||
|
|
||
| if (!optionalDeps || typeof optionalDeps !== "object") { | ||
| return false; | ||
| } | ||
|
|
||
| const depNames = Object.keys(optionalDeps); | ||
|
|
||
| if (depNames.length === 0) { | ||
| return false; | ||
| } | ||
|
|
||
| // If a significant portion of optionalDependencies match platform patterns, | ||
| // this is likely a native binary distributor package | ||
| const platformDeps = depNames.filter((name) => platformPattern.test(name)); | ||
|
|
||
| return platformDeps.length >= 2; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚩 Regex does not match @scope/platform-arch naming convention (e.g. @esbuild/*)
The
platformPatternregex atpackages/cli-v3/src/build/externals.ts:670uses[-.](darwin|linux|...)which requires a dot or hyphen before the platform name. This means scoped packages where the platform name immediately follows the/separator — like@esbuild/linux-x64,@esbuild/darwin-arm64— will NOT match. I verified this by testing the regex against both naming conventions:@rollup/rollup-linux-x64-gnu→ ✅ matches (-linux)@swc/core-darwin-arm64→ ✅ matches (-darwin)@esbuild/linux-x64→ ❌ no match (/linux)@esbuild/darwin-arm64→ ❌ no match (/darwin)This is likely acceptable because: (1) the PR specifically targets napi-rs/Rust packages which use the
name-platform-archconvention, (2) the@scope/platform-archconvention is primarily used by esbuild which is a build tool rarely needed at task runtime, and (3) adding/to the character class[-./]could increase false positive risk for scoped packages. However, if a user's task code depends on esbuild at runtime, it won't be auto-detected as external.Was this helpful? React with 👍 or 👎 to provide feedback.