Unverified Commit 2fdb231e authored by Luke Sandberg's avatar Luke Sandberg Committed by GitHub

[turbopack] Avoid returning multiple requests when performing file extension substitution (#86520)

If we modify user patterns with filename extension alternates, we should ensure we only produce one result.

e.g. given

```
src/
     foo.js
     foo.ts
     foo.json
```

If `fullySpecified=false` then `import foo` should produce `foo.js`, not `foo.js` and `foo.json`

similarly if your tsconfig sets `"moduleResolution":"nodeNext"` and you import `foo.js` it should produce `foo.ts`, but not `[foo.ts, foo.js]`.

The core problem is that we expand a user supplied pattern with alternates and then we need to only produce matches that 'correspond' to the original pattern.  To accomplish this I implemented an 'undo stack' so that when we examine a match produced by `read_matches` we can transform the request key back into something that matches the original pattern.

This neatly solves the issue of accidentally producing multiple matches with the same base filename.  Consider the pattern `./foo.js`, we expand the pattern to `./foo.{ts,tsx,js}` and then produce two matches `[(./foo.ts, foo.ts), (./foo.js, foo.js)]` our undo stack will transform the `./foo.ts` key back into `./foo.js` and then the alternate merging logic will pick the first one for each key!

The nice thing about the 'key' based approach is that it does the right thing for dynamic requests as well.

Finally, the new 'undo stack' based approach is much more composable than the old 'sequence of conditions' based approach so now it is trivial to produce all matches in parallel.
parent 558caf1d
...@@ -1229,7 +1229,10 @@ impl Pattern { ...@@ -1229,7 +1229,10 @@ impl Pattern {
/// Calls `cb` on all constants that are at the end of the pattern and /// Calls `cb` on all constants that are at the end of the pattern and
/// replaces the given final constant with the returned pattern. Returns /// replaces the given final constant with the returned pattern. Returns
/// true if replacements were performed. /// true if replacements were performed.
pub fn replace_final_constants(&mut self, cb: &impl Fn(&RcStr) -> Option<Pattern>) -> bool { pub fn replace_final_constants(
&mut self,
cb: &mut impl FnMut(&RcStr) -> Option<Pattern>,
) -> bool {
let mut replaced = false; let mut replaced = false;
match self { match self {
Pattern::Constant(c) => { Pattern::Constant(c) => {
...@@ -1524,6 +1527,7 @@ impl PatternMatch { ...@@ -1524,6 +1527,7 @@ impl PatternMatch {
// TODO this isn't super efficient // TODO this isn't super efficient
// avoid storing a large list of matches // avoid storing a large list of matches
#[turbo_tasks::value(transparent)] #[turbo_tasks::value(transparent)]
#[derive(Debug)]
pub struct PatternMatches(Vec<PatternMatch>); pub struct PatternMatches(Vec<PatternMatch>);
/// Find all files or directories that match the provided `pattern` with the /// Find all files or directories that match the provided `pattern` with the
...@@ -2492,12 +2496,12 @@ mod tests { ...@@ -2492,12 +2496,12 @@ mod tests {
#[test] #[test]
fn replace_final_constants() { fn replace_final_constants() {
fn f(mut p: Pattern, cb: &impl Fn(&RcStr) -> Option<Pattern>) -> Pattern { fn f(mut p: Pattern, cb: &mut impl FnMut(&RcStr) -> Option<Pattern>) -> Pattern {
p.replace_final_constants(cb); p.replace_final_constants(cb);
p p
} }
let js_to_ts_tsx = |c: &RcStr| -> Option<Pattern> { let mut js_to_ts_tsx = |c: &RcStr| -> Option<Pattern> {
c.strip_suffix(".js").map(|rest| { c.strip_suffix(".js").map(|rest| {
let new_ending = Pattern::Alternatives(vec![ let new_ending = Pattern::Alternatives(vec![
Pattern::Constant(rcstr!(".ts")), Pattern::Constant(rcstr!(".ts")),
...@@ -2523,7 +2527,7 @@ mod tests { ...@@ -2523,7 +2527,7 @@ mod tests {
Pattern::Constant(rcstr!(".node")), Pattern::Constant(rcstr!(".node")),
]) ])
]), ]),
&js_to_ts_tsx &mut js_to_ts_tsx
), ),
Pattern::Concatenation(vec![ Pattern::Concatenation(vec![
Pattern::Constant(rcstr!(".")), Pattern::Constant(rcstr!(".")),
...@@ -2546,7 +2550,7 @@ mod tests { ...@@ -2546,7 +2550,7 @@ mod tests {
Pattern::Constant(rcstr!("/")), Pattern::Constant(rcstr!("/")),
Pattern::Constant(rcstr!("abc.js")), Pattern::Constant(rcstr!("abc.js")),
]), ]),
&js_to_ts_tsx &mut js_to_ts_tsx
), ),
Pattern::Concatenation(vec![ Pattern::Concatenation(vec![
Pattern::Constant(rcstr!(".")), Pattern::Constant(rcstr!(".")),
......
...@@ -40,6 +40,7 @@ use turbopack::{ ...@@ -40,6 +40,7 @@ use turbopack::{
}, },
}; };
use turbopack_core::{ use turbopack_core::{
chunk::SourceMapsType,
compile_time_info::CompileTimeInfo, compile_time_info::CompileTimeInfo,
context::AssetContext, context::AssetContext,
environment::{Environment, ExecutionEnvironment, NodeJsEnvironment}, environment::{Environment, ExecutionEnvironment, NodeJsEnvironment},
...@@ -373,10 +374,12 @@ async fn node_file_trace_operation( ...@@ -373,10 +374,12 @@ async fn node_file_trace_operation(
enable_typescript_transform: Some( enable_typescript_transform: Some(
TypescriptTransformOptions::default().resolved_cell(), TypescriptTransformOptions::default().resolved_cell(),
), ),
// enable_types is required here to ensure .d.ts files are collected.
enable_types: true, enable_types: true,
..Default::default() ..Default::default()
}, },
css: CssOptionsContext { css: CssOptionsContext {
source_maps: SourceMapsType::None,
enable_raw_css: true, enable_raw_css: true,
..Default::default() ..Default::default()
}, },
......
...@@ -21,6 +21,7 @@ use turbopack::{ ...@@ -21,6 +21,7 @@ use turbopack::{
}, },
}; };
use turbopack_core::{ use turbopack_core::{
chunk::SourceMapsType,
compile_time_info::CompileTimeInfo, compile_time_info::CompileTimeInfo,
context::AssetContext, context::AssetContext,
environment::{Environment, ExecutionEnvironment, NodeJsEnvironment}, environment::{Environment, ExecutionEnvironment, NodeJsEnvironment},
...@@ -212,6 +213,7 @@ async fn node_file_trace_operation(package_root: RcStr, input: RcStr) -> Result< ...@@ -212,6 +213,7 @@ async fn node_file_trace_operation(package_root: RcStr, input: RcStr) -> Result<
..Default::default() ..Default::default()
}, },
css: CssOptionsContext { css: CssOptionsContext {
source_maps: SourceMapsType::None,
enable_raw_css: true, enable_raw_css: true,
..Default::default() ..Default::default()
}, },
......
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