Unverified Commit bbe79103 authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

fix(turbopack): name the module in the non-ESM-placeable error, and stop…

fix(turbopack): name the module in the non-ESM-placeable error, and stop duplicating the importer in traces (#96560)

A build that matched a stylesheet through `import.meta.glob` failed with
"non-ecmascript placeable asset" naming neither CSS nor the stylesheet,
and an
import trace listing the page twice. The message now names the offending
module; the duplicate came from the glob's virtual module ident having
no
layer, which is now set to the importer's layer so trace simplification
can
collapse it (and so the virtual module is distinct per layer).

<!-- NEXT_JS_LLM -->
Co-authored-by: 's avatarvercel-fleet[bot] <308483924+vercel-fleet[bot]@users.noreply.github.com>
Co-authored-by: 's avatarTobias Koppers <1365881+sokra@users.noreply.github.com>
parent 3ae062a1
import { ReactNode } from 'react'
export default function Root({ children }: { children: ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
export default function Page() {
return <p>hello world</p>
}
const mods = import.meta.glob('*.css', {
base: '../../content',
eager: true,
})
export default function Page() {
return <p>{Object.keys(mods).join(', ')}</p>
}
import { nextTestSetup } from 'e2e-utils'
describe('import.meta.glob matching a stylesheet', () => {
const { next, isTurbopack } = nextTestSetup({
files: __dirname,
skipStart: true,
})
if (!isTurbopack) {
it('is turbopack-only', () => {})
return
}
it('should name the stylesheet in the build error', async () => {
const { cliOutput, exitCode } = await next.build()
expect(exitCode).not.toBe(0)
// The offending file, not just the page that globbed it.
expect(cliOutput).toContain('styles.css')
// The page is listed once in the import trace, not twice.
expect(
cliOutput.match(/app\/x-css\/page\.tsx/g)?.length
).toBeLessThanOrEqual(2)
})
})
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {}
module.exports = nextConfig
...@@ -688,8 +688,13 @@ impl ImportMetaGlobAsset { ...@@ -688,8 +688,13 @@ impl ImportMetaGlobAsset {
impl Module for ImportMetaGlobAsset { impl Module for ImportMetaGlobAsset {
#[turbo_tasks::function] #[turbo_tasks::function]
async fn ident(&self) -> Result<Vc<AssetIdent>> { async fn ident(&self) -> Result<Vc<AssetIdent>> {
let origin_path = self.origin.into_trait_ref().await?.origin_path(); let origin = self.origin.into_trait_ref().await?;
let origin_path = origin.origin_path();
// The layer is part of the ident so that this virtual module is distinct
// per layer (the same file can be processed in multiple layers), and so
// that import traces can collapse it into the importing module.
Ok(AssetIdent::from_path(origin_path) Ok(AssetIdent::from_path(origin_path)
.with_layer(origin.asset_context().into_trait_ref().await?.layer())
.with_modifier(modifier( .with_modifier(modifier(
&self.patterns, &self.patterns,
self.eager, self.eager,
......
...@@ -13,8 +13,8 @@ use swc_core::{ ...@@ -13,8 +13,8 @@ use swc_core::{
}; };
use turbo_rcstr::{RcStr, rcstr}; use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{ use turbo_tasks::{
FxIndexMap, NonLocalValue, ResolvedVc, TryJoinIterExt, Vc, debug::ValueDebugFormat, FxIndexMap, NonLocalValue, ResolvedVc, TryJoinIterExt, ValueToString, Vc,
trace::TraceRawVcs, debug::ValueDebugFormat, trace::TraceRawVcs,
}; };
use turbopack_core::{ use turbopack_core::{
chunk::{ChunkableModule, ChunkingContext, ModuleChunkItemIdExt, ModuleId}, chunk::{ChunkableModule, ChunkingContext, ModuleChunkItemIdExt, ModuleId},
...@@ -392,11 +392,16 @@ async fn to_single_pattern_mapping( ...@@ -392,11 +392,16 @@ async fn to_single_pattern_mapping(
} }
} }
CodeGenerationIssue { CodeGenerationIssue {
severity: IssueSeverity::Bug, severity: IssueSeverity::Error,
title: StyledString::Text(rcstr!("non-ecmascript placeable asset")).resolved_cell(), title: StyledString::Text(rcstr!("non-ecmascript placeable asset")).resolved_cell(),
message: StyledString::Text(rcstr!( message: StyledString::Text(
"asset is not placeable in ESM chunks, so it doesn't have a module id" format!(
)) "{} is not placeable in ESM chunks, so it doesn't have a module id and can't be \
imported here. Only modules that are compiled to JavaScript can be imported.",
module.ident().to_string().await?
)
.into(),
)
.resolved_cell(), .resolved_cell(),
path: origin.into_trait_ref().await?.origin_path(), path: origin.into_trait_ref().await?.origin_path(),
source: None, source: None,
......
...@@ -7,11 +7,4 @@ warning - [analysis] /turbopack/crates/turbopack-tests/tests/execution/turbopack ...@@ -7,11 +7,4 @@ warning - [analysis] /turbopack/crates/turbopack-tests/tests/execution/turbopack
3 | 3 |
4 | // Warning: non-constant eager — should default to lazy (eager: false) 4 | // Warning: non-constant eager — should default to lazy (eager: false)
5 | const nonConstEager = import.meta.glob('./dir2/*.js', { 5 | const nonConstEager = import.meta.glob('./dir2/*.js', {
6 | eager: Math.random() > -1, 6 | eager: Math.random() > -1,
\ No newline at end of file
Import trace:
test:
./turbopack/crates/turbopack-tests/tests/execution/turbopack/resolving/import-meta-glob-warnings/input/index.js
./turbopack/crates/turbopack-tests/tests/execution/turbopack/resolving/import-meta-glob-warnings/input/index.js
\ No newline at end of file
...@@ -12,11 +12,4 @@ warning - [analysis] /turbopack/crates/turbopack-tests/tests/execution/turbopack ...@@ -12,11 +12,4 @@ warning - [analysis] /turbopack/crates/turbopack-tests/tests/execution/turbopack
18 | 18 |
19 | it('should still produce a lazy glob when "as" option is used', () => { 19 | it('should still produce a lazy glob when "as" option is used', () => {
20 | const keys = Object.keys(withAs).sort() 20 | const keys = Object.keys(withAs).sort()
21 | expect(keys).toEqual(['./dir/bar.js', './dir/foo.js']) 21 | expect(keys).toEqual(['./dir/bar.js', './dir/foo.js'])
\ No newline at end of file
Import trace:
test:
./turbopack/crates/turbopack-tests/tests/execution/turbopack/resolving/import-meta-glob-warnings/input/index.js
./turbopack/crates/turbopack-tests/tests/execution/turbopack/resolving/import-meta-glob-warnings/input/index.js
\ No newline at end of file
...@@ -12,11 +12,4 @@ warning - [analysis] /turbopack/crates/turbopack-tests/tests/execution/turbopack ...@@ -12,11 +12,4 @@ warning - [analysis] /turbopack/crates/turbopack-tests/tests/execution/turbopack
8 | 8 |
9 | // Warning: unknown option — should be ignored, glob still works 9 | // Warning: unknown option — should be ignored, glob still works
10 | const unknown = import.meta.glob(['./dir/*.js', './dir2/*.js'], { 10 | const unknown = import.meta.glob(['./dir/*.js', './dir2/*.js'], {
11 | exhaust: true, 11 | exhaust: true,
\ No newline at end of file
Import trace:
test:
./turbopack/crates/turbopack-tests/tests/execution/turbopack/resolving/import-meta-glob-warnings/input/index.js
./turbopack/crates/turbopack-tests/tests/execution/turbopack/resolving/import-meta-glob-warnings/input/index.js
\ No newline at end of file
...@@ -12,11 +12,4 @@ warning - [analysis] /turbopack/crates/turbopack-tests/tests/execution/turbopack ...@@ -12,11 +12,4 @@ warning - [analysis] /turbopack/crates/turbopack-tests/tests/execution/turbopack
13 | 13 |
14 | // Warning: non-constant caseSensitive — should default to case-sensitive matching 14 | // Warning: non-constant caseSensitive — should default to case-sensitive matching
15 | const nonConstCaseSensitive = import.meta.glob('./casedir/*.js', { 15 | const nonConstCaseSensitive = import.meta.glob('./casedir/*.js', {
16 | caseSensitive: 'false', 16 | caseSensitive: 'false',
\ No newline at end of file
Import trace:
test:
./turbopack/crates/turbopack-tests/tests/execution/turbopack/resolving/import-meta-glob-warnings/input/index.js
./turbopack/crates/turbopack-tests/tests/execution/turbopack/resolving/import-meta-glob-warnings/input/index.js
\ 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