Skip to content

Commit

Permalink
fix(next-swc): exclude raw target triples without native bindings (#5…
Browse files Browse the repository at this point in the history
…3230)

<!-- Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:

## For Contributors

### Improving Documentation

- Run `pnpm prettier-fix` to fix formatting issues before opening the
PR.
- Read the Docs Contribution Guide to ensure your contribution follows
the docs guidelines:
https://nextjs.org/docs/community/contribution-guide

### Adding or Updating Examples

- The "examples guidelines" are followed from our contributing doc
https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
- Make sure the linting passes by running `pnpm build && pnpm lint`. See
https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See:
https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

- Implements an existing feature request or RFC. Make sure the feature
request has been accepted for implementation before opening a PR. (A
discussion must be opened, see
https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added
(https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md


## For Maintainers

- Minimal description (aim for explaining to someone not on the team to
understand the PR)
- When linking to a Slack thread, you might want to share details of the
conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic
behind a change



### Why?

### How?

Closes NEXT-
Fixes #

-->

### What?
context: https://vercel.slack.com/archives/C04KC8A53T7/p1690403434786639

We've been using raw target triples from napi-rs directly. It includes
all of the platforms napi-rs lists, however next-swc have narrower
support for native bindings. PR omits target triples that doesn't have
corresponding next-swc binary, then try to pick up triples from there.
Also leaves small logs if triples are missing from the list for further
debugging.

Co-authored-by: JJ Kasper <jj@jjsweb.site>
  • Loading branch information
kwonoj and ijjk committed Jul 26, 2023
1 parent e4573a0 commit 0d4ec10
Showing 1 changed file with 64 additions and 3 deletions.
67 changes: 64 additions & 3 deletions packages/next/src/build/swc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ const nextVersion = process.env.__NEXT_VERSION as string

const ArchName = arch()
const PlatformName = platform()
const triples = platformArchTriples[PlatformName]?.[ArchName] || []

const infoLog = (...args: any[]) => {
if (process.env.NEXT_PRIVATE_BUILD_WORKER) {
Expand All @@ -25,6 +24,67 @@ const infoLog = (...args: any[]) => {
Log.info(...args)
}

/**
* Based on napi-rs's target triples, returns triples that have corresponding next-swc binaries.
*/
const getSupportedArchTriples: () => Record<string, any> = () => {
const { darwin, win32, linux, freebsd, android } = platformArchTriples

return {
darwin,
win32: {
arm64: win32.arm64,
ia32: win32.ia32.filter(
(triple: { abi: string }) => triple.abi === 'msvc'
),
x64: win32.x64.filter((triple: { abi: string }) => triple.abi === 'msvc'),
},
linux: {
// linux[x64] includes `gnux32` abi, with x64 arch.
x64: linux.x64.filter(
(triple: { abi: string }) => triple.abi !== 'gnux32'
),
arm64: linux.arm64,
// This target is being deprecated, however we keep it in `knownDefaultWasmFallbackTriples` for now
arm: linux.arm,
},
// Below targets are being deprecated, however we keep it in `knownDefaultWasmFallbackTriples` for now
freebsd: {
x64: freebsd.x64,
},
android: {
arm64: android.arm64,
arm: android.arm,
},
}
}

const triples = (() => {
const supportedArchTriples = getSupportedArchTriples()
const targetTriple = supportedArchTriples[PlatformName]?.[ArchName]

// If we have supported triple, return it right away
if (targetTriple) {
return targetTriple
}

// If there isn't corresponding target triple in `supportedArchTriples`, check if it's excluded from original raw triples
// Otherwise, it is completely unsupported platforms.
let rawTargetTriple = platformArchTriples[PlatformName]?.[ArchName]

if (rawTargetTriple) {
Log.warn(
`Trying to load next-swc for target triple ${rawTargetTriple}, but there next-swc does not have native bindings support`
)
} else {
Log.warn(
`Trying to load next-swc for unsupported platforms ${PlatformName}/${ArchName}`
)
}

return []
})()

// Allow to specify an absolute path to the custom turbopack binary to load.
// If one of env variables is set, `loadNative` will try to use any turbo-* interfaces from specified
// binary instead. This will not affect existing swc's transform, or other interfaces. This is thin,
Expand Down Expand Up @@ -65,12 +125,13 @@ function checkVersionMismatch(pkgData: any) {
// once we can verify loading-wasm-first won't cause visible regressions,
// we'll not include native bindings for these platform at all.
const knownDefaultWasmFallbackTriples = [
'aarch64-linux-android',
'x86_64-unknown-freebsd',
'aarch64-pc-windows-msvc',
'aarch64-linux-android',
'arm-linux-androideabi',
'armv7-unknown-linux-gnueabihf',
'i686-pc-windows-msvc',
// WOA targets are TBD, while current userbase is small we may support it in the future
//'aarch64-pc-windows-msvc',
]

// The last attempt's error code returned when cjs require to native bindings fails.
Expand Down

0 comments on commit 0d4ec10

Please sign in to comment.