Skip to content

Commit 90bc934

Browse files
authored
[turbopack] Issue 'too many matches' warning for DirAssets also (#84768)
Same as #84701 except for DirAssets which are what we create for things like `path.resolve` or `path.join`
1 parent 87ab87b commit 90bc934

File tree

3 files changed

+218
-178
lines changed

3 files changed

+218
-178
lines changed

turbopack/crates/turbopack-ecmascript/src/references/mod.rs

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,7 @@ use self::{
104104
EsmAssetReference, EsmAsyncAssetReference, EsmExports, EsmModuleItem, ImportMetaBinding,
105105
ImportMetaRef, UrlAssetReference, export::EsmExport,
106106
},
107-
node::DirAssetReference,
108-
raw::FileSourceReference,
107+
raw::{DirAssetReference, FileSourceReference},
109108
typescript::{TsConfigReference, TsReferencePathAssetReference, TsReferenceTypeAssetReference},
110109
};
111110
use super::{
@@ -1652,6 +1651,8 @@ async fn handle_call<G: Fn(Vec<Effect>) + Send + Sync>(
16521651
.await
16531652
};
16541653

1654+
let make_issue_source =
1655+
|| IssueSource::from_swc_offsets(source, span.lo.to_u32(), span.hi.to_u32());
16551656
if new {
16561657
match func {
16571658
JsValue::WellKnownFunction(WellKnownFunctionKind::URLConstructor) => {
@@ -1987,11 +1988,7 @@ async fn handle_call<G: Fn(Vec<Effect>) + Send + Sync>(
19871988
context_dir,
19881989
Pattern::new(pat),
19891990
collect_affecting_sources,
1990-
IssueSource::from_swc_offsets(
1991-
source,
1992-
span.lo.to_u32(),
1993-
span.hi.to_u32(),
1994-
),
1991+
make_issue_source(),
19951992
)
19961993
.to_resolved()
19971994
.await?,
@@ -2043,7 +2040,7 @@ async fn handle_call<G: Fn(Vec<Effect>) + Send + Sync>(
20432040
}
20442041
if let Some(context_dir) = get_traced_project_dir().await? {
20452042
analysis.add_reference(
2046-
DirAssetReference::new(context_dir, Pattern::new(pat))
2043+
DirAssetReference::new(context_dir, Pattern::new(pat), make_issue_source())
20472044
.to_resolved()
20482045
.await?,
20492046
);
@@ -2085,7 +2082,7 @@ async fn handle_call<G: Fn(Vec<Effect>) + Send + Sync>(
20852082
}
20862083
if let Some(context_dir) = get_traced_project_dir().await? {
20872084
analysis.add_reference(
2088-
DirAssetReference::new(context_dir, Pattern::new(pat))
2085+
DirAssetReference::new(context_dir, Pattern::new(pat), make_issue_source())
20892086
.to_resolved()
20902087
.await?,
20912088
);
@@ -2376,9 +2373,13 @@ async fn handle_call<G: Fn(Vec<Effect>) + Send + Sync>(
23762373
};
23772374
if let Some(context_dir) = get_traced_project_dir().await? {
23782375
analysis.add_reference(
2379-
DirAssetReference::new(context_dir, Pattern::new(abs_pattern))
2380-
.to_resolved()
2381-
.await?,
2376+
DirAssetReference::new(
2377+
context_dir,
2378+
Pattern::new(abs_pattern),
2379+
make_issue_source(),
2380+
)
2381+
.to_resolved()
2382+
.await?,
23822383
);
23832384
}
23842385
return Ok(());
@@ -2441,9 +2442,13 @@ async fn handle_call<G: Fn(Vec<Effect>) + Send + Sync>(
24412442
};
24422443
if let Some(context_dir) = get_traced_project_dir().await? {
24432444
analysis.add_reference(
2444-
DirAssetReference::new(context_dir, Pattern::new(abs_pattern))
2445-
.to_resolved()
2446-
.await?,
2445+
DirAssetReference::new(
2446+
context_dir,
2447+
Pattern::new(abs_pattern),
2448+
make_issue_source(),
2449+
)
2450+
.to_resolved()
2451+
.await?,
24472452
);
24482453
}
24492454
return Ok(());
@@ -2510,6 +2515,7 @@ async fn handle_call<G: Fn(Vec<Effect>) + Send + Sync>(
25102515
DirAssetReference::new(
25112516
context_dir.clone(),
25122517
Pattern::new(Pattern::Constant(dir.into())),
2518+
make_issue_source(),
25132519
)
25142520
.to_resolved()
25152521
})
Lines changed: 4 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
1-
use anyhow::{Result, bail};
2-
use tracing::Instrument;
3-
use turbo_rcstr::{RcStr, rcstr};
1+
use anyhow::Result;
2+
use turbo_rcstr::RcStr;
43
use turbo_tasks::{ResolvedVc, ValueToString, Vc};
54
use turbo_tasks_fs::FileSystemPath;
65
use turbopack_core::{
7-
chunk::{ChunkableModuleReference, ChunkingType, ChunkingTypeOption},
8-
file_source::FileSource,
9-
raw_module::RawModule,
10-
reference::ModuleReference,
11-
resolve::{
12-
ModuleResolveResult, RequestKey,
13-
pattern::{Pattern, PatternMatch, read_matches},
14-
},
6+
file_source::FileSource, raw_module::RawModule, reference::ModuleReference,
7+
resolve::ModuleResolveResult,
158
};
169

1710
#[turbo_tasks::value]
@@ -53,125 +46,3 @@ impl ValueToString for PackageJsonReference {
5346
))
5447
}
5548
}
56-
57-
#[turbo_tasks::value]
58-
#[derive(Hash, Debug)]
59-
pub struct DirAssetReference {
60-
pub context_dir: FileSystemPath,
61-
pub path: ResolvedVc<Pattern>,
62-
}
63-
64-
#[turbo_tasks::value_impl]
65-
impl DirAssetReference {
66-
#[turbo_tasks::function]
67-
pub fn new(context_dir: FileSystemPath, path: ResolvedVc<Pattern>) -> Vc<Self> {
68-
Self::cell(DirAssetReference { context_dir, path })
69-
}
70-
}
71-
72-
async fn resolve_reference_from_dir(
73-
context_dir: FileSystemPath,
74-
path: Vc<Pattern>,
75-
) -> Result<Vc<ModuleResolveResult>> {
76-
let path_ref = path.await?;
77-
let (abs_path, rel_path) = path_ref.split_could_match("/ROOT/");
78-
if abs_path.is_none() && rel_path.is_none() {
79-
return Ok(*ModuleResolveResult::unresolvable());
80-
}
81-
82-
let abs_matches = if let Some(abs_path) = &abs_path {
83-
Some(
84-
read_matches(
85-
context_dir.root().owned().await?,
86-
rcstr!("/ROOT/"),
87-
true,
88-
Pattern::new(abs_path.or_any_nested_file()),
89-
)
90-
.await?,
91-
)
92-
} else {
93-
None
94-
};
95-
let rel_matches = if let Some(rel_path) = &rel_path {
96-
Some(
97-
read_matches(
98-
context_dir,
99-
rcstr!(""),
100-
true,
101-
Pattern::new(rel_path.or_any_nested_file()),
102-
)
103-
.await?,
104-
)
105-
} else {
106-
None
107-
};
108-
109-
let matches = abs_matches
110-
.into_iter()
111-
.flatten()
112-
.chain(rel_matches.into_iter().flatten());
113-
114-
let mut affecting_sources = Vec::new();
115-
let mut results = Vec::new();
116-
for pat_match in matches {
117-
match pat_match {
118-
PatternMatch::File(matched_path, file) => {
119-
let realpath = file.realpath_with_links().await?;
120-
for symlink in &realpath.symlinks {
121-
affecting_sources.push(ResolvedVc::upcast(
122-
FileSource::new(symlink.clone()).to_resolved().await?,
123-
));
124-
}
125-
let path: FileSystemPath = match &realpath.path_result {
126-
Ok(path) => path.clone(),
127-
Err(e) => bail!(e.as_error_message(file, &realpath)),
128-
};
129-
results.push((
130-
RequestKey::new(matched_path.clone()),
131-
ResolvedVc::upcast(
132-
RawModule::new(Vc::upcast(FileSource::new(path)))
133-
.to_resolved()
134-
.await?,
135-
),
136-
));
137-
}
138-
PatternMatch::Directory(..) => {}
139-
}
140-
}
141-
Ok(*ModuleResolveResult::modules_with_affecting_sources(
142-
results,
143-
affecting_sources,
144-
))
145-
}
146-
147-
#[turbo_tasks::value_impl]
148-
impl ModuleReference for DirAssetReference {
149-
#[turbo_tasks::function]
150-
async fn resolve_reference(&self) -> Result<Vc<ModuleResolveResult>> {
151-
let span = tracing::info_span!(
152-
"trace directory",
153-
pattern = display(self.path.to_string().await?)
154-
);
155-
resolve_reference_from_dir(self.context_dir.clone(), *self.path)
156-
.instrument(span)
157-
.await
158-
}
159-
}
160-
161-
#[turbo_tasks::value_impl]
162-
impl ChunkableModuleReference for DirAssetReference {
163-
#[turbo_tasks::function]
164-
fn chunking_type(&self) -> Vc<ChunkingTypeOption> {
165-
Vc::cell(Some(ChunkingType::Traced))
166-
}
167-
}
168-
169-
#[turbo_tasks::value_impl]
170-
impl ValueToString for DirAssetReference {
171-
#[turbo_tasks::function]
172-
async fn to_string(&self) -> Result<Vc<RcStr>> {
173-
Ok(Vc::cell(
174-
format!("directory assets {}", self.path.to_string().await?,).into(),
175-
))
176-
}
177-
}

0 commit comments

Comments
 (0)