diff --git a/turbopack/crates/turbopack-css/src/process.rs b/turbopack/crates/turbopack-css/src/process.rs index 7233bd1dddb31..f967149243ee8 100644 --- a/turbopack/crates/turbopack-css/src/process.rs +++ b/turbopack/crates/turbopack-css/src/process.rs @@ -19,7 +19,7 @@ use swc_core::{ }; use tracing::Instrument; use turbo_rcstr::RcStr; -use turbo_tasks::{FxIndexMap, ResolvedVc, ValueToString, Vc}; +use turbo_tasks::{FxIndexMap, ResolvedVc, TryJoinIterExt, ValueToString, Vc}; use turbo_tasks_fs::{FileContent, FileSystemPath}; use turbopack_core::{ asset::{Asset, AssetContent}, @@ -113,7 +113,7 @@ impl StyleSheetLike<'_, '_> { /// Multiple [ModuleReference]s #[turbo_tasks::value(transparent)] -pub struct UnresolvedUrlReferences(pub Vec<(String, Vc)>); +pub struct UnresolvedUrlReferences(pub Vec<(String, ResolvedVc)>); #[turbo_tasks::value(shared, serialization = "none", eq = "manual", cell = "new")] pub enum ParseCssResult { @@ -247,7 +247,7 @@ pub async fn finalize_css( let mut url_map = HashMap::new(); for (src, reference) in (*url_references.await?).iter() { - let resolved = resolve_url_reference(*reference, chunking_context).await?; + let resolved = resolve_url_reference(**reference, chunking_context).await?; if let Some(v) = resolved.as_ref().cloned() { url_map.insert(RcStr::from(src.as_str()), v); } @@ -315,7 +315,7 @@ pub async fn parse_css( process_content( **file_content, string.into_owned(), - fs_path, + fs_path.to_resolved().await?, ident_str, source, origin, @@ -335,7 +335,7 @@ pub async fn parse_css( async fn process_content( content_vc: Vc, code: String, - fs_path_vc: Vc, + fs_path_vc: ResolvedVc, filename: &str, source: Vc>, origin: Vc>, @@ -399,23 +399,33 @@ async fn process_content( } } - for err in warnings.read().unwrap().iter() { + // We need to collect here because we need to avoid holding the lock while calling + // `.await` in the loop. + let warngins = warnings.read().unwrap().iter().cloned().collect::>(); + for err in warngins.iter() { match err.kind { lightningcss::error::ParserError::UnexpectedToken(_) | lightningcss::error::ParserError::UnexpectedImportRule | lightningcss::error::ParserError::SelectorError(..) | lightningcss::error::ParserError::EndOfInput => { - let source = err.loc.as_ref().map(|loc| { - let pos = SourcePos { - line: loc.line as _, - column: loc.column as _, - }; - IssueSource::from_line_col(source, pos, pos) - }); + let source = match &err.loc { + Some(loc) => { + let pos = SourcePos { + line: loc.line as _, + column: loc.column as _, + }; + Some( + IssueSource::from_line_col(source, pos, pos) + .to_resolved() + .await?, + ) + } + None => None, + }; ParsingIssue { file: fs_path_vc, - msg: Vc::cell(err.to_string().into()), + msg: ResolvedVc::cell(err.to_string().into()), source, } .cell() @@ -432,16 +442,23 @@ async fn process_content( stylesheet_into_static(&ss, without_warnings(config.clone())) } Err(e) => { - let source = e.loc.as_ref().map(|loc| { - let pos = SourcePos { - line: loc.line as _, - column: loc.column as _, - }; - IssueSource::from_line_col(source, pos, pos) - }); + let source = match &e.loc { + Some(loc) => { + let pos = SourcePos { + line: loc.line as _, + column: loc.column as _, + }; + Some( + IssueSource::from_line_col(source, pos, pos) + .to_resolved() + .await?, + ) + } + None => None, + }; ParsingIssue { file: fs_path_vc, - msg: Vc::cell(e.to_string().into()), + msg: ResolvedVc::cell(e.to_string().into()), source, } .cell() @@ -457,6 +474,12 @@ async fn process_content( let (references, url_references) = analyze_references(&mut stylesheet, source, origin, import_context)?; + let url_references = url_references + .into_iter() + .map(|(k, v)| async move { Ok((k, v.to_resolved().await?)) }) + .try_join() + .await?; + Ok(ParseCssResult::Ok { code: content_vc.to_resolved().await?, stylesheet, @@ -485,12 +508,14 @@ enum CssError { } impl CssError { - fn report(self, file: Vc) { + fn report(self, file: ResolvedVc) { match self { CssError::LightningCssSelectorInModuleNotPure { selector } => { ParsingIssue { file, - msg: Vc::cell(format!("{CSS_MODULE_ERROR}, (lightningcss, {selector})").into()), + msg: ResolvedVc::cell( + format!("{CSS_MODULE_ERROR}, (lightningcss, {selector})").into(), + ), source: None, } .cell() @@ -646,16 +671,16 @@ impl GenerateSourceMap for ParseCssResultSourceMap { #[turbo_tasks::value] struct ParsingIssue { - msg: Vc, - file: Vc, - source: Option>, + msg: ResolvedVc, + file: ResolvedVc, + source: Option>, } #[turbo_tasks::value_impl] impl Issue for ParsingIssue { #[turbo_tasks::function] fn file_path(&self) -> Vc { - self.file + *self.file } #[turbo_tasks::function] @@ -670,7 +695,7 @@ impl Issue for ParsingIssue { #[turbo_tasks::function] fn source(&self) -> Vc { - Vc::cell(self.source.map(|s| s.resolve_source_map(self.file))) + Vc::cell(self.source.map(|s| s.resolve_source_map(*self.file))) } #[turbo_tasks::function]