Unverified Commit 0d10c073 authored by vercel-fleet-prod[bot]'s avatar vercel-fleet-prod[bot] Committed by GitHub
parent 729ff712
...@@ -10807,6 +10807,21 @@ dependencies = [ ...@@ -10807,6 +10807,21 @@ dependencies = [
"turbopack-trace-utils", "turbopack-trace-utils",
] ]
[[package]]
name = "turbopack-nft-napi"
version = "0.1.0"
dependencies = [
"anyhow",
"napi",
"napi-build",
"napi-derive",
"turbo-rcstr",
"turbo-tasks",
"turbo-tasks-backend",
"turbo-tasks-malloc",
"turbopack-nft",
]
[[package]] [[package]]
name = "turbopack-node" name = "turbopack-node"
version = "0.1.0" version = "0.1.0"
......
...@@ -186,6 +186,7 @@ turbo-unix-path = { path = "turbopack/crates/turbo-unix-path" } ...@@ -186,6 +186,7 @@ turbo-unix-path = { path = "turbopack/crates/turbo-unix-path" }
turbopack = { path = "turbopack/crates/turbopack" } turbopack = { path = "turbopack/crates/turbopack" }
turbopack-bench = { path = "turbopack/crates/turbopack-bench" } turbopack-bench = { path = "turbopack/crates/turbopack-bench" }
turbopack-nodejs = { path = "turbopack/crates/turbopack-nodejs" } turbopack-nodejs = { path = "turbopack/crates/turbopack-nodejs" }
turbopack-nft = { path = "turbopack/crates/turbopack-nft" }
turbopack-cli-utils = { path = "turbopack/crates/turbopack-cli-utils" } turbopack-cli-utils = { path = "turbopack/crates/turbopack-cli-utils" }
turbopack-core = { path = "turbopack/crates/turbopack-core" } turbopack-core = { path = "turbopack/crates/turbopack-core" }
turbopack-create-test-app = { path = "turbopack/crates/turbopack-create-test-app" } turbopack-create-test-app = { path = "turbopack/crates/turbopack-create-test-app" }
......
[package]
name = "turbopack-nft-napi"
version = "0.1.0"
description = "NAPI bindings for turbopack node-file-trace"
license = "MIT"
edition = "2024"
publish = false
[lib]
crate-type = ["cdylib"]
[lints]
workspace = true
[dependencies]
anyhow = { workspace = true }
napi = { workspace = true }
napi-derive = { workspace = true }
turbo-tasks = { workspace = true }
turbo-rcstr = { workspace = true, features = ["napi"] }
turbo-tasks-backend = { workspace = true }
turbo-tasks-malloc = { workspace = true, default-features = false, features = [
"custom_allocator",
] }
turbopack-nft = { workspace = true }
[build-dependencies]
napi-build = { workspace = true }
fn main() {
#[cfg(not(all(target_os = "macos", target_arch = "aarch64")))]
napi_build::setup();
// Workaround for napi always including a GCC specific flag on macOS aarch64.
#[cfg(all(target_os = "macos", target_arch = "aarch64"))]
{
println!("cargo:rerun-if-env-changed=DEBUG_GENERATED_CODE");
println!("cargo:rerun-if-env-changed=TYPE_DEF_TMP_PATH");
println!("cargo:rerun-if-env-changed=CARGO_CFG_NAPI_RS_CLI_VERSION");
println!("cargo:rustc-cdylib-link-arg=-undefined");
println!("cargo:rustc-cdylib-link-arg=dynamic_lookup");
}
// Resolve a potential linker issue for unit tests on linux
// https://github.com/napi-rs/napi-rs/issues/1782
#[cfg(all(target_os = "linux", not(target_arch = "wasm32")))]
println!("cargo:rustc-link-arg=-Wl,--warn-unresolved-symbols");
}
#![feature(arbitrary_self_types)]
#![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};
#[global_allocator]
static ALLOC: turbo_tasks_malloc::TurboMalloc = turbo_tasks_malloc::TurboMalloc;
#[napi(object)]
pub struct NapiNftResult {
pub files: Vec<RcStr>,
pub issues: Vec<RcStr>,
}
#[napi]
pub async fn node_file_trace(
project_root: RcStr,
cwd: RcStr,
output_base: RcStr,
input: Vec<String>,
graph: bool,
show_issues: bool,
max_depth: Option<u32>,
) -> napi::Result<NapiNftResult> {
let tt = TurboTasks::new(TurboTasksBackend::new(
BackendOptions {
dependency_tracking: false,
storage_mode: None,
..Default::default()
},
noop_backing_storage(),
));
let result = tt
.run_once(async move {
turbopack_nft::nft::node_file_trace(
project_root,
cwd,
output_base,
input.into_iter().map(Into::into).collect(),
graph,
show_issues,
max_depth.map(|d| d as usize),
)
.await
})
.await
.map_err(|e| napi::Error::from_reason(format!("{e:#}")))?;
// Intentionally leak the TurboTasks instance to avoid expensive cleanup,
// same pattern as turbopack-nft/src/main.rs
std::mem::forget(tt);
Ok(NapiNftResult {
files: result.files,
issues: result.issues,
})
}
...@@ -5,6 +5,7 @@ use std::env::current_dir; ...@@ -5,6 +5,7 @@ use std::env::current_dir;
use anyhow::Result; use anyhow::Result;
use clap::Parser; use clap::Parser;
use tracing_subscriber::{Registry, layer::SubscriberExt, util::SubscriberInitExt}; use tracing_subscriber::{Registry, layer::SubscriberExt, util::SubscriberInitExt};
use turbo_rcstr::rcstr;
use turbo_tasks::TurboTasks; use turbo_tasks::TurboTasks;
use turbo_tasks_backend::{BackendOptions, TurboTasksBackend, noop_backing_storage}; use turbo_tasks_backend::{BackendOptions, TurboTasksBackend, noop_backing_storage};
use turbo_tasks_fs::canonicalize_to_rcstr; use turbo_tasks_fs::canonicalize_to_rcstr;
...@@ -91,18 +92,22 @@ async fn main_inner(args: Arguments) -> Result<()> { ...@@ -91,18 +92,22 @@ async fn main_inner(args: Arguments) -> Result<()> {
)); ));
let project_root = canonicalize_to_rcstr(&current_dir()?)?; let project_root = canonicalize_to_rcstr(&current_dir()?)?;
tt.run_once(async move { let result = tt
node_file_trace( .run_once(node_file_trace(
project_root, project_root,
args.entry.into(), rcstr!("."),
rcstr!("."),
vec![args.entry.into()],
args.graph, args.graph,
args.show_issues, args.show_issues,
args.depth, args.depth,
) ))
.await?; .await?;
Ok(())
}) println!("FILELIST:");
.await?; for a in result.files {
println!("{a}");
}
// Intentionally leak this `Arc`. Otherwise we'll waste time during process exit performing a // Intentionally leak this `Arc`. Otherwise we'll waste time during process exit performing a
// ton of drop calls. // ton of drop calls.
......
...@@ -3,8 +3,10 @@ use std::{env::current_dir, path::PathBuf}; ...@@ -3,8 +3,10 @@ use std::{env::current_dir, path::PathBuf};
use anyhow::Result; use anyhow::Result;
use rustc_hash::FxHashSet; use rustc_hash::FxHashSet;
use turbo_rcstr::{RcStr, rcstr}; use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{TransientInstance, Vc, turbofmt}; use turbo_tasks::{
use turbo_tasks_fs::{DiskFileSystem, FileSystem}; ResolvedVc, TransientInstance, TryJoinIterExt, Vc, trace::TraceRawVcs, turbofmt,
};
use turbo_tasks_fs::{DiskFileSystem, FileSystem, FileSystemPath};
use turbopack::{ use turbopack::{
ModuleAssetContext, ModuleAssetContext,
module_options::{ module_options::{
...@@ -28,17 +30,32 @@ use turbopack_core::{ ...@@ -28,17 +30,32 @@ use turbopack_core::{
use turbopack_ecmascript::AnalyzeMode; use turbopack_ecmascript::AnalyzeMode;
use turbopack_resolve::resolve_options_context::ResolveOptionsContext; use turbopack_resolve::resolve_options_context::ResolveOptionsContext;
#[derive(TraceRawVcs)]
pub struct NftResult {
pub files: Vec<RcStr>,
pub issues: Vec<RcStr>,
}
pub async fn node_file_trace( pub async fn node_file_trace(
project_root: RcStr, project_root: RcStr,
input: RcStr, cwd: RcStr,
output_base: RcStr,
input: Vec<RcStr>,
graph: bool, graph: bool,
show_issues: bool, print_issues: bool,
max_depth: Option<usize>, max_depth: Option<usize>,
) -> Result<()> { ) -> Result<NftResult> {
let op = node_file_trace_operation(project_root.clone(), input.clone(), graph, max_depth); let op = node_file_trace_operation(
let result = op.read_strongly_consistent().await?; project_root.clone(),
cwd,
output_base,
input,
graph,
max_depth,
);
let result = op.read_strongly_consistent().owned().await?;
if show_issues { if print_issues {
let issue_reporter: Vc<Box<dyn IssueReporter>> = let issue_reporter: Vc<Box<dyn IssueReporter>> =
Vc::upcast(ConsoleUi::new(TransientInstance::new(LogOptions { Vc::upcast(ConsoleUi::new(TransientInstance::new(LogOptions {
project_dir: PathBuf::from(project_root), project_dir: PathBuf::from(project_root),
...@@ -51,18 +68,18 @@ pub async fn node_file_trace( ...@@ -51,18 +68,18 @@ pub async fn node_file_trace(
handle_issues(op, issue_reporter, IssueSeverity::Error, None, None).await?; handle_issues(op, issue_reporter, IssueSeverity::Error, None, None).await?;
} }
println!("FILELIST:"); Ok(NftResult {
for a in &result { files: result,
println!("{a}"); issues: vec![], // TODO
} })
Ok(())
} }
#[turbo_tasks::function(operation)] #[turbo_tasks::function(operation)]
async fn node_file_trace_operation( async fn node_file_trace_operation(
project_root: RcStr, project_root: RcStr,
input: RcStr, cwd: RcStr,
output_base: RcStr,
input: Vec<RcStr>,
graph: bool, graph: bool,
max_depth: Option<usize>, max_depth: Option<usize>,
) -> Result<Vc<Vec<RcStr>>> { ) -> Result<Vc<Vec<RcStr>>> {
...@@ -71,11 +88,17 @@ async fn node_file_trace_operation( ...@@ -71,11 +88,17 @@ async fn node_file_trace_operation(
Vc::cell(project_root.clone()), Vc::cell(project_root.clone()),
)); ));
let input_dir = workspace_fs.root().owned().await?; let input_dir = workspace_fs.root().owned().await?;
let input = input_dir.join(&format!("{input}"))?; let sources = input
.iter()
let source = FileSource::new(input); .map(|i| anyhow::Ok(FileSource::new(input_dir.join(i)?)))
.collect::<Result<Vec<_>, _>>()?;
let output_base = input_dir.join(&output_base)?;
let environment = Environment::new(ExecutionEnvironment::NodeJsLambda( let environment = Environment::new(ExecutionEnvironment::NodeJsLambda(
NodeJsEnvironment::default().resolved_cell(), NodeJsEnvironment {
cwd: ResolvedVc::cell(Some(input_dir.join(&cwd)?)),
..Default::default()
}
.resolved_cell(),
)); ));
let module_asset_context = ModuleAssetContext::new_without_replace_externals( let module_asset_context = ModuleAssetContext::new_without_replace_externals(
Default::default(), Default::default(),
...@@ -118,69 +141,90 @@ async fn node_file_trace_operation( ...@@ -118,69 +141,90 @@ async fn node_file_trace_operation(
.cell(), .cell(),
Layer::new(rcstr!("externals-tracing")), Layer::new(rcstr!("externals-tracing")),
); );
let module = module_asset_context let modules = sources
.process(Vc::upcast(source), ReferenceType::Undefined) .into_iter()
.module(); .map(|source| {
module_asset_context
.process(Vc::upcast(source), ReferenceType::Undefined)
.module()
.to_resolved()
})
.try_join()
.await?;
Ok(Vc::cell(if graph { Ok(Vc::cell(if graph {
to_graph(module, max_depth.unwrap_or(usize::MAX)).await? to_graph(modules, output_base, max_depth.unwrap_or(usize::MAX)).await?
} else { } else {
to_list(module).await? to_list(modules, output_base).await?
})) }))
} }
async fn to_list(asset: Vc<Box<dyn Module>>) -> Result<Vec<RcStr>> { async fn to_list(
let mut assets = vec![]; entries: Vec<ResolvedVc<Box<dyn Module>>>,
output_base: FileSystemPath,
) -> Result<Vec<RcStr>> {
let mut result = vec![];
let mut visited = FxHashSet::default(); let mut visited = FxHashSet::default();
let mut queue = Vec::new(); let mut queue = entries;
queue.push(asset);
while let Some(module) = queue.pop() {
while let Some(asset) = queue.pop() { let references = referenced_modules_and_affecting_sources(*module, false).await?;
let references = referenced_modules_and_affecting_sources(asset, false).await?; let Some(path) = output_base.get_relative_path_to(&module.ident().await?.path) else {
let path = &asset.ident().await?.path; continue;
if visited.insert(asset) { };
let path = path
.strip_prefix("./")
.map_or_else(|| path.clone(), RcStr::from);
if visited.insert(module) {
for (_, references) in references.iter().rev() { for (_, references) in references.iter().rev() {
for asset in references.modules.iter() { for module in references.modules.iter() {
queue.push(**asset); queue.push(*module);
} }
} }
} }
assets.push(path.path.clone()); result.push(path);
} }
assets.sort(); result.sort();
assets.dedup(); result.dedup();
Ok(assets) Ok(result)
} }
async fn to_graph(asset: Vc<Box<dyn Module>>, max_depth: usize) -> Result<Vec<RcStr>> { async fn to_graph(
assets: Vec<ResolvedVc<Box<dyn Module>>>,
output_base: FileSystemPath,
max_depth: usize,
) -> Result<Vec<RcStr>> {
let mut visited = FxHashSet::default(); let mut visited = FxHashSet::default();
let mut queue = Vec::new(); let mut queue: Vec<_> = assets.into_iter().map(|a| (0, a)).collect();
queue.push((0, asset));
let mut result = vec![]; let mut result = vec![];
while let Some((depth, asset)) = queue.pop() { while let Some((depth, asset)) = queue.pop() {
let references = referenced_modules_and_affecting_sources(asset, false).await?; let references = referenced_modules_and_affecting_sources(*asset, false).await?;
let mut indent = String::new(); let mut indent = String::new();
for _ in 0..depth { for _ in 0..depth {
indent.push_str(" "); indent.push_str(" ");
} }
let path = &asset.ident().await?.path; let Some(path) = &output_base.get_relative_path_to(&asset.ident().await?.path) else {
continue;
};
let path = path.strip_prefix("./").unwrap_or(path);
if visited.insert(asset) { if visited.insert(asset) {
if depth < max_depth { if depth < max_depth {
for (_, references) in references.iter().rev() { for (_, references) in references.iter().rev() {
for asset in references.modules.iter() { for asset in references.modules.iter() {
queue.push((depth + 1, **asset)); queue.push((depth + 1, *asset));
} }
} }
} }
result.push(turbofmt!("{indent}{}", path.path).await?); result.push(turbofmt!("{indent}{}", path).await?);
} else if references.is_empty() { } else if references.is_empty() {
result.push(turbofmt!("{indent}{} *", path.path).await?); result.push(turbofmt!("{indent}{} *", path).await?);
} else { } else {
result.push(turbofmt!("{indent}{} *...", path.path).await?); result.push(turbofmt!("{indent}{} *...", path).await?);
} }
} }
result.push("".into()); result.push("".into());
......
/* tslint:disable */
/* eslint-disable */
/* auto-generated by NAPI-RS */
export interface NapiNftResult {
files: Array<RcStr>
issues: Array<RcStr>
}
export declare function nodeFileTrace(projectRoot: RcStr, cwd: RcStr, outputBase: RcStr, input: Array<string>, graph: boolean, showIssues: boolean, maxDepth?: number | undefined | null): Promise<NapiNftResult>
{
"name": "@turbopack/nft",
"version": "0.1.0",
"private": true,
"scripts": {
"build-native": "napi build --platform -p turbopack-nft-napi --cargo-cwd ../../../ --cargo-name turbopack_nft_napi --js false native",
"build-native-release": "napi build --platform -p turbopack-nft-napi --cargo-cwd ../../../ --cargo-name turbopack_nft_napi --release --js false native"
},
"napi": {
"name": "turbopack-nft",
"triples": {
"defaults": true,
"additional": [
"aarch64-apple-darwin",
"aarch64-unknown-linux-gnu",
"x86_64-unknown-linux-musl",
"aarch64-unknown-linux-musl"
]
}
},
"devDependencies": {
"@napi-rs/cli": "2.18.4"
}
}
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