Unverified Commit 9e4e6d6f authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

Turbopack: fix CSS dependency locations (#89761)

For some locations LightningCSS is 1-indexed (but not all of them, the rustdoc describes it though)
IssueSource is always 0-indexed

Previously, it looked like this
```
Error: Turbopack build failed with 1 errors:
./bench/basic-app/app/[variants]/styles.css:7:12
Module not found: Can't resolve './test1.png'
  5 | .style {
  6 |   cursor: url(./test1.png);
> 7 | }
    |  ^
  8 |
```
parent 1fac9a1b
...@@ -1508,10 +1508,10 @@ describe('ReactRefreshLogBox app', () => { ...@@ -1508,10 +1508,10 @@ describe('ReactRefreshLogBox app', () => {
"description": "Module not found: Can't resolve './boom.css'", "description": "Module not found: Can't resolve './boom.css'",
"environmentLabel": null, "environmentLabel": null,
"label": "Build Error", "label": "Build Error",
"source": "./app/styles2.css (1:2) "source": "./app/styles2.css (1:1)
Module not found: Can't resolve './boom.css' Module not found: Can't resolve './boom.css'
> 1 | @import "./boom.css" > 1 | @import "./boom.css"
| ^", | ^",
"stack": [], "stack": [],
} }
`) `)
......
...@@ -22,7 +22,8 @@ describe('CSS Support', () => { ...@@ -22,7 +22,8 @@ describe('CSS Support', () => {
const appDir = join(fixturesDir, 'npm-import-bad') const appDir = join(fixturesDir, 'npm-import-bad')
const nextConfig = new File(join(appDir, 'next.config.js')) const nextConfig = new File(join(appDir, 'next.config.js'))
describe.each([true, false])( // Turbopack always uses Lightning CSS anyway
describe.each(process.env.IS_TURBOPACK_TEST ? [true] : [true, false])(
`useLightnincsss(%s)`, `useLightnincsss(%s)`,
(useLightningcss) => { (useLightningcss) => {
beforeAll(async () => { beforeAll(async () => {
......
...@@ -490,6 +490,20 @@ impl IssueSource { ...@@ -490,6 +490,20 @@ impl IssueSource {
} }
} }
pub fn from_single_line_col(source: ResolvedVc<Box<dyn Source>>, pos: SourcePos) -> Self {
IssueSource {
source,
range: Some(SourceRange::LineColumn(
pos,
SourcePos {
line: pos.line,
// The end position is the first character after the range
column: pos.column + 1,
},
)),
}
}
async fn into_plain(self) -> Result<PlainIssueSource> { async fn into_plain(self) -> Result<PlainIssueSource> {
let Self { mut source, range } = self; let Self { mut source, range } = self;
......
...@@ -460,13 +460,14 @@ async fn process_content( ...@@ -460,13 +460,14 @@ async fn process_content(
| lightningcss::error::ParserError::SelectorError(..) | lightningcss::error::ParserError::SelectorError(..)
| lightningcss::error::ParserError::EndOfInput => { | lightningcss::error::ParserError::EndOfInput => {
let source = match &err.loc { let source = match &err.loc {
Some(loc) => { Some(loc) => IssueSource::from_single_line_col(
let pos = SourcePos { source,
line: loc.line as _, SourcePos {
column: (loc.column - 1) as _, // lightningcss::ErrorLocation is 1-based for column only
}; line: loc.line,
IssueSource::from_line_col(source, pos, pos) column: loc.column - 1,
} },
),
None => IssueSource::from_source_only(source), None => IssueSource::from_source_only(source),
}; };
...@@ -499,13 +500,14 @@ async fn process_content( ...@@ -499,13 +500,14 @@ async fn process_content(
..Default::default() ..Default::default()
}) { }) {
let source = match &e.loc { let source = match &e.loc {
Some(loc) => { Some(loc) => IssueSource::from_single_line_col(
let pos = SourcePos { source,
line: loc.line as _, SourcePos {
column: (loc.column - 1) as _, // lightningcss::ErrorLocation is 1-based for column only
}; line: loc.line,
IssueSource::from_line_col(source, pos, pos) column: loc.column - 1,
} },
),
None => IssueSource::from_source_only(source), None => IssueSource::from_source_only(source),
}; };
ParsingIssue { ParsingIssue {
...@@ -522,13 +524,14 @@ async fn process_content( ...@@ -522,13 +524,14 @@ async fn process_content(
} }
Err(e) => { Err(e) => {
let source = match &e.loc { let source = match &e.loc {
Some(loc) => { Some(loc) => IssueSource::from_single_line_col(
let pos = SourcePos { source,
line: loc.line as _, SourcePos {
column: (loc.column - 1) as _, // lightningcss::ErrorLocation is 1-based for column only
}; line: loc.line,
IssueSource::from_line_col(source, pos, pos) column: loc.column - 1,
} },
),
None => IssueSource::from_source_only(source), None => IssueSource::from_source_only(source),
}; };
ParsingIssue { ParsingIssue {
......
...@@ -105,15 +105,12 @@ impl Visitor<'_> for ModuleReferencesVisitor<'_> { ...@@ -105,15 +105,12 @@ impl Visitor<'_> for ModuleReferencesVisitor<'_> {
Request::parse(RcStr::from(src).into()), Request::parse(RcStr::from(src).into()),
ImportAttributes::new_from_lightningcss(&i.clone().into_owned()).cell(), ImportAttributes::new_from_lightningcss(&i.clone().into_owned()).cell(),
self.import_context.map(|ctx| *ctx), self.import_context.map(|ctx| *ctx),
IssueSource::from_line_col( IssueSource::from_single_line_col(
self.source, self.source,
SourcePos { SourcePos {
line: issue_span.line as _, // lightningcss::rules::Location is 1-based for column only
column: issue_span.column as _, line: issue_span.line,
}, column: issue_span.column - 1,
SourcePos {
line: issue_span.line as _,
column: issue_span.column as _,
}, },
), ),
))); )));
...@@ -140,15 +137,12 @@ impl Visitor<'_> for ModuleReferencesVisitor<'_> { ...@@ -140,15 +137,12 @@ impl Visitor<'_> for ModuleReferencesVisitor<'_> {
let vc = UrlAssetReference::new( let vc = UrlAssetReference::new(
*self.origin, *self.origin,
Request::parse(RcStr::from(src).into()), Request::parse(RcStr::from(src).into()),
IssueSource::from_line_col( IssueSource::from_single_line_col(
self.source, self.source,
SourcePos { SourcePos {
line: issue_span.line as _, // lightningcss::dependencies::Location is 1-based for both line and column
column: issue_span.column as _, line: issue_span.line - 1,
}, column: issue_span.column - 1,
SourcePos {
line: issue_span.line as _,
column: issue_span.column as _,
}, },
), ),
); );
......
...@@ -3,3 +3,9 @@ ...@@ -3,3 +3,9 @@
list-style-image: url(image.png); list-style-image: url(image.png);
background-image: url('image.png'); background-image: url('image.png');
} }
.style-unresolveable {
cursor: url(./missing1.png);
list-style-image: url(missing2.png);
background-image: url('missing3.png');
}
error - [resolve] /turbopack/crates/turbopack-tests/tests/snapshot/css/embed-url/input/style.css:8:10 Module not found: Can't resolve './missing1.png'
4 | background-image: url('image.png');
5 | }
6 |
7 | .style-unresolveable {
+ v
8 + cursor: url(./missing1.png);
+ ^
9 | list-style-image: url(missing2.png);
10 | background-image: url('missing3.png');
11 | }
12 |
| It was not possible to find the requested file.
| Parsed request as written in source code: relative './missing1.png'
| Path where resolving has started: [project]/turbopack/crates/turbopack-tests/tests/snapshot/css/embed-url/input/style.css
| Type of request: url request
|
Import trace:
test:
./turbopack/crates/turbopack-tests/tests/snapshot/css/embed-url/input/style.css
./turbopack/crates/turbopack-tests/tests/snapshot/css/embed-url/input/index.js
\ No newline at end of file
error - [resolve] /turbopack/crates/turbopack-tests/tests/snapshot/css/embed-url/input/style.css:9:19 Module not found: Can't resolve 'missing2.png'
5 | }
6 |
7 | .style-unresolveable {
8 | cursor: url(./missing1.png);
+ v
9 + list-style-image: url(missing2.png);
+ ^
10 | background-image: url('missing3.png');
11 | }
12 |
| It was not possible to find the requested file.
| Parsed request as written in source code: module "'missing2.png'"
| Path where resolving has started: [project]/turbopack/crates/turbopack-tests/tests/snapshot/css/embed-url/input/style.css
| Type of request: url request
|
Import trace:
test:
./turbopack/crates/turbopack-tests/tests/snapshot/css/embed-url/input/style.css
./turbopack/crates/turbopack-tests/tests/snapshot/css/embed-url/input/index.js
\ No newline at end of file
error - [resolve] /turbopack/crates/turbopack-tests/tests/snapshot/css/embed-url/input/style.css:10:20 Module not found: Can't resolve 'missing3.png'
6 |
7 | .style-unresolveable {
8 | cursor: url(./missing1.png);
9 | list-style-image: url(missing2.png);
+ v
10 + background-image: url('missing3.png');
+ ^
11 | }
12 |
| It was not possible to find the requested file.
| Parsed request as written in source code: module "'missing3.png'"
| Path where resolving has started: [project]/turbopack/crates/turbopack-tests/tests/snapshot/css/embed-url/input/style.css
| Type of request: url request
|
Import trace:
test:
./turbopack/crates/turbopack-tests/tests/snapshot/css/embed-url/input/style.css
./turbopack/crates/turbopack-tests/tests/snapshot/css/embed-url/input/index.js
\ No newline at end of file
...@@ -12,4 +12,10 @@ ...@@ -12,4 +12,10 @@
list-style-image: url("../static/image.0ccdd6e3.png"); list-style-image: url("../static/image.0ccdd6e3.png");
} }
.style-unresolveable {
cursor: url("./missing1.png");
background-image: url("missing3.png");
list-style-image: url("missing2.png");
}
/*# sourceMappingURL=turbopack_crates_turbopack-tests_tests_snapshot_css_embed-url_input_6213f966._.css.map*/ /*# sourceMappingURL=turbopack_crates_turbopack-tests_tests_snapshot_css_embed-url_input_6213f966._.css.map*/
\ No newline at end of file
...@@ -3,5 +3,5 @@ ...@@ -3,5 +3,5 @@
"sources": [], "sources": [],
"sections": [ "sections": [
{"offset": {"line": 1, "column": 0}, "map": {"version":3,"sources":["turbopack:///[project]/turbopack/crates/turbopack-tests/tests/snapshot/css/embed-url/input/style.module.css"],"sourcesContent":[".module-style {\n cursor: url(./image.png);\n list-style-image: url(image.png);\n background-image: url('./image.png');\n}\n"],"names":[],"mappings":"AAAA"}}, {"offset": {"line": 1, "column": 0}, "map": {"version":3,"sources":["turbopack:///[project]/turbopack/crates/turbopack-tests/tests/snapshot/css/embed-url/input/style.module.css"],"sourcesContent":[".module-style {\n cursor: url(./image.png);\n list-style-image: url(image.png);\n background-image: url('./image.png');\n}\n"],"names":[],"mappings":"AAAA"}},
{"offset": {"line": 8, "column": 0}, "map": {"version":3,"sources":["turbopack:///[project]/turbopack/crates/turbopack-tests/tests/snapshot/css/embed-url/input/style.css"],"sourcesContent":[".style {\n cursor: url(./image.png);\n list-style-image: url(image.png);\n background-image: url('image.png');\n}\n"],"names":[],"mappings":"AAAA"}}] {"offset": {"line": 8, "column": 0}, "map": {"version":3,"sources":["turbopack:///[project]/turbopack/crates/turbopack-tests/tests/snapshot/css/embed-url/input/style.css"],"sourcesContent":[".style {\n cursor: url(./image.png);\n list-style-image: url(image.png);\n background-image: url('image.png');\n}\n\n.style-unresolveable {\n cursor: url(./missing1.png);\n list-style-image: url(missing2.png);\n background-image: url('missing3.png');\n}\n"],"names":[],"mappings":"AAAA;;;;;;AAMA"}}]
} }
\ No newline at end of file
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