Unverified Commit 33af645b authored by Sebastian "Sebbie" Silbermann's avatar Sebastian "Sebbie" Silbermann Committed by GitHub

[test] Convert the `prerender-native-module` suite to local fixture packages (#97542)

This suite installed `sqlite` and `sqlite3` on every run to prerender a
page from a checked-in SQLite database. Neither package was needed for
what the test covers. The pinned `sqlite3@5.0.2` has no linux-arm64
prebuild, and every job that runs this suite is linux-arm64, so each one
compiled the SQLite amalgamation from source. That pin is also Node-API
based and therefore context-aware, so it could no longer reproduce the
abort the suite was originally added for.

The fixture now uses its own compiled `native-addon` plus a
dependency-free JS wrapper standing in for `sqlite`'s role, and reads
its rows from a plain JSON file. The `path.join(process.cwd(), ...)`
expression stays in the page, because output file tracing only follows
it from the app's own code, so moving it into the wrapper would stop the
data file being traced.

The emitted traces were read rather than assumed. Turbopack and
`@vercel/nft` produce the same fixture entries, including the compiled
binary at `native-addon/build/Release/native_addon.node` and the
`process.cwd()`-derived `users.json`.

The trace assertion now checks each pattern separately instead of
collapsing them into a single `every(...)`, which could only report that
something did not match. The `notTests` block went away with it, since
`[].some(...)` asserted nothing.
parent 1f933ce9
......@@ -35,6 +35,9 @@ const customJestConfig = {
'<rootDir>/e2e/app-dir/server-source-maps/fixtures/default/internal-pkg',
'<rootDir>/e2e/app-dir/turbopack-reports/bindings',
'<rootDir>/e2e/app-dir/turbopack-reports/native-addon',
'<rootDir>/e2e/prerender-native-module/bindings',
'<rootDir>/e2e/prerender-native-module/native-addon',
'<rootDir>/e2e/prerender-native-module/native-addon-wrapper',
'<rootDir>/e2e/transpile-packages-typescript-foreign/pkg',
'<rootDir>/production/standalone-mode/tracing-side-effects-false/foo',
'<rootDir>/production/standalone-mode/tracing-static-files/foo',
......
import path from 'path'
import { FileRef, isReact18, nextTestSetup } from 'e2e-utils'
import { isReact18, nextTestSetup } from 'e2e-utils'
const fixture = path.join(__dirname, 'prerender-native-module')
const expectedUsers = [
{ id: 1, first_name: 'john', last_name: 'deux' },
{ id: 2, first_name: 'zeit', last_name: 'geist' },
]
describe('prerender native module', () => {
const { next } = nextTestSetup({
files: {
pages: new FileRef(path.join(__dirname, 'prerender-native-module/pages')),
'data.sqlite': new FileRef(
path.join(__dirname, 'prerender-native-module/data.sqlite')
),
},
dependencies: {
sqlite: '4.0.22',
sqlite3: '5.0.2',
},
packageJson: {
pnpm: {
onlyBuiltDependencies: ['sqlite3'],
},
},
files: fixture,
// `bindings` is a direct dependency rather than one of `native-addon`. The
// addon resolves it by walking up from the virtual store, and declaring it on
// the addon would make it a non-registry transitive dependency, which
// `blockExoticSubdeps` rejects.
//
// These use `file:` rather than `link:` so pnpm copies them into the virtual
// store. A linked package resolves to a path inside the app, which the bundler
// treats as app code and tries to bundle, and `bindings` contains a `require`
// it cannot resolve statically.
dependencies: require('./prerender-native-module/package.json')
.dependencies,
})
it('should render index correctly', async () => {
......@@ -35,10 +39,8 @@ describe('prerender native module', () => {
expect(JSON.parse(await browser.elementByCss('#props').text())).toEqual({
params: { slug: 'first' },
blog: true,
users: [
{ id: 1, first_name: 'john', last_name: 'deux' },
{ id: 2, first_name: 'zeit', last_name: 'geist' },
],
contextAware: true,
users: expectedUsers,
})
})
......@@ -50,10 +52,8 @@ describe('prerender native module', () => {
expect(JSON.parse(await browser.elementByCss('#props').text())).toEqual({
params: { slug: 'second' },
blog: true,
users: [
{ id: 1, first_name: 'john', last_name: 'deux' },
{ id: 2, first_name: 'zeit', last_name: 'geist' },
],
contextAware: true,
users: expectedUsers,
})
})
......@@ -70,7 +70,6 @@ describe('prerender native module', () => {
? /node_modules\/react\/cjs\/react\.production\.min\.js/
: /node_modules\/react\/cjs\/react\.production\.js/,
],
notTests: [],
},
{
page: '/blog/[slug]',
......@@ -81,13 +80,16 @@ describe('prerender native module', () => {
isReact18
? /node_modules\/react\/cjs\/react\.production\.min\.js/
: /node_modules\/react\/cjs\/react\.production\.js/,
/node_modules\/sqlite3\/.*?\.js/,
/node_modules\/sqlite3\/.*?\.node/,
/node_modules\/sqlite\/.*?\.js/,
// The addon's JS entry, and the binary it locates through
// `require('bindings')(...)`.
/node_modules\/native-addon\/.*?\.js/,
/node_modules\/native-addon\/.*?\.node/,
// The pure-JS wrapper layered on top of it.
/node_modules\/native-addon-wrapper\/.*?\.js/,
/node_modules\/next/,
/\/data\.sqlite/,
// Reached only by statically evaluating `path.join(process.cwd(), ...)`.
/\/users\.json/,
],
notTests: [],
},
]
......@@ -97,14 +99,11 @@ describe('prerender native module', () => {
)
const { version, files } = JSON.parse(contents)
expect(version).toBe(1)
expect(
check.tests.every((item) => files.some((file) => item.test(file)))
).toBe(true)
if (path.sep === '/') {
expect(
check.notTests.some((item) => files.some((file) => item.test(file)))
).toBe(false)
for (const test of check.tests) {
expect(files).toEqual(
expect.arrayContaining([expect.stringMatching(test)])
)
}
}
})
......
# Deploy builds install with npm, which symlinks a `file:` directory dependency by
# default. A symlink resolves back into the app, so the server build stops treating
# the package as external and bundles it, and `bindings` cannot survive that: it
# locates its binary from the calling file's path, which becomes a path inside
# `.next` once bundled.
#
# `install-links` makes npm pack the dependency into `node_modules` instead. It only
# takes effect when the dependency is specified without a leading `./`, which is why
# `package.json` says `file:native-addon` rather than `file:./native-addon`.
#
# pnpm ignores this setting and copies the directory into its virtual store either
# way, so the local install is unaffected.
install-links=true
// A minimal stand-in for the `bindings` npm package, reproducing only the part
// both bundlers model: locate the calling package's root by walking up for a
// `package.json`, then load the compiled binary from `build/Release`.
//
// Neither bundler evaluates this file. `@vercel/nft` maps the `bindings`
// specifier to its own bundled copy, and Turbopack maps it to
// `WellKnownFunctionKind::NodeBindings`; both then resolve the binary's path
// themselves. `build/Release/<name>` is node-gyp's default output location and
// is on both of their candidate lists, so this agrees with what they trace.
const fs = require('fs')
const path = require('path')
function getCallerFile() {
const { prepareStackTrace } = Error
try {
Error.prepareStackTrace = (_error, stack) => stack
for (const frame of new Error().stack) {
const fileName = frame.getFileName()
if (typeof fileName === 'string' && fileName !== __filename) {
return fileName
}
}
} finally {
Error.prepareStackTrace = prepareStackTrace
}
throw new Error('bindings: could not determine the calling file')
}
function getPackageRoot(file) {
let dir = path.dirname(file)
while (!fs.existsSync(path.join(dir, 'package.json'))) {
const parent = path.dirname(dir)
if (parent === dir) {
throw new Error(`bindings: found no package.json above ${file}`)
}
dir = parent
}
return dir
}
module.exports = function bindings(name) {
const binary = path.join(
getPackageRoot(getCallerFile()),
'build',
'Release',
name
)
if (!fs.existsSync(binary)) {
throw new Error(
`bindings: ${binary} does not exist. The fixture addon is compiled by ` +
`node-gyp during install, which requires the package to be listed in ` +
`the test's pnpm.onlyBuiltDependencies.`
)
}
// Deliberately not wrapped. A non-context-aware addon fails on this line with
// Node's own `ERR_DLOPEN_FAILED` / "Module did not self-register", and the
// worker-thread tests assert on that message.
return require(binary)
}
{
"name": "bindings",
"version": "1.0.0",
"description": "Minimal stand-in for the bindings npm package, for tests",
"main": "index.js"
}
// Stands in for a pure-JS convenience package layered over a native one, the
// role `sqlite` played over `sqlite3`. It has no dependencies of its own and
// resolves `native-addon` positionally, so it does not need a dependency edge.
//
// The caller passes an already-resolved `filename`. Keep it that way: output
// file tracing only follows a `process.cwd()`-derived path when the expression
// appears in the app's own code, since Turbopack gates that on the source not
// being inside `node_modules`.
const fs = require('fs/promises')
exports.open = async function open({ filename, driver }) {
const rows = JSON.parse(await fs.readFile(filename, 'utf8'))
return {
contextAware: driver.CONTEXT_AWARE,
all: async () => rows,
}
}
{
"name": "native-addon-wrapper",
"version": "1.0.0",
"description": "Dependency-free JS wrapper over native-addon, for tests",
"main": "index.js"
}
// A context-aware addon. `NODE_MODULE_INIT` registers per-context, so this can
// be loaded from the main thread and from a worker thread in the same process.
// See https://nodejs.org/api/addons.html#context-aware-addons
#include <node.h>
NODE_MODULE_INIT(/* exports, module, context */) {
v8::Isolate* isolate = context->GetIsolate();
exports
->Set(context,
v8::String::NewFromUtf8(isolate, "CONTEXT_AWARE").ToLocalChecked(),
v8::Boolean::New(isolate, true))
.Check();
}
{
"targets": [
{
"target_name": "native_addon",
"sources": ["addon.cc"]
}
]
}
// Mirrors how `sqlite3` loads its binary, which is the shape both bundlers
// special-case: a single `require('bindings')('<name>.node')` call.
module.exports = require('bindings')('native_addon.node')
{
"name": "native-addon",
"version": "1.0.0",
"description": "Context-aware native addon fixture, compiled by node-gyp",
"main": "index.js"
}
{
"dependencies": {
"native-addon": "file:native-addon",
"native-addon-wrapper": "file:native-addon-wrapper",
"bindings": "file:bindings"
}
}
import path from 'path'
import { open } from 'sqlite'
import sqlite3 from 'sqlite3'
import { open } from 'native-addon-wrapper'
import nativeAddon from 'native-addon'
import { useRouter } from 'next/router'
export const getStaticProps = async ({ params }) => {
const dbPath = path.join(process.cwd(), 'data.sqlite')
console.log('using db', dbPath)
// The `process.cwd()` join stays in the page on purpose. Output file tracing
// only follows it into the trace from the app's own code, so moving it into
// `native-addon-wrapper` would stop `users.json` being traced.
const dataPath = path.join(process.cwd(), 'users.json')
console.log('using data', dataPath)
const db = await open({
filename: dbPath,
driver: sqlite3.Database,
filename: dataPath,
driver: nativeAddon,
})
const users = await db.all(`SELECT * FROM users`)
const users = await db.all()
return {
props: {
users,
// Read off the compiled binary, so a native module that failed to load
// shows up here rather than passing quietly.
contextAware: db.contextAware,
blog: true,
params: params || null,
},
......
# `native-addon` is compiled by node-gyp during install, and pnpm does
# not run a dependency's build scripts unless they are approved here. The repo
# root uses the same key for the one package it allows to build.
allowBuilds:
native-addon: true
[
{ "id": 1, "first_name": "john", "last_name": "deux" },
{ "id": 2, "first_name": "zeit", "last_name": "geist" }
]
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment