Unverified Commit c85be347 authored by vercel-fleet-prod[bot]'s avatar vercel-fleet-prod[bot] Committed by GitHub

Test Turbopack NFT API compatibility

Co-authored-by: 's avatarNiklas Mischkulnig <4586894+mischnic@users.noreply.github.com>
parent 8a517f43
/* auto-generated by NAPI-RS */
/* eslint-disable */
export interface NapiNftResult {
files: Array<RcStr>
issues: Array<RcStr>
files: Array<string>
issues: Array<string>
}
export declare function nodeFileTrace(
projectRoot: RcStr,
cwd: RcStr,
outputBase: RcStr,
projectRoot: string,
cwd: string,
outputBase: string,
input: Array<string>,
graph: boolean,
showIssues: boolean,
......
......@@ -2,9 +2,15 @@
"name": "@turbopack/nft",
"version": "0.1.0",
"private": true,
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"build": "pnpm run build-native && pnpm run build-js",
"build-js": "tsc -p tsconfig.json",
"build-native": "napi build --platform -p turbopack-nft-napi --manifest-path ../../Cargo.toml --no-js -o native",
"build-native-release": "napi build --platform -p turbopack-nft-napi --manifest-path ../../Cargo.toml --release --no-js -o native"
"build-native-release": "napi build --platform -p turbopack-nft-napi --manifest-path ../../Cargo.toml --release --no-js -o native",
"test:unit": "pnpm run build && node --test test/unit.test.js",
"typecheck": "tsc -p tsconfig.json --noEmit"
},
"napi": {
"binaryName": "turbopack-nft",
......@@ -19,6 +25,8 @@
]
},
"devDependencies": {
"@napi-rs/cli": "3.7.2"
"@napi-rs/cli": "3.7.2",
"@types/node": "20.17.7",
"typescript": "6.0.2"
}
}
import path from 'path'
import path from 'node:path'
const native =
require('../native/turbopack-nft.darwin-arm64.node') as typeof import('../native/index.d.ts')
interface NativeNftResult {
files: string[]
issues: string[]
}
interface NativeBinding {
nodeFileTrace(
projectRoot: string,
cwd: string,
outputBase: string,
input: string[],
graph: boolean,
showIssues: boolean,
maxDepth?: number | null
): Promise<NativeNftResult>
}
function isMusl(): boolean {
const report = process.report?.getReport() as
| { header?: { glibcVersionRuntime?: string } }
| undefined
return !report?.header?.glibcVersionRuntime
}
function nativePlatform(): string {
switch (process.platform) {
case 'darwin':
if (process.arch === 'arm64') return 'darwin-arm64'
if (process.arch === 'x64') return 'darwin-x64'
break
case 'linux':
if (process.arch === 'arm64')
return isMusl() ? 'linux-arm64-musl' : 'linux-arm64-gnu'
if (process.arch === 'x64')
return isMusl() ? 'linux-x64-musl' : 'linux-x64-gnu'
break
case 'win32':
if (process.arch === 'x64') return 'win32-x64-msvc'
break
}
throw new Error(
`@turbopack/nft does not provide a native binding for ${process.platform}-${process.arch}`
)
}
const native = require(
path.join(__dirname, '..', 'native', `turbopack-nft.${nativePlatform()}.node`)
) as NativeBinding
export interface Stats {
isFile(): boolean
isDirectory(): boolean
isBlockDevice(): boolean
isCharacterDevice(): boolean
isSymbolicLink(): boolean
isFIFO(): boolean
isSocket(): boolean
dev: number
ino: number
mode: number
nlink: number
uid: number
gid: number
rdev: number
size: number
blksize: number
blocks: number
atimeMs: number
mtimeMs: number
ctimeMs: number
birthtimeMs: number
atime: Date
mtime: Date
ctime: Date
birthtime: Date
}
export interface NodeFileTraceOptions {
base?: string
......@@ -9,6 +84,7 @@ export interface NodeFileTraceOptions {
exports?: string[]
conditions?: string[]
exportsOnly?: boolean
moduleSyncCatchall?: boolean
ignore?: string | string[] | ((path: string) => boolean)
analysis?:
| boolean
......@@ -22,15 +98,15 @@ export interface NodeFileTraceOptions {
ts?: boolean
log?: boolean
mixedModules?: boolean
// readFile?: (path: string) => Promise<Buffer | string | null>
// stat?: (path: string) => Promise<Stats | null>
// readlink?: (path: string) => Promise<string | null>
// resolve?: (
// id: string,
// parent: string,
// job: Job,
// cjsResolve: boolean
// ) => Promise<string | string[]>
readFile?: (path: string) => Promise<Buffer | string | null>
stat?: (path: string) => Promise<Stats | null>
readlink?: (path: string) => Promise<string | null>
resolve?: (
id: string,
parent: string,
job: unknown,
cjsResolve: boolean
) => Promise<string | string[]>
fileIOConcurrency?: number
depth?: number
}
......@@ -42,15 +118,14 @@ export type NodeFileTraceReasonType =
| 'asset'
| 'sharedlib'
export interface NodeFileTraceReason {
type: NodeFileTraceReasonType[]
ignored: boolean
parents: Set<string>
}
export interface NodeFileTraceReasons
extends Map<
string,
{
type: NodeFileTraceReasonType[]
ignored: boolean
parents: Set<string>
}
> {}
extends Map<string, NodeFileTraceReason> {}
export interface NodeFileTraceResult {
fileList: Set<string>
......@@ -59,34 +134,73 @@ export interface NodeFileTraceResult {
warnings: Set<Error>
}
function commonAncestor(paths: string[]): string {
const resolved = paths.map((value) => path.resolve(value))
const root = path.parse(resolved[0]).root
if (resolved.some((value) => path.parse(value).root !== root)) return root
const parts = resolved.map((value) =>
value.slice(root.length).split(path.sep)
)
const shared: string[] = []
for (let index = 0; index < parts[0].length; index++) {
const candidate = parts[0][index]
if (parts.every((value) => value[index] === candidate)) {
shared.push(candidate)
} else {
break
}
}
return path.join(root, ...shared)
}
function normalizePath(value: string): string {
return value.split(path.sep).join('/')
}
export async function nodeFileTrace(
files: string[],
opts: NodeFileTraceOptions = {}
): Promise<NodeFileTraceResult> {
let base = path.resolve(opts.base || process.cwd())
let processCwd = path.resolve(opts.processCwd || base)
let root = base.length < processCwd.length ? base : processCwd
let x = await native.nodeFileTrace(
root,
path.relative(root, processCwd),
path.relative(root, base),
files.map((f) => path.relative(root, path.resolve(f))),
false,
const base = path.resolve(opts.base || process.cwd())
const processCwd = path.resolve(opts.processCwd || base)
const absoluteFiles = files.map((file) => path.resolve(file))
const projectRoot = commonAncestor([base, processCwd, ...absoluteFiles])
const result = await native.nodeFileTrace(
projectRoot,
path.relative(projectRoot, processCwd) || '.',
path.relative(projectRoot, base) || '.',
absoluteFiles.map((file) => path.relative(projectRoot, file)),
false,
0
// TODO pass along ignore globs
Boolean(opts.log),
opts.depth
)
let ignoreFn: Function | undefined =
typeof opts.ignore === 'function'
? // @ts-ignore
opts.ignore
: undefined
const initialFiles = new Set(
absoluteFiles.map((file) => normalizePath(path.relative(base, file)))
)
const normalizedFiles = result.files.map(normalizePath)
const ignore = opts.ignore
const ignored =
typeof ignore === 'function'
? normalizedFiles.filter((file) => !ignore(file))
: normalizedFiles
const fileList = new Set(ignored)
const reasons: NodeFileTraceReasons = new Map()
for (const file of normalizedFiles) {
reasons.set(file, {
type: [initialFiles.has(file) ? 'initial' : 'dependency'],
ignored: !fileList.has(file),
parents: new Set(),
})
}
return {
fileList: new Set(ignoreFn ? x.files.filter((f) => !ignoreFn(f)) : x.files),
fileList,
esmFileList: new Set(),
warnings: new Set(),
reasons: new Map(),
reasons,
warnings: new Set(result.issues.map((issue) => new Error(issue))),
}
}
// Adapted from vercel/nft test/unit.test.js at
// 941be1cb3b9d38fbc99a8a6f07af568a64ed0f12. The fixture tree was imported
// from the same revision in vercel/next.js#83233 and remains in place there.
//
// The upstream runner's analyze/graceful-fs mocks, I/O call-count assertions,
// cache assertions, and JavaScript readFile/resolve hooks inspect its pure-JS
// implementation. They cannot cross the native boundary, so this runner keeps
// the behavioral contract: each case must match its unchanged output.js list.
const fs = require('node:fs')
const path = require('node:path')
const test = require('node:test')
const assert = require('node:assert/strict')
const vm = require('node:vm')
const { nodeFileTrace } = require('../dist')
const fixtureRoot = path.resolve(
__dirname,
'../../../turbopack/crates/turbopack-tracing/tests/node-file-trace'
)
const linuxX64Binding = path.resolve(
__dirname,
'../native/turbopack-nft.linux-x64-gnu.node'
)
test(
'loads the Linux x64 GNU native binding on the verification host',
{
skip:
process.platform === 'linux' && process.arch === 'x64'
? false
: 'This assertion targets the Linux x64 GNU verification host.',
},
() => {
assert.ok(require.cache[linuxX64Binding])
}
)
// These dependency-light cases are also enabled by turbopack-tracing/tests/unit.rs.
const passingCases = [
'amd-disable',
'array-emission',
'array-holes',
'class-static',
'dot-dot',
]
const semanticSkips = new Map([
[
'asset-conditional',
'Turbopack represents a non-static ternary as Unknown instead of tracing nft alternatives.',
],
[
'browser-remappings',
'Turbopack traces the selected browser resolution, while nft also lists candidate resolutions.',
],
[
'depth-0',
'The native wrapper does not yet limit the traced dependency depth.',
],
[
'exports',
'Turbopack follows the selected exports target, while nft also traces the legacy main target.',
],
[
'resolve-hook',
'JavaScript custom resolve hooks cannot cross the native tracing boundary.',
],
])
function readExpected(caseDirectory) {
const source = fs.readFileSync(path.join(caseDirectory, 'output.js'), 'utf8')
const value = vm.runInNewContext(source)
return Array.from(value).sort()
}
for (const testName of passingCases) {
test(`matches @vercel/nft unit fixture: ${testName}`, async () => {
const caseDirectory = path.join(fixtureRoot, 'test/unit', testName)
const result = await nodeFileTrace([path.join(caseDirectory, 'input.js')], {
base: fixtureRoot,
processCwd: caseDirectory,
ts: true,
log: false,
analysis: true,
mixedModules: true,
ignore: (file) => file.endsWith('/actual.js'),
})
assert.ok(result.fileList instanceof Set)
assert.ok(result.esmFileList instanceof Set)
assert.ok(result.reasons instanceof Map)
assert.ok(result.warnings instanceof Set)
for (const file of result.fileList) assert.ok(result.reasons.has(file))
assert.ok(
result.reasons
.get(`test/unit/${testName}/input.js`)
?.type.includes('initial')
)
assert.deepStrictEqual(
Array.from(result.fileList).sort(),
readExpected(caseDirectory)
)
})
}
for (const [testName, reason] of semanticSkips) {
test(
`skips unsupported @vercel/nft unit fixture: ${testName}`,
{ skip: reason },
() => {}
)
}
{
"compilerOptions": {
"declaration": true,
"esModuleInterop": true,
"module": "Node16",
"moduleResolution": "Node16",
"outDir": "dist",
"rootDir": "src",
"skipLibCheck": true,
"strict": true,
"target": "ES2020",
"types": ["node"]
},
"include": ["src/**/*.ts"]
}
......@@ -1987,7 +1987,13 @@ importers:
devDependencies:
'@napi-rs/cli':
specifier: 3.7.2
version: 3.7.2(@emnapi/runtime@1.9.2)(@types/node@20.17.7(patch_hash=fe6957869a9b616ec526ededb72bbe846b863bd2349204218772df5c1e62d6ab))(node-addon-api@6.1.0)
version: 3.7.2(@emnapi/runtime@1.11.2)(@types/node@20.17.7(patch_hash=fe6957869a9b616ec526ededb72bbe846b863bd2349204218772df5c1e62d6ab))(node-addon-api@6.1.0)
'@types/node':
specifier: 20.17.7
version: 20.17.7(patch_hash=fe6957869a9b616ec526ededb72bbe846b863bd2349204218772df5c1e62d6ab)
typescript:
specifier: 6.0.2
version: 6.0.2
turbopack/crates/turbopack-cli/js:
dependencies:
......@@ -2,7 +2,6 @@
#![feature(arbitrary_self_types_pointers)]
use napi_derive::napi;
use turbo_rcstr::RcStr;
use turbo_tasks::TurboTasks;
use turbo_tasks_backend::{BackendOptions, TurboTasksBackend, noop_backing_storage};
......@@ -11,15 +10,15 @@ static ALLOC: turbo_tasks_malloc::TurboMalloc = turbo_tasks_malloc::TurboMalloc;
#[napi(object)]
pub struct NapiNftResult {
pub files: Vec<RcStr>,
pub issues: Vec<RcStr>,
pub files: Vec<String>,
pub issues: Vec<String>,
}
#[napi]
pub async fn node_file_trace(
project_root: RcStr,
cwd: RcStr,
output_base: RcStr,
project_root: String,
cwd: String,
output_base: String,
input: Vec<String>,
graph: bool,
show_issues: bool,
......@@ -37,9 +36,9 @@ pub async fn node_file_trace(
let result = tt
.run_once(async move {
turbopack_nft::nft::node_file_trace(
project_root,
cwd,
output_base,
project_root.into(),
cwd.into(),
output_base.into(),
input.into_iter().map(Into::into).collect(),
graph,
show_issues,
......@@ -51,7 +50,7 @@ pub async fn node_file_trace(
.map_err(|e| napi::Error::from_reason(format!("{e:#}")))?;
Ok(NapiNftResult {
files: result.files,
issues: result.issues,
files: result.files.into_iter().map(Into::into).collect(),
issues: result.issues.into_iter().map(Into::into).collect(),
})
}
......@@ -74,7 +74,7 @@ pub async fn node_file_trace(
})
}
#[turbo_tasks::function(operation)]
#[turbo_tasks::function(operation, root)]
async fn node_file_trace_operation(
project_root: RcStr,
cwd: RcStr,
......
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