Unverified Commit 2ae01a84 authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

Turbopack: show codeframe when tracing too much (#94207)

Finally, we can show a codeframe
<img width="1204" height="774" alt="Bildschirmfoto 2026-05-28 um 21 16
35"
src="https://github.com/user-attachments/assets/a70c1fce-8a86-472a-9904-28f181d32385"
/>
parent 87ee4fe4
...@@ -121,25 +121,30 @@ impl Asset for ServerNftJsonAsset { ...@@ -121,25 +121,30 @@ impl Asset for ServerNftJsonAsset {
) )
.connect(); .connect();
let mut server_output_assets = let mut server_output_assets = traced_modules_for_entries(
traced_modules_for_entries(module_graph, self.entries(), Some(self.ignores()), true) module_graph,
.await? self.entries(),
.iter() Some(self.ignores()),
.map(async |m| { true,
Ok(( None,
base_dir )
.get_relative_path_to(&m.ident().await?.path) .await?
.context("failed to compute relative path for server NFT JSON")?, .iter()
m.source() .map(async |m| {
.await? Ok((
.context("NFT module has no content")? base_dir
.content() .get_relative_path_to(&m.ident().await?.path)
.hash(HashAlgorithm::Xxh3Hash128Hex) .context("failed to compute relative path for server NFT JSON")?,
.await?, m.source()
)) .await?
}) .context("NFT module has no content")?
.try_join() .content()
.await?; .hash(HashAlgorithm::Xxh3Hash128Hex)
.await?,
))
})
.try_join()
.await?;
let next_dir = get_next_package(this.project.project_path().owned().await?).await?; let next_dir = get_next_package(this.project.project_path().owned().await?).await?;
for ty in ["app-page", "pages"] { for ty in ["app-page", "pages"] {
......
use std::collections::{BTreeSet, VecDeque}; use std::collections::{BTreeSet, VecDeque};
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use async_trait::async_trait;
use next_core::{app_structure::FileSystemPathVec, next_config::NextConfig}; use next_core::{app_structure::FileSystemPathVec, next_config::NextConfig};
use rustc_hash::{FxHashMap, FxHashSet}; use rustc_hash::{FxHashMap, FxHashSet};
use tracing::Instrument; use tracing::Instrument;
use turbo_rcstr::RcStr; use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{ use turbo_tasks::{
FxIndexMap, FxIndexSet, ReadRef, ResolvedVc, TryFlatJoinIterExt, TryJoinIterExt, Vc, FxIndexMap, FxIndexSet, ReadRef, ResolvedVc, TryFlatJoinIterExt, TryJoinIterExt, Vc,
}; };
...@@ -16,9 +17,12 @@ use turbo_tasks_hash::HashAlgorithm; ...@@ -16,9 +17,12 @@ use turbo_tasks_hash::HashAlgorithm;
use turbopack_core::{ use turbopack_core::{
asset::Asset, asset::Asset,
chunk::{ChunkingType, TracedMode}, chunk::{ChunkingType, TracedMode},
file_source::FileSource,
ident::AssetIdent, ident::AssetIdent,
issue::{Issue, IssueExt, IssueSeverity, IssueSource, IssueStage, StyledString},
module::{Module, Modules}, module::{Module, Modules},
module_graph::{GraphTraversalAction, ModuleGraph}, module_graph::{GraphTraversalAction, ModuleGraph},
raw_module::RawModule,
}; };
use crate::project::Project; use crate::project::Project;
...@@ -75,6 +79,7 @@ pub async fn trace_endpoint( ...@@ -75,6 +79,7 @@ pub async fn trace_endpoint(
.await? .await?
.map(|v| *v), .map(|v| *v),
false, false,
Some(next_config.config_file_path(project_path.clone())),
) )
.await?; .await?;
...@@ -262,6 +267,7 @@ pub async fn traced_modules_for_entries( ...@@ -262,6 +267,7 @@ pub async fn traced_modules_for_entries(
entry_modules: Vc<Modules>, entry_modules: Vc<Modules>,
exclude_glob: Option<Vc<Glob>>, exclude_glob: Option<Vc<Glob>>,
entries_are_traced: bool, entries_are_traced: bool,
forbidden_path: Option<Vc<FileSystemPath>>,
) -> Result<Vc<Modules>> { ) -> Result<Vc<Modules>> {
let exclude_glob_and_module_idents = if let Some(exclude_glob) = exclude_glob { let exclude_glob_and_module_idents = if let Some(exclude_glob) = exclude_glob {
let exclude_glob = exclude_glob.await?; let exclude_glob = exclude_glob.await?;
...@@ -271,6 +277,18 @@ pub async fn traced_modules_for_entries( ...@@ -271,6 +277,18 @@ pub async fn traced_modules_for_entries(
None None
}; };
let forbidden_module = if let Some(forbidden_path) = forbidden_path {
Some(ResolvedVc::upcast(
RawModule::new(Vc::upcast(FileSource::new(forbidden_path.owned().await?)))
.to_resolved()
.await?,
))
} else {
None
};
let mut forbidden_issues = vec![];
let mut traced_modules = FxIndexSet::default(); let mut traced_modules = FxIndexSet::default();
module_graph.await?.traverse_edges_dfs( module_graph.await?.traverse_edges_dfs(
entry_modules.await?.iter().copied(), entry_modules.await?.iter().copied(),
...@@ -283,6 +301,10 @@ pub async fn traced_modules_for_entries( ...@@ -283,6 +301,10 @@ pub async fn traced_modules_for_entries(
return Ok(GraphTraversalAction::Continue); return Ok(GraphTraversalAction::Continue);
}; };
if forbidden_module.is_some_and(|m| m == target) {
forbidden_issues.push((parent, ref_data.reference));
}
if should_visit_for_tracing(&ref_data.chunking_type, traced_modules.contains(&parent)) { if should_visit_for_tracing(&ref_data.chunking_type, traced_modules.contains(&parent)) {
if let Some((exclude_glob, module_idents)) = &exclude_glob_and_module_idents if let Some((exclude_glob, module_idents)) = &exclude_glob_and_module_idents
&& exclude_glob.matches( && exclude_glob.matches(
...@@ -303,6 +325,16 @@ pub async fn traced_modules_for_entries( ...@@ -303,6 +325,16 @@ pub async fn traced_modules_for_entries(
true, true,
)?; )?;
for (parent, reference) in forbidden_issues {
ForbiddenTracedFileIssue::new(
parent.ident().await?.path.clone(),
reference.into_trait_ref().await?.source(),
)
.to_resolved()
.await?
.emit();
}
Ok(Vc::cell(traced_modules.into_iter().collect())) Ok(Vc::cell(traced_modules.into_iter().collect()))
} }
...@@ -392,3 +424,93 @@ pub async fn traced_module_data_for_graph( ...@@ -392,3 +424,93 @@ pub async fn traced_module_data_for_graph(
} }
.cell()) .cell())
} }
#[turbo_tasks::value(shared)]
struct ForbiddenTracedFileIssue {
parent: FileSystemPath,
issue_source: Option<IssueSource>,
}
#[turbo_tasks::value_impl]
impl ForbiddenTracedFileIssue {
#[turbo_tasks::function]
pub async fn new(
parent: FileSystemPath,
issue_source: Option<IssueSource>,
) -> Result<Vc<Self>> {
Ok(Self {
parent,
issue_source,
}
.cell())
}
}
#[async_trait]
#[turbo_tasks::value_impl]
impl Issue for ForbiddenTracedFileIssue {
fn severity(&self) -> IssueSeverity {
// Ideally this would be an error, but for now we keep it a warning to avoid breaking
// existing apps
IssueSeverity::Warning
}
fn stage(&self) -> IssueStage {
IssueStage::Misc
}
fn source(&self) -> Option<IssueSource> {
self.issue_source
}
async fn file_path(&self) -> Result<FileSystemPath> {
Ok(self.parent.clone())
}
async fn title(&self) -> Result<StyledString> {
Ok(StyledString::Text(rcstr!(
"Encountered unexpected file in NFT list"
)))
}
async fn description(&self) -> Result<Option<StyledString>> {
let stack = vec![
StyledString::Text(rcstr!(
"This import traced the next.config.js file which indicates that the whole \
project was traced unintentionally. Somewhere in the import trace below, there \
are:"
)),
StyledString::Line(vec![
StyledString::Text(rcstr!("- filesystem operations (like ")),
StyledString::Code(rcstr!("path.join")),
StyledString::Text(rcstr!(", ")),
StyledString::Code(rcstr!("path.resolve")),
StyledString::Text(rcstr!(" or ")),
StyledString::Code(rcstr!("fs.readFile")),
StyledString::Text(rcstr!("), or")),
]),
StyledString::Line(vec![
StyledString::Text(rcstr!("- very dynamic requires (like ")),
StyledString::Code(rcstr!("require('./' + foo)")),
StyledString::Text(rcstr!(").")),
]),
StyledString::Text(rcstr!("To resolve this, you can")),
StyledString::Text(rcstr!("- remove them if possible, or")),
StyledString::Text(rcstr!("- only use them in development, or")),
StyledString::Line(vec![
StyledString::Text(rcstr!(
"- make sure they are statically scoped to some subfolder: "
)),
StyledString::Code(rcstr!("path.join(process.cwd(), 'data', bar)")),
StyledString::Text(rcstr!(", or")),
]),
StyledString::Line(vec![
StyledString::Text(rcstr!("- add ignore comments: ")),
StyledString::Code(rcstr!(
"path.join(/*turbopackIgnore: true*/ process.cwd(), bar)"
)),
]),
];
Ok(Some(StyledString::Stack(stack)))
}
}
use anyhow::{Context, Result, bail}; use anyhow::{Context, Result, bail};
use async_trait::async_trait;
use either::Either; use either::Either;
use serde_json::json; use serde_json::json;
use tracing::{Instrument, Level, Span}; use tracing::{Instrument, Level, Span};
use turbo_rcstr::{RcStr, rcstr}; use turbo_rcstr::RcStr;
use turbo_tasks::{ use turbo_tasks::{
ReadRef, ResolvedVc, TryFlatJoinIterExt, TryJoinIterExt, ValueToString, Vc, ReadRef, ResolvedVc, TryFlatJoinIterExt, TryJoinIterExt, ValueToString, Vc,
graph::{AdjacencyMap, GraphTraversal, Visit}, graph::{AdjacencyMap, GraphTraversal, Visit},
...@@ -13,7 +12,6 @@ use turbo_tasks_fs::{File, FileContent, FileSystem, FileSystemPath, glob::Glob}; ...@@ -13,7 +12,6 @@ use turbo_tasks_fs::{File, FileContent, FileSystem, FileSystemPath, glob::Glob};
use turbo_tasks_hash::HashAlgorithm; use turbo_tasks_hash::HashAlgorithm;
use turbopack_core::{ use turbopack_core::{
asset::{Asset, AssetContent}, asset::{Asset, AssetContent},
issue::{Issue, IssueExt, IssueSeverity, IssueStage, StyledString},
module::Module, module::Module,
output::{OutputAsset, OutputAssets, OutputAssetsReference}, output::{OutputAsset, OutputAssets, OutputAssetsReference},
}; };
...@@ -121,11 +119,6 @@ impl Asset for NftJsonAsset { ...@@ -121,11 +119,6 @@ impl Asset for NftJsonAsset {
let output_root_ref = this.project.output_fs().root().await?; let output_root_ref = this.project.output_fs().root().await?;
let project_root_ref = this.project.project_fs().root().await?; let project_root_ref = this.project.project_fs().root().await?;
let next_config = this.project.next_config(); let next_config = this.project.next_config();
let next_config_path = this
.project
.next_config()
.config_file_path(project_path.clone())
.await?;
let client_root = this.project.client_fs().root(); let client_root = this.project.client_fs().root();
let client_root = client_root.owned().await?; let client_root = client_root.owned().await?;
...@@ -201,18 +194,6 @@ impl Asset for NftJsonAsset { ...@@ -201,18 +194,6 @@ impl Asset for NftJsonAsset {
Either::Right(p) => p, Either::Right(p) => p,
}; };
if let AssetOrModule::Module(_) = referenced
&& referenced_chunk_path == &*next_config_path
{
// If next.config.js was traced, assume that the whole project was traced
// (unintentionally). Print a message in this case to avoid deploying
// unnecessary files.
ForbiddenTracedFileIssue::new(referenced_chunk_path.clone())
.to_resolved()
.await?
.emit();
}
if referenced_chunk_path.has_extension(".map") { if referenced_chunk_path.has_extension(".map") {
return Ok(None); return Ok(None);
} }
...@@ -350,83 +331,6 @@ async fn all_assets_from_entries_filtered( ...@@ -350,83 +331,6 @@ async fn all_assets_from_entries_filtered(
)) ))
} }
#[turbo_tasks::value(shared)]
struct ForbiddenTracedFileIssue {
file: FileSystemPath,
}
#[turbo_tasks::value_impl]
impl ForbiddenTracedFileIssue {
#[turbo_tasks::function]
pub fn new(file: FileSystemPath) -> Vc<Self> {
Self { file }.cell()
}
}
#[async_trait]
#[turbo_tasks::value_impl]
impl Issue for ForbiddenTracedFileIssue {
fn severity(&self) -> IssueSeverity {
// Ideally this would be an error, but for now we keep it a warning to avoid breaking
// existing apps
IssueSeverity::Warning
}
fn stage(&self) -> IssueStage {
IssueStage::Misc
}
async fn file_path(&self) -> Result<FileSystemPath> {
Ok(self.file.clone())
}
async fn title(&self) -> Result<StyledString> {
Ok(StyledString::Text(rcstr!(
"Encountered unexpected file in NFT list"
)))
}
async fn description(&self) -> Result<Option<StyledString>> {
let stack = vec![
StyledString::Text(rcstr!(
"A file was traced that indicates that the whole project was traced \
unintentionally. Somewhere in the import trace below, there are:"
)),
StyledString::Line(vec![
StyledString::Text(rcstr!("- filesystem operations (like ")),
StyledString::Code(rcstr!("path.join")),
StyledString::Text(rcstr!(", ")),
StyledString::Code(rcstr!("path.resolve")),
StyledString::Text(rcstr!(" or ")),
StyledString::Code(rcstr!("fs.readFile")),
StyledString::Text(rcstr!("), or")),
]),
StyledString::Line(vec![
StyledString::Text(rcstr!("- very dynamic requires (like ")),
StyledString::Code(rcstr!("require('./' + foo)")),
StyledString::Text(rcstr!(").")),
]),
StyledString::Text(rcstr!("To resolve this, you can")),
StyledString::Text(rcstr!("- remove them if possible, or")),
StyledString::Text(rcstr!("- only use them in development, or")),
StyledString::Line(vec![
StyledString::Text(rcstr!(
"- make sure they are statically scoped to some subfolder: "
)),
StyledString::Code(rcstr!("path.join(process.cwd(), 'data', bar)")),
StyledString::Text(rcstr!(", or")),
]),
StyledString::Line(vec![
StyledString::Text(rcstr!("- add ignore comments: ")),
StyledString::Code(rcstr!(
"path.join(/*turbopackIgnore: true*/ process.cwd(), bar)"
)),
]),
];
Ok(Some(StyledString::Stack(stack)))
}
}
struct OutputAssetFilteredVisit { struct OutputAssetFilteredVisit {
client_root: Option<FileSystemPath>, client_root: Option<FileSystemPath>,
exclude_glob: Option<ReadRef<Glob>>, exclude_glob: Option<ReadRef<Glob>>,
......
import { nextTestSetup } from 'e2e-utils' import { nextTestSetup } from 'e2e-utils'
import stripAnsi from 'strip-ansi'
// Only Turbopack prints these warnings // Only Turbopack prints these warnings
;(process.env.IS_TURBOPACK_TEST ? describe : describe.skip)( ;(process.env.IS_TURBOPACK_TEST ? describe : describe.skip)(
...@@ -20,11 +21,18 @@ import { nextTestSetup } from 'e2e-utils' ...@@ -20,11 +21,18 @@ import { nextTestSetup } from 'e2e-utils'
) )
.trim() .trim()
expect(output).toMatchInlineSnapshot(` expect(stripAnsi(output)).toMatchInlineSnapshot(`
"Turbopack build encountered 1 warnings: "Turbopack build encountered 1 warnings:
./next.config.js ./app/join-cwd.js:4:10
Encountered unexpected file in NFT list Encountered unexpected file in NFT list
A file was traced that indicates that the whole project was traced unintentionally. Somewhere in the import trace below, there are: 2 |
3 | export default function (f) {
> 4 | return path.join(process.cwd(), f)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
5 | }
6 |
This import traced the next.config.js file which indicates that the whole project was traced unintentionally. Somewhere in the import trace below, there are:
- filesystem operations (like path.join, path.resolve or fs.readFile), or - filesystem operations (like path.join, path.resolve or fs.readFile), or
- very dynamic requires (like require('./' + foo)). - very dynamic requires (like require('./' + foo)).
To resolve this, you can To resolve this, you can
...@@ -35,7 +43,6 @@ import { nextTestSetup } from 'e2e-utils' ...@@ -35,7 +43,6 @@ import { nextTestSetup } from 'e2e-utils'
Import trace: Import trace:
Server Component: Server Component:
./next.config.js
./app/join-cwd.js ./app/join-cwd.js
./app/page.js" ./app/page.js"
`) `)
......
...@@ -10,6 +10,7 @@ use turbo_tasks::{ ...@@ -10,6 +10,7 @@ use turbo_tasks::{
use crate::{ use crate::{
chunk::{ChunkingType, TracedMode}, chunk::{ChunkingType, TracedMode},
issue::IssueSource,
module::{Module, Modules}, module::{Module, Modules},
output::{ output::{
ExpandOutputAssetsInput, ExpandedOutputAssets, OutputAsset, OutputAssets, ExpandOutputAssetsInput, ExpandedOutputAssets, OutputAsset, OutputAssets,
...@@ -39,6 +40,10 @@ pub trait ModuleReference: ValueToString { ...@@ -39,6 +40,10 @@ pub trait ModuleReference: ValueToString {
fn binding_usage(&self) -> BindingUsage { fn binding_usage(&self) -> BindingUsage {
BindingUsage::default() BindingUsage::default()
} }
fn source(&self) -> Option<IssueSource> {
None
}
} }
/// Multiple [ModuleReference]s /// Multiple [ModuleReference]s
......
...@@ -148,6 +148,10 @@ impl ModuleReference for ImportAssetReference { ...@@ -148,6 +148,10 @@ impl ModuleReference for ImportAssetReference {
hoisted: false, hoisted: false,
}) })
} }
fn source(&self) -> Option<IssueSource> {
Some(self.issue_source)
}
} }
#[turbo_tasks::value_impl] #[turbo_tasks::value_impl]
......
...@@ -92,6 +92,10 @@ impl ModuleReference for UrlAssetReference { ...@@ -92,6 +92,10 @@ impl ModuleReference for UrlAssetReference {
hoisted: false, hoisted: false,
}) })
} }
fn source(&self) -> Option<IssueSource> {
Some(self.issue_source)
}
} }
#[turbo_tasks::function] #[turbo_tasks::function]
......
...@@ -80,6 +80,10 @@ impl ModuleReference for AmdDefineAssetReference { ...@@ -80,6 +80,10 @@ impl ModuleReference for AmdDefineAssetReference {
hoisted: false, hoisted: false,
}) })
} }
fn source(&self) -> Option<IssueSource> {
Some(self.issue_source)
}
} }
#[derive( #[derive(
......
...@@ -77,6 +77,10 @@ impl ModuleReference for CjsAssetReference { ...@@ -77,6 +77,10 @@ impl ModuleReference for CjsAssetReference {
hoisted: false, hoisted: false,
}) })
} }
fn source(&self) -> Option<IssueSource> {
Some(self.issue_source)
}
} }
#[turbo_tasks::value] #[turbo_tasks::value]
...@@ -139,6 +143,10 @@ impl ModuleReference for CjsRequireAssetReference { ...@@ -139,6 +143,10 @@ impl ModuleReference for CjsRequireAssetReference {
|c| c.as_chunking_type(false, false), |c| c.as_chunking_type(false, false),
) )
} }
fn source(&self) -> Option<IssueSource> {
Some(self.issue_source)
}
} }
impl IntoCodeGenReference for CjsRequireAssetReference { impl IntoCodeGenReference for CjsRequireAssetReference {
...@@ -276,6 +284,10 @@ impl ModuleReference for CjsRequireResolveAssetReference { ...@@ -276,6 +284,10 @@ impl ModuleReference for CjsRequireResolveAssetReference {
|c| c.as_chunking_type(false, false), |c| c.as_chunking_type(false, false),
) )
} }
fn source(&self) -> Option<IssueSource> {
Some(self.issue_source)
}
} }
impl IntoCodeGenReference for CjsRequireResolveAssetReference { impl IntoCodeGenReference for CjsRequireResolveAssetReference {
......
...@@ -587,6 +587,10 @@ impl ModuleReference for EsmAssetReference { ...@@ -587,6 +587,10 @@ impl ModuleReference for EsmAssetReference {
}, },
} }
} }
fn source(&self) -> Option<IssueSource> {
Some(self.issue_source)
}
} }
impl EsmAssetReference { impl EsmAssetReference {
......
...@@ -112,6 +112,10 @@ impl ModuleReference for EsmAsyncAssetReference { ...@@ -112,6 +112,10 @@ impl ModuleReference for EsmAsyncAssetReference {
export: self.export_usage.clone(), export: self.export_usage.clone(),
} }
} }
fn source(&self) -> Option<IssueSource> {
Some(self.issue_source)
}
} }
impl IntoCodeGenReference for EsmAsyncAssetReference { impl IntoCodeGenReference for EsmAsyncAssetReference {
......
...@@ -107,6 +107,10 @@ impl ModuleReference for UrlAssetReference { ...@@ -107,6 +107,10 @@ impl ModuleReference for UrlAssetReference {
hoisted: false, hoisted: false,
}) })
} }
fn source(&self) -> Option<IssueSource> {
Some(self.issue_source)
}
} }
impl IntoCodeGenReference for UrlAssetReference { impl IntoCodeGenReference for UrlAssetReference {
......
...@@ -83,6 +83,10 @@ impl ModuleReference for FileSourceReference { ...@@ -83,6 +83,10 @@ impl ModuleReference for FileSourceReference {
mode: TracedMode::Entry, mode: TracedMode::Entry,
}) })
} }
fn source(&self) -> Option<IssueSource> {
Some(self.issue_source)
}
} }
#[turbo_tasks::value] #[turbo_tasks::value]
...@@ -213,4 +217,8 @@ impl ModuleReference for DirAssetReference { ...@@ -213,4 +217,8 @@ impl ModuleReference for DirAssetReference {
mode: TracedMode::Entry, mode: TracedMode::Entry,
}) })
} }
fn source(&self) -> Option<IssueSource> {
Some(self.issue_source)
}
} }
...@@ -301,6 +301,10 @@ impl ModuleReference for RequireContextAssetReference { ...@@ -301,6 +301,10 @@ impl ModuleReference for RequireContextAssetReference {
hoisted: false, hoisted: false,
}) })
} }
fn source(&self) -> Option<IssueSource> {
self.issue_source
}
} }
impl IntoCodeGenReference for RequireContextAssetReference { impl IntoCodeGenReference for RequireContextAssetReference {
......
...@@ -262,6 +262,10 @@ impl ModuleReference for WorkerAssetReference { ...@@ -262,6 +262,10 @@ impl ModuleReference for WorkerAssetReference {
hoisted: false, hoisted: false,
}) })
} }
fn source(&self) -> Option<IssueSource> {
Some(self.issue_source)
}
} }
impl WorkerAssetReference { impl WorkerAssetReference {
......
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