|
| 1 | +/** #4055: native addons built against Node >=24.19 headers abort on Node 24.19-24.21 (nodejs/node#65446). */ |
| 2 | +import { afterEach, beforeEach, describe, expect, it } from 'vitest'; |
| 3 | +import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'; |
| 4 | +import { tmpdir } from 'node:os'; |
| 5 | +import { join } from 'node:path'; |
| 6 | +import { |
| 7 | + checkNativeAddonHeaders, |
| 8 | + headersVersionFromGypi, |
| 9 | + isAffectedNodeVersion, |
| 10 | +} from '../src/commands/doctor-native-addon.js'; |
| 11 | + |
| 12 | +let root: string; |
| 13 | +beforeEach(() => { root = mkdtempSync(join(tmpdir(), 'ruflo-4055-')); }); |
| 14 | +afterEach(() => { rmSync(root, { recursive: true, force: true }); }); |
| 15 | + |
| 16 | +const gypi = (nodedir: string) => `# Do not edit. File was generated by node-gyp's "configure" step |
| 17 | +{ |
| 18 | + "target_defaults": { "cflags": [], "default_configuration": "Release" }, |
| 19 | + "variables": { |
| 20 | + "napi_build_version": "0", |
| 21 | + "nodedir": "${nodedir}", |
| 22 | + "node_module_version": 137 |
| 23 | + } |
| 24 | +} |
| 25 | +`; |
| 26 | + |
| 27 | +function addon(dir: string, version: string, nodedir: string | null) { |
| 28 | + mkdirSync(join(dir, 'build'), { recursive: true }); |
| 29 | + writeFileSync(join(dir, 'package.json'), JSON.stringify({ name: 'better-sqlite3', version })); |
| 30 | + if (nodedir !== null) writeFileSync(join(dir, 'build', 'config.gypi'), gypi(nodedir)); |
| 31 | +} |
| 32 | + |
| 33 | +describe('isAffectedNodeVersion', () => { |
| 34 | + it.each([ |
| 35 | + ['24.18.1', false], ['24.19.0', true], ['24.20.1', true], ['24.21.0', true], |
| 36 | + ['24.21.5', true], ['22.23.2', false], ['25.0.0', false], ['20.11.0', false], |
| 37 | + ])('%s -> %s', (v, expected) => expect(isAffectedNodeVersion(v)).toBe(expected)); |
| 38 | +}); |
| 39 | + |
| 40 | +describe('headersVersionFromGypi', () => { |
| 41 | + it('reads the version out of the node-gyp cache nodedir', () => { |
| 42 | + expect(headersVersionFromGypi(gypi('/home/u/.cache/node-gyp/24.20.0'))).toBe('24.20.0'); |
| 43 | + }); |
| 44 | + it('reads a node-vXX source-tree nodedir', () => { |
| 45 | + expect(headersVersionFromGypi(gypi('/opt/node-v24.21.0'))).toBe('24.21.0'); |
| 46 | + }); |
| 47 | + it('returns null when no version can be read', () => { |
| 48 | + expect(headersVersionFromGypi(gypi('/usr/include/node'))).toBeNull(); |
| 49 | + expect(headersVersionFromGypi('not a gypi')).toBeNull(); |
| 50 | + }); |
| 51 | +}); |
| 52 | + |
| 53 | +describe('checkNativeAddonHeaders', () => { |
| 54 | + it('warns for a copy built against 24.20 headers on Node 24.21', async () => { |
| 55 | + const bad = join(root, 'node_modules', 'agentdb', 'node_modules', 'better-sqlite3'); |
| 56 | + addon(bad, '11.10.0', '/home/u/.cache/node-gyp/24.20.0'); |
| 57 | + const r = await checkNativeAddonHeaders({ nodeVersion: '24.21.0', copies: [bad] }); |
| 58 | + expect(r.status).toBe('warn'); |
| 59 | + expect(r.message).toContain('11.10.0'); |
| 60 | + expect(r.message).toContain('24.20.0'); |
| 61 | + expect(r.message).toContain(bad); |
| 62 | + expect(r.fix).toMatch(/24\.18\.1/); |
| 63 | + expect(r.fix).toMatch(/node-gyp rebuild/); |
| 64 | + expect(r.fix).toMatch(/nodedir/); |
| 65 | + }); |
| 66 | + |
| 67 | + it('passes for a prebuilt copy (no config.gypi) on an affected Node', async () => { |
| 68 | + const pre = join(root, 'node_modules', 'better-sqlite3'); |
| 69 | + addon(pre, '12.11.1', null); |
| 70 | + const r = await checkNativeAddonHeaders({ nodeVersion: '24.21.0', copies: [pre] }); |
| 71 | + expect(r.status).toBe('pass'); |
| 72 | + }); |
| 73 | + |
| 74 | + it('passes for a copy rebuilt against 24.18.1 headers', async () => { |
| 75 | + const ok = join(root, 'node_modules', 'better-sqlite3'); |
| 76 | + addon(ok, '11.10.0', '/home/u/.cache/node-gyp/24.18.1'); |
| 77 | + const r = await checkNativeAddonHeaders({ nodeVersion: '24.21.0', copies: [ok] }); |
| 78 | + expect(r.status).toBe('pass'); |
| 79 | + }); |
| 80 | + |
| 81 | + it('is not applicable off the affected Node range', async () => { |
| 82 | + const bad = join(root, 'node_modules', 'better-sqlite3'); |
| 83 | + addon(bad, '11.10.0', '/home/u/.cache/node-gyp/24.20.0'); |
| 84 | + for (const nodeVersion of ['22.23.2', '24.18.1', '25.0.0']) { |
| 85 | + const r = await checkNativeAddonHeaders({ nodeVersion, copies: [bad] }); |
| 86 | + expect(r.status).toBe('pass'); |
| 87 | + expect(r.message).toMatch(/not affected|not in the affected/i); |
| 88 | + } |
| 89 | + }); |
| 90 | + |
| 91 | + it('reports every bad copy, not just the first', async () => { |
| 92 | + const a = join(root, 'node_modules', 'agentdb', 'node_modules', 'better-sqlite3'); |
| 93 | + const b = join(root, 'node_modules', 'agentic-flow', 'node_modules', 'better-sqlite3'); |
| 94 | + addon(a, '11.10.0', '/c/node-gyp/24.20.0'); |
| 95 | + addon(b, '11.10.0', '/c/node-gyp/24.19.0'); |
| 96 | + const r = await checkNativeAddonHeaders({ nodeVersion: '24.20.0', copies: [a, b] }); |
| 97 | + expect(r.status).toBe('warn'); |
| 98 | + expect(r.message).toContain(a); |
| 99 | + expect(r.message).toContain(b); |
| 100 | + }); |
| 101 | + |
| 102 | + it('discovers nested copies under node_modules by walking up from a start dir', async () => { |
| 103 | + const top = join(root, 'node_modules', 'better-sqlite3'); |
| 104 | + const nested = join(root, 'node_modules', 'agentdb', 'node_modules', 'better-sqlite3'); |
| 105 | + addon(top, '12.11.1', null); |
| 106 | + addon(nested, '11.10.0', '/c/node-gyp/24.20.0'); |
| 107 | + const start = join(root, 'node_modules', '@claude-flow', 'cli', 'dist', 'src', 'commands'); |
| 108 | + mkdirSync(start, { recursive: true }); |
| 109 | + const r = await checkNativeAddonHeaders({ nodeVersion: '24.21.0', startDirs: [start] }); |
| 110 | + expect(r.status).toBe('warn'); |
| 111 | + expect(r.message).toContain(nested); |
| 112 | + expect(r.message).not.toContain(`${top} `); |
| 113 | + }); |
| 114 | + |
| 115 | + it('never throws on unreadable or malformed files', async () => { |
| 116 | + const weird = join(root, 'node_modules', 'better-sqlite3'); |
| 117 | + mkdirSync(join(weird, 'build'), { recursive: true }); |
| 118 | + writeFileSync(join(weird, 'package.json'), '{not json'); |
| 119 | + writeFileSync(join(weird, 'build', 'config.gypi'), '\u0000\u0001'); |
| 120 | + const r = await checkNativeAddonHeaders({ nodeVersion: '24.21.0', copies: [weird, join(root, 'missing')] }); |
| 121 | + expect(['pass', 'warn']).toContain(r.status); |
| 122 | + }); |
| 123 | +}); |
0 commit comments