Skip to content

The agent that grows with you

fix(install): let Rolldown name the missing binding; repair on every OS

teknium13dcf0d49adba
diff --git a/apps/desktop/scripts/ensure-rolldown-binding.mjs b/apps/desktop/scripts/ensure-rolldown-binding.mjs
index 0567b100b2c..7f38e80debb 100644
--- a/apps/desktop/scripts/ensure-rolldown-binding.mjs
+++ b/apps/desktop/scripts/ensure-rolldown-binding.mjs
@@ -3,14 +3,16 @@ import { dirname, join, resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
import { spawnSync } from 'node:child_process'
-const appDir = resolve(dirname(fileURLToPath(import.meta.url)), '..')
-const rootDir = resolve(appDir, '..', '..')
+const rootDir = resolve(dirname(fileURLToPath(import.meta.url)), '..', '..', '..')
-export function selectRolldownBinding(optionalDependencies, platform, arch) {
- const suffix = `binding-${platform}-${arch}`
- const matches = Object.entries(optionalDependencies ?? {}).filter(([name]) => name.endsWith(suffix))
-
- return matches.length === 1 ? matches[0] : null
+// Rolldown's loader already resolves platform, arch and libc; when the native
+// package is absent, its load-error chain names the exact `@rolldown/binding-*`
+// it wanted, followed by the wasm fallback it also tried. Reuse that verdict
+// instead of re-deriving it from `process.platform` (Windows bindings carry a
+// `-msvc` suffix, Linux ones `-gnu`/`-musl`).
+export function missingRolldownBinding(stderr) {
+ const names = [...String(stderr ?? '').matchAll(/@rolldown\/binding-[a-z0-9-]+/g)].map(m => m[0])
+ return names.find(name => !name.includes('wasm32')) ?? null
}
function probeRolldown(root) {
@@ -21,57 +23,46 @@ function probeRolldown(root) {
}
function installBinding(root, spec) {
- const npm = process.platform === 'win32' ? 'npm.cmd' : 'npm'
- return spawnSync(npm, ['install', '--no-save', '--package-lock=false', '--include=optional', spec], {
+ // npm.cmd needs a shell on Windows (Node refuses to spawn .cmd directly).
+ return spawnSync('npm', ['install', '--no-save', '--package-lock=false', '--include=optional', spec], {
cwd: root,
- stdio: 'inherit'
+ stdio: 'inherit',
+ shell: process.platform === 'win32'
})
}
+function installedRolldownVersion(root) {
+ const packagePath = [
+ join(root, 'node_modules', 'rolldown', 'package.json'),
+ join(root, 'apps', 'desktop', 'node_modules', 'rolldown', 'package.json')
+ ].find(existsSync)
+ return packagePath ? JSON.parse(readFileSync(packagePath, 'utf8')).version : null
+}
+
export function ensureRolldownBinding({
root = rootDir,
- platform = process.platform,
- arch = process.arch,
probe = probeRolldown,
install = installBinding,
- findPackage = rootPath =>
- [
- join(rootPath, 'node_modules', 'rolldown', 'package.json'),
- join(rootPath, 'apps', 'desktop', 'node_modules', 'rolldown', 'package.json')
- ].find(existsSync),
- readPackage = path => JSON.parse(readFileSync(path, 'utf8'))
+ rolldownVersion = installedRolldownVersion
} = {}) {
const initial = probe(root)
if (initial.status === 0) return true
- let rolldownPackage
- try {
- const packagePath = findPackage(root)
- if (!packagePath) throw new Error('package.json was not found')
- rolldownPackage = readPackage(packagePath)
- } catch (error) {
- console.error(`Could not inspect the installed Rolldown package: ${error.message}`)
+ const name = missingRolldownBinding(initial.stderr)
+ const version = rolldownVersion(root)
+ if (!name || !version) {
+ console.error(initial.stderr?.trim() || 'Rolldown could not load.')
return false
}
- const binding = selectRolldownBinding(rolldownPackage.optionalDependencies, platform, arch)
- if (!binding) {
- console.error(`Rolldown has no unambiguous native binding for ${platform}/${arch}.`)
- return false
- }
-
- const [name, version] = binding
- console.warn(`Rolldown could not load; repairing ${name}@${version}...`)
- const installed = install(root, `${name}@${version}`)
- if (installed.status !== 0) return false
+ console.warn(`Rolldown could not load; installing ${name}@${version}...`)
+ if (install(root, `${name}@${version}`).status !== 0) return false
const repaired = probe(root)
if (repaired.status !== 0) {
- const detail = repaired.stderr?.trim() || repaired.stdout?.trim()
- console.error(detail || `Rolldown still cannot load after installing ${name}.`)
+ console.error(repaired.stderr?.trim() || `Rolldown still cannot load after installing ${name}.`)
return false
}
-
return true
}
diff --git a/apps/desktop/scripts/ensure-rolldown-binding.test.mjs b/apps/desktop/scripts/ensure-rolldown-binding.test.mjs
index e5bb0bea286..9f8b34b908e 100644
--- a/apps/desktop/scripts/ensure-rolldown-binding.test.mjs
+++ b/apps/desktop/scripts/ensure-rolldown-binding.test.mjs
@@ -1,23 +1,13 @@
import assert from 'node:assert/strict'
import { test } from 'vitest'
-import { ensureRolldownBinding, selectRolldownBinding } from './ensure-rolldown-binding.mjs'
+import { ensureRolldownBinding } from './ensure-rolldown-binding.mjs'
-const packageMetadata = {
- optionalDependencies: {
- '@rolldown/binding-darwin-arm64': '1.2.1',
- '@rolldown/binding-darwin-x64': '1.2.1',
- '@rolldown/binding-linux-x64-gnu': '1.2.1',
- '@rolldown/binding-linux-x64-musl': '1.2.1'
- }
-}
-
-test('selects the exact platform binding from Rolldown metadata', () => {
- assert.deepEqual(selectRolldownBinding(packageMetadata.optionalDependencies, 'darwin', 'arm64'), [
- '@rolldown/binding-darwin-arm64',
- '1.2.1'
- ])
-})
+// Shape of Node's uncaught-error output when rolldown's loader finds neither
+// the native package nor the wasm fallback (cause chain, native first).
+const loaderStderr = `Error: Cannot find native binding. npm has a bug related to optional dependencies (https://github.com/npm/cli/issues/4828).
+ [cause]: Error: Cannot find module '@rolldown/binding-win32-x64-msvc'
+ [cause]: Error: Cannot find module '@rolldown/binding-wasm32-wasi'`
test('does nothing when Rolldown already loads', () => {
let installs = 0
@@ -34,42 +24,20 @@ test('does nothing when Rolldown already loads', () => {
assert.equal(installs, 0)
})
-test('installs the missing macOS binding and verifies the repair', () => {
+test('installs the exact native binding the loader asked for, then re-verifies', () => {
let probes = 0
const installs = []
const ok = ensureRolldownBinding({
root: '/repo',
- platform: 'darwin',
- arch: 'arm64',
- probe: () => ({ status: probes++ === 0 ? 1 : 0 }),
+ probe: () => ({ status: probes++ === 0 ? 1 : 0, stderr: loaderStderr }),
install: (_root, spec) => {
installs.push(spec)
return { status: 0 }
},
- findPackage: () => '/repo/node_modules/rolldown/package.json',
- readPackage: () => packageMetadata
+ rolldownVersion: () => '1.2.1'
})
assert.equal(ok, true)
- assert.deepEqual(installs, ['@rolldown/[email protected]'])
+ assert.deepEqual(installs, ['@rolldown/[email protected]'])
assert.equal(probes, 2)
})
-
-test('fails without guessing when the platform binding is ambiguous', () => {
- let installs = 0
- const ok = ensureRolldownBinding({
- root: '/repo',
- platform: 'linux',
- arch: 'x64',
- probe: () => ({ status: 1 }),
- install: () => {
- installs += 1
- return { status: 0 }
- },
- findPackage: () => '/repo/node_modules/rolldown/package.json',
- readPackage: () => packageMetadata
- })
-
- assert.equal(ok, false)
- assert.equal(installs, 0)
-})
diff --git a/scripts/install.sh b/scripts/install.sh
index ae0d9c83b0e..93167399966 100755
--- a/scripts/install.sh
+++ b/scripts/install.sh
@@ -3566,11 +3566,11 @@ install_desktop() {
log_info "Installing desktop workspace dependencies (includes Electron ~150MB, 1-3min)..."
local _deps_start _deps_remaining
_deps_start=$(date +%s)
- if run_with_timeout "$DESKTOP_BUILD_TIMEOUT" bash -c 'cd "$1" && npm ci --include=optional && { [ "$2" != macos ] || node apps/desktop/scripts/ensure-rolldown-binding.mjs; }' _ "$INSTALL_DIR" "$OS"; then
+ if run_with_timeout "$DESKTOP_BUILD_TIMEOUT" bash -c 'cd "$1" && npm ci --include=optional && node apps/desktop/scripts/ensure-rolldown-binding.mjs' _ "$INSTALL_DIR"; then
log_success "Desktop workspace dependencies installed"
elif _deps_remaining=$(( DESKTOP_BUILD_TIMEOUT - ($(date +%s) - _deps_start) )); \
[ "$_deps_remaining" -lt 30 ] && _deps_remaining=30; \
- run_with_timeout "$_deps_remaining" bash -c 'cd "$1" && npm install --include=optional && { [ "$2" != macos ] || node apps/desktop/scripts/ensure-rolldown-binding.mjs; }' _ "$INSTALL_DIR" "$OS"; then
+ run_with_timeout "$_deps_remaining" bash -c 'cd "$1" && npm install --include=optional && node apps/desktop/scripts/ensure-rolldown-binding.mjs' _ "$INSTALL_DIR"; then
log_success "Desktop workspace dependencies installed"
elif _electron_pkg_staged_missing_dist "$INSTALL_DIR"; then
log_warn "Desktop dependency install failed with a missing Electron dist; attempting self-heal..."