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

Turbopack: don't match dynamic pattern for node_modules packages (#83176)

This code would previously pull in every single package in node_modules

```
var path = require("path");

var dir = foo;
if (bar) {
  dir = process.cwd();
}
path.join(dir, "node_modules");
```

Now, `node_modules/<dynamic>` doesn't match every single package anymore. It effectively thinks that `node_modules` is an empty folder when enumerating it.
parent e657c383
use anyhow::Result; use anyhow::Result;
use either::Either;
use tracing::Instrument; use tracing::Instrument;
use turbo_rcstr::{RcStr, rcstr}; use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{ResolvedVc, ValueToString, Vc}; use turbo_tasks::{ResolvedVc, ValueToString, Vc};
...@@ -78,51 +77,42 @@ async fn resolve_reference_from_dir( ...@@ -78,51 +77,42 @@ async fn resolve_reference_from_dir(
) -> Result<Vc<ModuleResolveResult>> { ) -> Result<Vc<ModuleResolveResult>> {
let path_ref = path.await?; let path_ref = path.await?;
let (abs_path, rel_path) = path_ref.split_could_match("/ROOT/"); let (abs_path, rel_path) = path_ref.split_could_match("/ROOT/");
let matches = match (abs_path, rel_path) { if abs_path.is_none() && rel_path.is_none() {
(Some(abs_path), Some(rel_path)) => Either::Right( return Ok(*ModuleResolveResult::unresolvable());
read_matches( }
parent_path.root().owned().await?,
rcstr!("/ROOT/"), let abs_matches = if let Some(abs_path) = &abs_path {
true, Some(
Pattern::new(abs_path.or_any_nested_file()),
)
.await?
.into_iter()
.chain(
read_matches(
parent_path,
rcstr!(""),
true,
Pattern::new(rel_path.or_any_nested_file()),
)
.await?
.into_iter(),
),
),
(Some(abs_path), None) => Either::Left(
// absolute path only
read_matches( read_matches(
parent_path.root().owned().await?, parent_path.root().owned().await?,
rcstr!("/ROOT/"), rcstr!("/ROOT/"),
true, true,
Pattern::new(abs_path.or_any_nested_file()), Pattern::new(abs_path.or_any_nested_file()),
) )
.await? .await?,
.into_iter(), )
), } else {
(None, Some(rel_path)) => Either::Left( None
// relative path only };
let rel_matches = if let Some(rel_path) = &rel_path {
Some(
read_matches( read_matches(
parent_path, parent_path,
rcstr!(""), rcstr!(""),
true, true,
Pattern::new(rel_path.or_any_nested_file()), Pattern::new(rel_path.or_any_nested_file()),
) )
.await? .await?,
.into_iter(), )
), } else {
(None, None) => return Ok(*ModuleResolveResult::unresolvable()), None
}; };
let matches = abs_matches
.into_iter()
.flatten()
.chain(rel_matches.into_iter().flatten());
let mut affecting_sources = Vec::new(); let mut affecting_sources = Vec::new();
let mut results = Vec::new(); let mut results = Vec::new();
for pat_match in matches { for pat_match in matches {
...@@ -158,7 +148,7 @@ impl ModuleReference for DirAssetReference { ...@@ -158,7 +148,7 @@ impl ModuleReference for DirAssetReference {
async fn resolve_reference(&self) -> Result<Vc<ModuleResolveResult>> { async fn resolve_reference(&self) -> Result<Vc<ModuleResolveResult>> {
let parent_path = self.source.ident().path().await?.parent(); let parent_path = self.source.ident().path().await?.parent();
let span = tracing::info_span!( let span = tracing::info_span!(
"resolve DirAssetReference", "trace directory",
pattern = display(self.path.to_string().await?) pattern = display(self.path.to_string().await?)
); );
async { async {
......
use anyhow::Result; use anyhow::Result;
use tracing::Instrument;
use turbo_rcstr::RcStr; use turbo_rcstr::RcStr;
use turbo_tasks::{ResolvedVc, ValueToString, Vc}; use turbo_tasks::{ResolvedVc, ValueToString, Vc};
use turbopack_core::{ use turbopack_core::{
...@@ -28,7 +29,18 @@ impl ModuleReference for FileSourceReference { ...@@ -28,7 +29,18 @@ impl ModuleReference for FileSourceReference {
async fn resolve_reference(&self) -> Result<Vc<ModuleResolveResult>> { async fn resolve_reference(&self) -> Result<Vc<ModuleResolveResult>> {
let context_dir = self.source.ident().path().await?.parent(); let context_dir = self.source.ident().path().await?.parent();
Ok(resolve_raw(context_dir, *self.path, false).as_raw_module_result()) let span = tracing::info_span!(
"trace file",
pattern = display(self.path.to_string().await?)
);
async {
resolve_raw(context_dir, *self.path, false)
.as_raw_module_result()
.resolve()
.await
}
.instrument(span)
.await
} }
} }
......
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