Unverified Commit a15b48c7 authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

Turbopack: support ignore comments for NFT fs access tracing (#78460)

Support `readFileSync(/* turbopackIgnore: true */ path, 'utf8')` (and same for `path.resolve` and `path.join`) to ignore it during NFT tracing

We already had the infrastructure, just member calls and then specifically the NFT access functions needed to be handled.

Would fix the failing test in https://github.com/vercel/next.js/pull/78358
Part of PACK-4426
parent f0d81f14
...@@ -122,9 +122,11 @@ impl Pattern { ...@@ -122,9 +122,11 @@ impl Pattern {
} }
} }
/// Whether the pattern has any significant constant parts (everything except `/`).
/// E.g. `<dynamic>/<dynamic>` doesn't really have constant parts
pub fn has_constant_parts(&self) -> bool { pub fn has_constant_parts(&self) -> bool {
match self { match self {
Pattern::Constant(_) => true, Pattern::Constant(str) => str != "/",
Pattern::Dynamic => false, Pattern::Dynamic => false,
Pattern::Alternatives(list) | Pattern::Concatenation(list) => { Pattern::Alternatives(list) | Pattern::Concatenation(list) => {
list.iter().any(|p| p.has_constant_parts()) list.iter().any(|p| p.has_constant_parts())
......
...@@ -697,14 +697,11 @@ impl Visit for Analyzer<'_> { ...@@ -697,14 +697,11 @@ impl Visit for Analyzer<'_> {
// we could actually unwrap thanks to the optimisation above but it can't hurt to be safe... // we could actually unwrap thanks to the optimisation above but it can't hurt to be safe...
if let Some(comments) = self.comments { if let Some(comments) = self.comments {
let callee_span = match &n.callee { let callee_span = match &n.callee {
Callee::Import(Import { span, .. }) => Some(span), Callee::Import(Import { span, .. }) => Some(*span),
Callee::Expr(box Expr::Ident(Ident { span, sym, .. })) if sym == "require" => { Callee::Expr(e) => Some(e.span()),
Some(span)
}
_ => None, _ => None,
}; };
// we are interested here in the last comment with a valid directive
let ignore_directive = parse_ignore_directive(comments, n.args.first()); let ignore_directive = parse_ignore_directive(comments, n.args.first());
if let Some((callee_span, ignore_directive)) = callee_span.zip(ignore_directive) { if let Some((callee_span, ignore_directive)) = callee_span.zip(ignore_directive) {
......
...@@ -1820,7 +1820,7 @@ impl JsValue { ...@@ -1820,7 +1820,7 @@ impl JsValue {
"The Node.js process.cwd method: https://nodejs.org/api/process.html#processcwd", "The Node.js process.cwd method: https://nodejs.org/api/process.html#processcwd",
), ),
WellKnownFunctionKind::NodePreGypFind => ( WellKnownFunctionKind::NodePreGypFind => (
"find".to_string(), "binary.find".to_string(),
"The Node.js @mapbox/node-pre-gyp module: https://github.com/mapbox/node-pre-gyp", "The Node.js @mapbox/node-pre-gyp module: https://github.com/mapbox/node-pre-gyp",
), ),
WellKnownFunctionKind::NodeGypBuild => ( WellKnownFunctionKind::NodeGypBuild => (
......
...@@ -1312,7 +1312,7 @@ pub(crate) async fn analyse_ecmascript_module_internal( ...@@ -1312,7 +1312,7 @@ pub(crate) async fn analyse_ecmascript_module_internal(
let func = analysis_state let func = analysis_state
.link_value( .link_value(
JsValue::member(Box::new(obj.clone()), Box::new(prop)), JsValue::member(Box::new(obj.clone()), Box::new(prop)),
ImportAttributes::empty_ref(), eval_context.imports.get_attributes(span),
) )
.await?; .await?;
...@@ -2875,7 +2875,7 @@ async fn value_visitor_inner( ...@@ -2875,7 +2875,7 @@ async fn value_visitor_inner(
), ),
_, _,
) => { ) => {
// TODO: figure out how to do static analysis without invalidating the while // TODO: figure out how to do static analysis without invalidating the whole
// analysis when a new file gets added // analysis when a new file gets added
v.into_unknown( v.into_unknown(
true, true,
...@@ -2903,6 +2903,20 @@ async fn value_visitor_inner( ...@@ -2903,6 +2903,20 @@ async fn value_visitor_inner(
v.into_unknown(true, "new non constant") v.into_unknown(true, "new non constant")
} }
} }
JsValue::WellKnownFunction(
WellKnownFunctionKind::PathJoin
| WellKnownFunctionKind::PathResolve(_)
| WellKnownFunctionKind::FsReadMethod(_),
) => {
if ignore {
return Ok((
JsValue::unknown(v, true, "ignored well known function"),
true,
));
} else {
return Ok((v, false));
}
}
JsValue::FreeVar(ref kind) => match &**kind { JsValue::FreeVar(ref kind) => match &**kind {
"__dirname" => as_abs_path(origin.origin_path().parent()).await?, "__dirname" => as_abs_path(origin.origin_path().parent()).await?,
"__filename" => as_abs_path(origin.origin_path()).await?, "__filename" => as_abs_path(origin.origin_path()).await?,
......
...@@ -184,7 +184,9 @@ pub fn module_value_to_well_known_object(module_value: &ModuleValue) -> Option<J ...@@ -184,7 +184,9 @@ pub fn module_value_to_well_known_object(module_value: &ModuleValue) -> Option<J
} }
"node:os" | "os" => JsValue::WellKnownObject(WellKnownObjectKind::OsModule), "node:os" | "os" => JsValue::WellKnownObject(WellKnownObjectKind::OsModule),
"node:process" | "process" => JsValue::WellKnownObject(WellKnownObjectKind::NodeProcess), "node:process" | "process" => JsValue::WellKnownObject(WellKnownObjectKind::NodeProcess),
"@mapbox/node-pre-gyp" => JsValue::WellKnownObject(WellKnownObjectKind::NodePreGyp), "node-pre-gyp" | "@mapbox/node-pre-gyp" => {
JsValue::WellKnownObject(WellKnownObjectKind::NodePreGyp)
}
"node-gyp-build" => JsValue::WellKnownFunction(WellKnownFunctionKind::NodeGypBuild), "node-gyp-build" => JsValue::WellKnownFunction(WellKnownFunctionKind::NodeGypBuild),
"node:bindings" | "bindings" => { "node:bindings" | "bindings" => {
JsValue::WellKnownFunction(WellKnownFunctionKind::NodeBindings) JsValue::WellKnownFunction(WellKnownFunctionKind::NodeBindings)
......
...@@ -668,6 +668,9 @@ async fn externals_tracing_module_context(ty: ExternalType) -> Result<Vc<ModuleA ...@@ -668,6 +668,9 @@ async fn externals_tracing_module_context(ty: ExternalType) -> Result<Vc<ModuleA
Ok(ModuleAssetContext::new_without_replace_externals( Ok(ModuleAssetContext::new_without_replace_externals(
Default::default(), Default::default(),
CompileTimeInfo::builder(env).cell().await?, CompileTimeInfo::builder(env).cell().await?,
// Keep these options more or less in sync with
// turbopack/crates/turbopack/tests/node-file-trace.rs to ensure that the NFT unit tests
// are actually representative of what Turbopack does.
ModuleOptionsContext { ModuleOptionsContext {
ecmascript: EcmascriptOptionsContext { ecmascript: EcmascriptOptionsContext {
source_maps: SourceMapsType::None, source_maps: SourceMapsType::None,
......
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