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

fix(turbopack): point at the glob that matched a file with no module type (#96561)

A build error for a glob-matched file with no module type named only the
file,
with no route, call site or import trace — the file never becomes a
module, so
it has no place in the module graph. `import.meta.glob` now also reports
an
error at the call site with the pattern and matched key. Also fixed the
stale
`Read more` link (308s to a page whose anchor changed).

<!-- 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 f8e4ccf4
...@@ -437,7 +437,7 @@ export function runErrorRecoveryHmrTest(nextConfig: { ...@@ -437,7 +437,7 @@ export function runErrorRecoveryHmrTest(nextConfig: {
Error: Unknown module type Error: Unknown module type
This module doesn't have an associated type. Use a known file extension, or register a loader for it. This module doesn't have an associated type. Use a known file extension, or register a loader for it.
Read more: https://nextjs.org/docs/app/api-reference/next-config-js/turbo#webpack-loaders" Read more: https://nextjs.org/docs/app/api-reference/config/next-config-js/turbopack#configuring-webpack-loaders"
`) `)
} else if (process.env.NEXT_RSPACK) { } else if (process.env.NEXT_RSPACK) {
expect(trimEndMultiline(await getRedboxSource(browser))) expect(trimEndMultiline(await getRedboxSource(browser)))
......
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('*.txt', {
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 file without a module type', () => {
const { next, isTurbopack } = nextTestSetup({
files: __dirname,
skipStart: true,
})
if (!isTurbopack) {
it('is turbopack-only', () => {})
return
}
it('should say which glob matched the file', async () => {
const { cliOutput, exitCode } = await next.build()
expect(exitCode).not.toBe(0)
expect(cliOutput).toContain('gamma.txt')
// The call site, so the glob that pulled the file in can be found.
expect(cliOutput).toContain('app/x-txt/page.tsx')
expect(cliOutput).toContain('import.meta.glob')
})
it('should link to the current webpack loaders documentation', async () => {
const { cliOutput } = await next.build()
expect(cliOutput).toContain(
'https://nextjs.org/docs/app/api-reference/config/next-config-js/turbopack#configuring-webpack-loaders'
)
})
})
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {}
module.exports = nextConfig
...@@ -58,6 +58,13 @@ impl Issue for ModuleIssue { ...@@ -58,6 +58,13 @@ impl Issue for ModuleIssue {
} }
} }
/// Report a file that could not be turned into a module because no module type
/// applies to it.
///
/// The file has no module in the module graph, so no import trace can be
/// computed for this issue. Whoever creates the reference should report the
/// importing location separately when the request isn't written by hand (a glob
/// or a dynamic request, for example).
#[turbo_tasks::function] #[turbo_tasks::function]
pub async fn emit_unknown_module_type_error(source: Vc<Box<dyn Source>>) -> Result<()> { pub async fn emit_unknown_module_type_error(source: Vc<Box<dyn Source>>) -> Result<()> {
ModuleIssue {ident: source.ident().to_resolved().await?, ModuleIssue {ident: source.ident().to_resolved().await?,
...@@ -65,7 +72,7 @@ pub async fn emit_unknown_module_type_error(source: Vc<Box<dyn Source>>) -> Resu ...@@ -65,7 +72,7 @@ pub async fn emit_unknown_module_type_error(source: Vc<Box<dyn Source>>) -> Resu
description: StyledString::Text( description: StyledString::Text(
r"This module doesn't have an associated type. Use a known file extension, or register a loader for it. r"This module doesn't have an associated type. Use a known file extension, or register a loader for it.
Read more: https://nextjs.org/docs/app/api-reference/next-config-js/turbo#webpack-loaders".into(), Read more: https://nextjs.org/docs/app/api-reference/config/next-config-js/turbopack#configuring-webpack-loaders".into(),
) )
.resolved_cell(), .resolved_cell(),
source: Some(IssueSource::from_source_only(source.to_resolved().await?)),} source: Some(IssueSource::from_source_only(source.to_resolved().await?)),}
......
...@@ -16,7 +16,7 @@ use swc_core::{ ...@@ -16,7 +16,7 @@ use swc_core::{
}, },
quote, quote_expr, quote, quote_expr,
}; };
use turbo_rcstr::RcStr; use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{ use turbo_tasks::{
FxIndexMap, NonLocalValue, ResolvedVc, TryJoinIterExt, ValueToString, Vc, FxIndexMap, NonLocalValue, ResolvedVc, TryJoinIterExt, ValueToString, Vc,
debug::ValueDebugFormat, trace::TraceRawVcs, debug::ValueDebugFormat, trace::TraceRawVcs,
...@@ -31,7 +31,7 @@ use turbopack_core::{ ...@@ -31,7 +31,7 @@ use turbopack_core::{
ModuleChunkItemIdExt, ModuleChunkItemIdExt,
}, },
ident::AssetIdent, ident::AssetIdent,
issue::IssueSource, issue::{IssueExt, IssueSeverity, IssueSource, StyledString, code_gen::CodeGenerationIssue},
module::{Module, ModuleSideEffects}, module::{Module, ModuleSideEffects},
module_graph::ModuleGraph, module_graph::ModuleGraph,
reference::{ModuleReference, ModuleReferences}, reference::{ModuleReference, ModuleReferences},
...@@ -721,6 +721,46 @@ impl Module for ImportMetaGlobAsset { ...@@ -721,6 +721,46 @@ impl Module for ImportMetaGlobAsset {
None => ExportUsage::All, None => ExportUsage::All,
}; };
// A matched file that has no module type is reported against the file
// itself, which is not part of the module graph and therefore has no
// import trace. Point at the call site as well, otherwise there is
// nothing connecting the error to a request the user never wrote.
for (key, entry) in map.iter() {
if entry.result.await?.primary.iter().any(|(_, item)| {
matches!(
item,
turbopack_core::resolve::ModuleResolveResultItem::Unknown(_)
)
}) {
CodeGenerationIssue {
severity: IssueSeverity::Error,
title: StyledString::Text(rcstr!(
"import.meta.glob() matched a file that has no module type"
))
.resolved_cell(),
message: StyledString::Text(
format!(
"import.meta.glob({}) matched {key}, which doesn't have an associated \
module type. Narrow the pattern, exclude the file with a negative \
pattern (\"!...\"), or register a loader or module type for its file \
extension.",
this.patterns
.iter()
.map(|p| format!("{p:?}"))
.collect::<Vec<_>>()
.join(", ")
)
.into(),
)
.resolved_cell(),
path: this.origin.into_trait_ref().await?.origin_path(),
source: this.issue_source,
}
.resolved_cell()
.emit();
}
}
Ok(Vc::cell( Ok(Vc::cell(
map.iter() map.iter()
.map(|(_, entry)| { .map(|(_, entry)| {
......
...@@ -2,4 +2,4 @@ error - [process module] /turbopack/crates/turbopack-tests/tests/execution/turbo ...@@ -2,4 +2,4 @@ error - [process module] /turbopack/crates/turbopack-tests/tests/execution/turbo
This module doesn't have an associated type. Use a known file extension, or register a loader for it. This module doesn't have an associated type. Use a known file extension, or register a loader for it.
Read more: https://nextjs.org/docs/app/api-reference/next-config-js/turbo#webpack-loaders Read more: https://nextjs.org/docs/app/api-reference/config/next-config-js/turbopack#configuring-webpack-loaders
\ No newline at end of file \ No newline at end of file
...@@ -2,4 +2,4 @@ error - [process module] /turbopack/crates/turbopack-tests/tests/execution/turbo ...@@ -2,4 +2,4 @@ error - [process module] /turbopack/crates/turbopack-tests/tests/execution/turbo
This module doesn't have an associated type. Use a known file extension, or register a loader for it. This module doesn't have an associated type. Use a known file extension, or register a loader for it.
Read more: https://nextjs.org/docs/app/api-reference/next-config-js/turbo#webpack-loaders Read more: https://nextjs.org/docs/app/api-reference/config/next-config-js/turbopack#configuring-webpack-loaders
\ No newline at end of file \ No newline at end of file
// A glob that matches a file without a module type must report an error that
// says which glob pulled the file in. The value of this test is the `issues/`
// snapshot.
const texts = import.meta.glob('./content/*.txt')
it('should have reported an issue naming the glob that matched the file', () => {
expect(Object.keys(texts)).toEqual(['./content/gamma.txt'])
})
error - [process module] /turbopack/crates/turbopack-tests/tests/execution/turbopack/resolving/import-meta-glob-unknown-type/input/content/gamma.txt Unknown module type
This module doesn't have an associated type. Use a known file extension, or register a loader for it.
Read more: https://nextjs.org/docs/app/api-reference/config/next-config-js/turbopack#configuring-webpack-loaders
\ No newline at end of file
error - [code gen] /turbopack/crates/turbopack-tests/tests/execution/turbopack/resolving/import-meta-glob-unknown-type/input/index.js:5:14 import.meta.glob() matched a file that has no module type
1 | // A glob that matches a file without a module type must report an error that
2 | // says which glob pulled the file in. The value of this test is the `issues/`
3 | // snapshot.
4 |
+ v---------------------------------v
5 + const texts = import.meta.glob('./content/*.txt')
+ ^---------------------------------^
6 |
7 | it('should have reported an issue naming the glob that matched the file', () => {
8 | expect(Object.keys(texts)).toEqual(['./content/gamma.txt'])
9 | })
import.meta.glob("./content/*.txt") matched ./content/gamma.txt, which doesn't have an associated module type. Narrow the pattern, exclude the file with a negative pattern ("!..."), or register a loader or module type for its file extension.
\ 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