Skip to content

Commit

Permalink
Lookup alias and import maps using Pattern instead of String (#68274)
Browse files Browse the repository at this point in the history
Including vercel/turborepo#8852
Fixes #63372
Fixes PACK-2801

- [x] Change `RequestKey` handling:
vercel/turborepo#8852 (comment)
- [x] Fix lookup for alternatives:
vercel/turborepo#8852 (comment)
  • Loading branch information
mischnic committed Aug 16, 2024
1 parent e89a921 commit b06c2e0
Show file tree
Hide file tree
Showing 26 changed files with 1,438 additions and 244 deletions.
9 changes: 5 additions & 4 deletions crates/next-core/src/next_edge/unsupported.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use anyhow::Result;
use indoc::formatdoc;
use turbo_tasks::{RcStr, Vc};
use turbo_tasks::Vc;
use turbo_tasks_fs::{File, FileSystemPath};
use turbopack_core::{
asset::AssetContent,
resolve::{
options::{ImportMapResult, ImportMapping, ImportMappingReplacement},
options::{ImportMapResult, ImportMappingReplacement, ReplacedImportMapping},
parse::Request,
pattern::Pattern,
ResolveResult,
},
virtual_source::VirtualSource,
Expand Down Expand Up @@ -41,8 +42,8 @@ impl NextEdgeUnsupportedModuleReplacer {
#[turbo_tasks::value_impl]
impl ImportMappingReplacement for NextEdgeUnsupportedModuleReplacer {
#[turbo_tasks::function]
fn replace(&self, _capture: RcStr) -> Vc<ImportMapping> {
ImportMapping::Ignore.into()
fn replace(&self, _capture: Vc<Pattern>) -> Vc<ReplacedImportMapping> {
ReplacedImportMapping::Ignore.into()
}

#[turbo_tasks::function]
Expand Down
15 changes: 8 additions & 7 deletions crates/next-core/src/next_font/google/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ use turbopack_core::{
issue::{IssueExt, IssueSeverity},
reference_type::{InnerAssets, ReferenceType},
resolve::{
options::{ImportMapResult, ImportMapping, ImportMappingReplacement},
options::{ImportMapResult, ImportMappingReplacement, ReplacedImportMapping},
parse::Request,
pattern::Pattern,
ResolveResult,
},
virtual_source::VirtualSource,
Expand Down Expand Up @@ -137,8 +138,8 @@ impl NextFontGoogleReplacer {
#[turbo_tasks::value_impl]
impl ImportMappingReplacement for NextFontGoogleReplacer {
#[turbo_tasks::function]
fn replace(&self, _capture: RcStr) -> Vc<ImportMapping> {
ImportMapping::Ignore.into()
fn replace(&self, _capture: Vc<Pattern>) -> Vc<ReplacedImportMapping> {
ReplacedImportMapping::Ignore.into()
}

/// Intercepts requests for `next/font/google/target.css` and returns a
Expand Down Expand Up @@ -266,8 +267,8 @@ impl NextFontGoogleCssModuleReplacer {
#[turbo_tasks::value_impl]
impl ImportMappingReplacement for NextFontGoogleCssModuleReplacer {
#[turbo_tasks::function]
fn replace(&self, _capture: RcStr) -> Vc<ImportMapping> {
ImportMapping::Ignore.into()
fn replace(&self, _capture: Vc<Pattern>) -> Vc<ReplacedImportMapping> {
ReplacedImportMapping::Ignore.into()
}

/// Intercepts requests for the css module made by the virtual JavaScript
Expand Down Expand Up @@ -318,8 +319,8 @@ impl NextFontGoogleFontFileReplacer {
#[turbo_tasks::value_impl]
impl ImportMappingReplacement for NextFontGoogleFontFileReplacer {
#[turbo_tasks::function]
fn replace(&self, _capture: RcStr) -> Vc<ImportMapping> {
ImportMapping::Ignore.into()
fn replace(&self, _capture: Vc<Pattern>) -> Vc<ReplacedImportMapping> {
ReplacedImportMapping::Ignore.into()
}

/// Intercepts requests for the font made by the CSS
Expand Down
42 changes: 21 additions & 21 deletions turbopack/crates/turbo-tasks-fs/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,62 +65,62 @@ pub fn unix_to_sys(path: &str) -> Cow<'_, str> {
}

/// Normalizes a /-separated path into a form that contains no leading /, no
/// double /, no "." seqment, no ".." seqment.
/// double /, no "." segment, no ".." segment.
///
/// Returns None if the path would need to start with ".." to be equal.
pub fn normalize_path(str: &str) -> Option<String> {
let mut seqments = Vec::new();
for seqment in str.split('/') {
match seqment {
let mut segments = Vec::new();
for segment in str.split('/') {
match segment {
"." | "" => {}
".." => {
seqments.pop()?;
segments.pop()?;
}
seqment => {
seqments.push(seqment);
segment => {
segments.push(segment);
}
}
}
Some(seqments.join("/"))
Some(segments.join("/"))
}

/// Normalizes a /-separated request into a form that contains no leading /, no
/// double /, and no "." or ".." seqments in the middle of the request. A
/// request might only start with a single "." seqment and no ".." segements, or
/// any positive number of ".." seqments but no "." seqment.
/// double /, and no "." or ".." segments in the middle of the request. A
/// request might only start with a single "." segment and no ".." segments, or
/// any positive number of ".." segments but no "." segment.
pub fn normalize_request(str: &str) -> String {
let mut seqments = vec!["."];
let mut segments = vec!["."];
// Keeps track of our directory depth so that we can pop directories when
// encountering a "..". If this is positive, then we're inside a directory
// and we can pop that. If it's 0, then we can't pop the directory and we must
// keep the ".." in our seqments. This is not the same as the seqments.len(),
// keep the ".." in our segments. This is not the same as the segments.len(),
// because we cannot pop a kept ".." when encountering another "..".
let mut depth = 0;
let mut popped_dot = false;
for seqment in str.split('/') {
match seqment {
for segment in str.split('/') {
match segment {
"." => {}
".." => {
if depth > 0 {
depth -= 1;
seqments.pop();
segments.pop();
} else {
// The first time we push a "..", we need to remove the "." we include by
// default.
if !popped_dot {
popped_dot = true;
seqments.pop();
segments.pop();
}
seqments.push(seqment);
segments.push(segment);
}
}
seqment => {
seqments.push(seqment);
segment => {
segments.push(segment);
depth += 1;
}
}
}
seqments.join("/")
segments.join("/")
}

/// Converts a disk access Result<T> into a Result<Some<T>>, where a NotFound
Expand Down
26 changes: 17 additions & 9 deletions turbopack/crates/turbopack-core/src/issue/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,24 @@ impl Issue for ResolvingIssue {
if let Some(error_message) = &self.error_message {
writeln!(description, "{error_message}")?;
}
let request_value = self.request.await?;
let request_parts = match &*request_value {
Request::Alternatives { requests } => requests.as_slice(),
_ => &[self.request],
};

if let Some(import_map) = &self.resolve_options.await?.import_map {
match lookup_import_map(*import_map, self.file_path, self.request).await {
Ok(None) => {}
Ok(Some(str)) => writeln!(description, "Import map: {}", str)?,
Err(err) => {
writeln!(
description,
"Error while looking up import map: {}",
PrettyPrintError(&err)
)?;
for request in request_parts {
match lookup_import_map(*import_map, self.file_path, *request).await {
Ok(None) => {}
Ok(Some(str)) => writeln!(description, "Import map: {}", str)?,
Err(err) => {
writeln!(
description,
"Error while looking up import map: {}",
PrettyPrintError(&err)
)?;
}
}
}
}
Expand Down
Loading

0 comments on commit b06c2e0

Please sign in to comment.