Unverified Commit 5bbd5ddf authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

Turbopack: improve Lightning CSS error handling (#82561)

Don't return the `Result:Err`, but turn into an issue and return `Unparseable`

Also fixes the location translation: Lightning CSS uses 1-based columns, while Turbopack uses 0-based column indices.
parent 39fffcc3
...@@ -415,11 +415,11 @@ describe('ReactRefreshLogBox app', () => { ...@@ -415,11 +415,11 @@ describe('ReactRefreshLogBox app', () => {
if (isTurbopack) { if (isTurbopack) {
await expect(browser).toDisplayRedbox(` await expect(browser).toDisplayRedbox(`
{ {
"description": "Parsing css source code failed", "description": "Parsing CSS source code failed",
"environmentLabel": null, "environmentLabel": null,
"label": "Build Error", "label": "Build Error",
"source": "./index.module.css (1:9) "source": "./index.module.css (1:8)
Parsing css source code failed Parsing CSS source code failed
> 1 | .button > 1 | .button
| ^", | ^",
"stack": [], "stack": [],
...@@ -447,11 +447,11 @@ describe('ReactRefreshLogBox app', () => { ...@@ -447,11 +447,11 @@ describe('ReactRefreshLogBox app', () => {
if (isTurbopack) { if (isTurbopack) {
await expect(browser).toDisplayRedbox(` await expect(browser).toDisplayRedbox(`
{ {
"description": "Parsing css source code failed", "description": "Transforming CSS failed",
"environmentLabel": null, "environmentLabel": null,
"label": "Build Error", "label": "Build Error",
"source": "./index.module.css "source": "./index.module.css
Parsing css source code failed Transforming CSS failed
Selector "button" is not pure. Pure selectors must contain at least one local class or id. Selector "button" is not pure. Pure selectors must contain at least one local class or id.
Import traces: Import traces:
Client Component Browser: Client Component Browser:
......
...@@ -578,11 +578,11 @@ describe('ReactRefreshLogBox', () => { ...@@ -578,11 +578,11 @@ describe('ReactRefreshLogBox', () => {
if (isTurbopack) { if (isTurbopack) {
await expect(browser).toDisplayRedbox(` await expect(browser).toDisplayRedbox(`
{ {
"description": "Parsing css source code failed", "description": "Parsing CSS source code failed",
"environmentLabel": null, "environmentLabel": null,
"label": "Build Error", "label": "Build Error",
"source": "./index.module.css (1:9) "source": "./index.module.css (1:8)
Parsing css source code failed Parsing CSS source code failed
> 1 | .button > 1 | .button
| ^", | ^",
"stack": [], "stack": [],
...@@ -610,11 +610,11 @@ describe('ReactRefreshLogBox', () => { ...@@ -610,11 +610,11 @@ describe('ReactRefreshLogBox', () => {
if (isTurbopack) { if (isTurbopack) {
await expect(browser).toDisplayRedbox(` await expect(browser).toDisplayRedbox(`
{ {
"description": "Parsing css source code failed", "description": "Transforming CSS failed",
"environmentLabel": null, "environmentLabel": null,
"label": "Build Error", "label": "Build Error",
"source": "./index.module.css "source": "./index.module.css
Parsing css source code failed Transforming CSS failed
Selector "button" is not pure. Pure selectors must contain at least one local class or id. Selector "button" is not pure. Pure selectors must contain at least one local class or id.
Import traces: Import traces:
Browser: Browser:
......
...@@ -27,7 +27,7 @@ describe('app dir - css', () => { ...@@ -27,7 +27,7 @@ describe('app dir - css', () => {
// css-loader does not report an error for this case // css-loader does not report an error for this case
expect(source).toMatchInlineSnapshot(` expect(source).toMatchInlineSnapshot(`
"./app/global.scss.css (45:1) "./app/global.scss.css (45:1)
Parsing css source code failed Parsing CSS source code failed
43 | } 43 | }
44 | 44 |
> 45 | input.defaultCheckbox::before path { > 45 | input.defaultCheckbox::before path {
...@@ -36,7 +36,7 @@ describe('app dir - css', () => { ...@@ -36,7 +36,7 @@ describe('app dir - css', () => {
47 | } 47 | }
48 | 48 |
Pseudo-elements like '::before' or '::after' can't be followed by selectors like 'Ident("path")' at [project]/app/global.scss.css:0:884 Pseudo-elements like '::before' or '::after' can't be followed by selectors like 'Ident("path")'
Import trace: Import trace:
Client Component Browser: Client Component Browser:
......
...@@ -767,8 +767,8 @@ async fn into_plain_trace(traces: Vec<Vec<ReadRef<AssetIdent>>>) -> Result<Vec<P ...@@ -767,8 +767,8 @@ async fn into_plain_trace(traces: Vec<Vec<ReadRef<AssetIdent>>>) -> Result<Vec<P
Ok(plain_traces) Ok(plain_traces)
} }
#[turbo_tasks::value(shared, serialization = "none")] #[turbo_tasks::value(shared)]
#[derive(Clone, Debug, PartialOrd, Ord, DeterministicHash, Serialize)] #[derive(Clone, Debug, PartialOrd, Ord, DeterministicHash)]
pub enum IssueStage { pub enum IssueStage {
Config, Config,
AppStructure, AppStructure,
......
use std::sync::{Arc, RwLock}; use std::sync::{Arc, RwLock};
use anyhow::{Context, Result, bail}; use anyhow::{Result, bail};
use lightningcss::{ use lightningcss::{
css_modules::{CssModuleExport, CssModuleExports, Pattern, Segment}, css_modules::{CssModuleExport, CssModuleExports, Pattern, Segment},
stylesheet::{MinifyOptions, ParserOptions, PrinterOptions, StyleSheet, ToCssResult}, stylesheet::{MinifyOptions, ParserOptions, PrinterOptions, StyleSheet, ToCssResult},
...@@ -466,8 +466,8 @@ async fn process_content( ...@@ -466,8 +466,8 @@ async fn process_content(
// We need to collect here because we need to avoid holding the lock while calling // We need to collect here because we need to avoid holding the lock while calling
// `.await` in the loop. // `.await` in the loop.
let warngins = warnings.read().unwrap().iter().cloned().collect::<Vec<_>>(); let warnings = warnings.read().unwrap().iter().cloned().collect::<Vec<_>>();
for err in warngins.iter() { for err in warnings.iter() {
match err.kind { match err.kind {
lightningcss::error::ParserError::UnexpectedToken(_) lightningcss::error::ParserError::UnexpectedToken(_)
| lightningcss::error::ParserError::UnexpectedImportRule | lightningcss::error::ParserError::UnexpectedImportRule
...@@ -477,7 +477,7 @@ async fn process_content( ...@@ -477,7 +477,7 @@ async fn process_content(
Some(loc) => { Some(loc) => {
let pos = SourcePos { let pos = SourcePos {
line: loc.line as _, line: loc.line as _,
column: loc.column as _, column: (loc.column - 1) as _,
}; };
IssueSource::from_line_col(source, pos, pos) IssueSource::from_line_col(source, pos, pos)
} }
...@@ -485,7 +485,8 @@ async fn process_content( ...@@ -485,7 +485,8 @@ async fn process_content(
}; };
ParsingIssue { ParsingIssue {
msg: err.to_string().into(), msg: err.kind.to_string().into(),
stage: IssueStage::Parse,
source, source,
} }
.resolved_cell() .resolved_cell()
...@@ -506,13 +507,30 @@ async fn process_content( ...@@ -506,13 +507,30 @@ async fn process_content(
// minify() is actually transform, and it performs operations like CSS modules // minify() is actually transform, and it performs operations like CSS modules
// handling. // handling.
// //
//
// See: https://github.com/parcel-bundler/lightningcss/issues/935#issuecomment-2739325537 // See: https://github.com/parcel-bundler/lightningcss/issues/935#issuecomment-2739325537
ss.minify(MinifyOptions { if let Err(e) = ss.minify(MinifyOptions {
targets, targets,
..Default::default() ..Default::default()
}) }) {
.context("failed to transform css")?; let source = match &e.loc {
Some(loc) => {
let pos = SourcePos {
line: loc.line as _,
column: (loc.column - 1) as _,
};
IssueSource::from_line_col(source, pos, pos)
}
None => IssueSource::from_source_only(source),
};
ParsingIssue {
msg: e.kind.to_string().into(),
stage: IssueStage::Transform,
source,
}
.resolved_cell()
.emit();
return Ok(ParseCssResult::Unparsable.cell());
}
stylesheet_into_static(&ss, without_warnings(config.clone())) stylesheet_into_static(&ss, without_warnings(config.clone()))
} }
...@@ -521,14 +539,15 @@ async fn process_content( ...@@ -521,14 +539,15 @@ async fn process_content(
Some(loc) => { Some(loc) => {
let pos = SourcePos { let pos = SourcePos {
line: loc.line as _, line: loc.line as _,
column: loc.column as _, column: (loc.column - 1) as _,
}; };
IssueSource::from_line_col(source, pos, pos) IssueSource::from_line_col(source, pos, pos)
} }
None => IssueSource::from_source_only(source), None => IssueSource::from_source_only(source),
}; };
ParsingIssue { ParsingIssue {
msg: e.to_string().into(), msg: e.kind.to_string().into(),
stage: IssueStage::Parse,
source, source,
} }
.resolved_cell() .resolved_cell()
...@@ -581,6 +600,7 @@ impl CssError { ...@@ -581,6 +600,7 @@ impl CssError {
least one local class or id." least one local class or id."
) )
.into(), .into(),
stage: IssueStage::Transform,
// TODO: This should include the location of the selector in the file. // TODO: This should include the location of the selector in the file.
source: IssueSource::from_source_only(source), source: IssueSource::from_source_only(source),
} }
...@@ -680,6 +700,7 @@ fn generate_css_source_map(source_map: &parcel_sourcemap::SourceMap) -> Result<R ...@@ -680,6 +700,7 @@ fn generate_css_source_map(source_map: &parcel_sourcemap::SourceMap) -> Result<R
#[turbo_tasks::value] #[turbo_tasks::value]
struct ParsingIssue { struct ParsingIssue {
msg: RcStr, msg: RcStr,
stage: IssueStage,
source: IssueSource, source: IssueSource,
} }
...@@ -692,12 +713,17 @@ impl Issue for ParsingIssue { ...@@ -692,12 +713,17 @@ impl Issue for ParsingIssue {
#[turbo_tasks::function] #[turbo_tasks::function]
fn stage(&self) -> Vc<IssueStage> { fn stage(&self) -> Vc<IssueStage> {
IssueStage::Parse.cell() self.stage.clone().cell()
} }
#[turbo_tasks::function] #[turbo_tasks::function]
fn title(&self) -> Vc<StyledString> { fn title(&self) -> Vc<StyledString> {
StyledString::Text(rcstr!("Parsing css source code failed")).cell() StyledString::Text(match self.stage {
IssueStage::Parse => rcstr!("Parsing CSS source code failed"),
IssueStage::Transform => rcstr!("Transforming CSS failed"),
_ => rcstr!("CSS processing failed"),
})
.cell()
} }
#[turbo_tasks::function] #[turbo_tasks::function]
......
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