Unverified Commit 33872bfa authored by Will Binns-Smith's avatar Will Binns-Smith Committed by GitHub

Make `InvalidImportResolvePlugin` a `BeforeResolvePlugin` (#66622)

https://github.com/vercel/turbo/pull/8165 introduced plugins that
operate before resolving occurs, meaning that plugins like
`InvalidImportResolvePlugin` which never use the initial resolve result
and report issues can avoid that work.

Test Plan: `TURBOPACK_DEV=1 TURBOPACK=1 pnpm test-dev
test/development/acceptance-app/invalid-imports.test.ts`
parent 8f67469b
This diff is collapsed.
...@@ -37,11 +37,11 @@ swc_core = { version = "0.93.2", features = [ ...@@ -37,11 +37,11 @@ swc_core = { version = "0.93.2", features = [
testing = { version = "0.35.25" } testing = { version = "0.35.25" }
# Turbo crates # Turbo crates
turbopack-binding = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-240612.1" } turbopack-binding = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-240612.3" }
# [TODO]: need to refactor embed_directory! macro usages, as well as resolving turbo_tasks::function, macros.. # [TODO]: need to refactor embed_directory! macro usages, as well as resolving turbo_tasks::function, macros..
turbo-tasks = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-240612.1" } turbo-tasks = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-240612.3" }
# [TODO]: need to refactor embed_directory! macro usage in next-core # [TODO]: need to refactor embed_directory! macro usage in next-core
turbo-tasks-fs = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-240612.1" } turbo-tasks-fs = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-240612.3" }
# General Deps # General Deps
......
...@@ -49,7 +49,7 @@ use crate::{ ...@@ -49,7 +49,7 @@ use crate::{
next_shared::{ next_shared::{
resolve::{ resolve::{
get_invalid_server_only_resolve_plugin, ModuleFeatureReportResolvePlugin, get_invalid_server_only_resolve_plugin, ModuleFeatureReportResolvePlugin,
NextSharedRuntimeResolvePlugin, UnsupportedModulesResolvePlugin, NextSharedRuntimeResolvePlugin,
}, },
transforms::{ transforms::{
emotion::get_emotion_transform_rule, relay::get_relay_transform_rule, emotion::get_emotion_transform_rule, relay::get_relay_transform_rule,
...@@ -158,13 +158,14 @@ pub async fn get_client_resolve_options_context( ...@@ -158,13 +158,14 @@ pub async fn get_client_resolve_options_context(
resolved_map: Some(next_client_resolved_map), resolved_map: Some(next_client_resolved_map),
browser: true, browser: true,
module: true, module: true,
before_resolve_plugins: vec![Vc::upcast(NextFontLocalResolvePlugin::new(project_path))], before_resolve_plugins: vec![
after_resolve_plugins: vec![
Vc::upcast(ModuleFeatureReportResolvePlugin::new(project_path)),
Vc::upcast(UnsupportedModulesResolvePlugin::new(project_path)),
Vc::upcast(NextSharedRuntimeResolvePlugin::new(project_path)),
Vc::upcast(get_invalid_server_only_resolve_plugin(project_path)), Vc::upcast(get_invalid_server_only_resolve_plugin(project_path)),
Vc::upcast(ModuleFeatureReportResolvePlugin::new(project_path)),
Vc::upcast(NextFontLocalResolvePlugin::new(project_path)),
], ],
after_resolve_plugins: vec![Vc::upcast(NextSharedRuntimeResolvePlugin::new(
project_path,
))],
..Default::default() ..Default::default()
}; };
Ok(ResolveOptionsContext { Ok(ResolveOptionsContext {
......
...@@ -28,7 +28,6 @@ use crate::{ ...@@ -28,7 +28,6 @@ use crate::{
next_shared::resolve::{ next_shared::resolve::{
get_invalid_client_only_resolve_plugin, get_invalid_styled_jsx_resolve_plugin, get_invalid_client_only_resolve_plugin, get_invalid_styled_jsx_resolve_plugin,
ModuleFeatureReportResolvePlugin, NextSharedRuntimeResolvePlugin, ModuleFeatureReportResolvePlugin, NextSharedRuntimeResolvePlugin,
UnsupportedModulesResolvePlugin,
}, },
util::{foreign_code_context_condition, NextRuntime}, util::{foreign_code_context_condition, NextRuntime},
}; };
...@@ -101,44 +100,37 @@ pub async fn get_edge_resolve_options_context( ...@@ -101,44 +100,37 @@ pub async fn get_edge_resolve_options_context(
let ty = ty.into_value(); let ty = ty.into_value();
let before_resolve_plugins = match ty { let mut before_resolve_plugins = vec![Vc::upcast(ModuleFeatureReportResolvePlugin::new(
project_path,
))];
if matches!(
ty,
ServerContextType::Pages { .. } ServerContextType::Pages { .. }
| ServerContextType::AppSSR { .. } | ServerContextType::AppSSR { .. }
| ServerContextType::AppRSC { .. } => { | ServerContextType::AppRSC { .. }
vec![Vc::upcast(NextFontLocalResolvePlugin::new(project_path))] ) {
before_resolve_plugins.push(Vc::upcast(NextFontLocalResolvePlugin::new(project_path)));
} }
ServerContextType::PagesData { .. }
| ServerContextType::PagesApi { .. }
| ServerContextType::AppRoute { .. }
| ServerContextType::Middleware { .. }
| ServerContextType::Instrumentation => vec![],
};
let mut after_resolve_plugins = match ty { if matches!(
ServerContextType::Pages { .. } ty,
| ServerContextType::PagesApi { .. }
| ServerContextType::AppSSR { .. } => {
vec![]
}
ServerContextType::AppRSC { .. } ServerContextType::AppRSC { .. }
| ServerContextType::AppRoute { .. } | ServerContextType::AppRoute { .. }
| ServerContextType::PagesData { .. } | ServerContextType::PagesData { .. }
| ServerContextType::Middleware { .. } | ServerContextType::Middleware { .. }
| ServerContextType::Instrumentation => { | ServerContextType::Instrumentation
vec![ ) {
Vc::upcast(get_invalid_client_only_resolve_plugin(project_path)), before_resolve_plugins.push(Vc::upcast(get_invalid_client_only_resolve_plugin(
Vc::upcast(get_invalid_styled_jsx_resolve_plugin(project_path)), project_path,
] )));
before_resolve_plugins.push(Vc::upcast(get_invalid_styled_jsx_resolve_plugin(
project_path,
)));
} }
};
let base_plugins = vec![ let after_resolve_plugins = vec![Vc::upcast(NextSharedRuntimeResolvePlugin::new(
Vc::upcast(ModuleFeatureReportResolvePlugin::new(project_path)), project_path,
Vc::upcast(UnsupportedModulesResolvePlugin::new(project_path)), ))];
Vc::upcast(NextSharedRuntimeResolvePlugin::new(project_path)),
];
after_resolve_plugins.extend_from_slice(&base_plugins);
// https://github.com/vercel/next.js/blob/bf52c254973d99fed9d71507a2e818af80b8ade7/packages/next/src/build/webpack-config.ts#L96-L102 // https://github.com/vercel/next.js/blob/bf52c254973d99fed9d71507a2e818af80b8ade7/packages/next/src/build/webpack-config.ts#L96-L102
let mut custom_conditions = vec![mode.await?.condition().into()]; let mut custom_conditions = vec![mode.await?.condition().into()];
......
...@@ -15,7 +15,7 @@ use turbopack_binding::{ ...@@ -15,7 +15,7 @@ use turbopack_binding::{
resolve::{ resolve::{
parse::Request, parse::Request,
plugin::{BeforeResolvePlugin, BeforeResolvePluginCondition}, plugin::{BeforeResolvePlugin, BeforeResolvePluginCondition},
RequestKey, ResolveResult, ResolveResultItem, ResolveResultOption, ResolveResult, ResolveResultItem, ResolveResultOption,
}, },
virtual_source::VirtualSource, virtual_source::VirtualSource,
}, },
...@@ -70,7 +70,7 @@ impl NextFontLocalResolvePlugin { ...@@ -70,7 +70,7 @@ impl NextFontLocalResolvePlugin {
impl BeforeResolvePlugin for NextFontLocalResolvePlugin { impl BeforeResolvePlugin for NextFontLocalResolvePlugin {
#[turbo_tasks::function] #[turbo_tasks::function]
async fn before_resolve_condition(&self) -> Vc<BeforeResolvePluginCondition> { async fn before_resolve_condition(&self) -> Vc<BeforeResolvePluginCondition> {
BeforeResolvePluginCondition::new(Glob::new( BeforeResolvePluginCondition::from_request_glob(Glob::new(
"{next,@vercel/turbopack-next/internal}/font/local/*".into(), "{next,@vercel/turbopack-next/internal}/font/local/*".into(),
)) ))
} }
...@@ -125,16 +125,10 @@ impl BeforeResolvePlugin for NextFontLocalResolvePlugin { ...@@ -125,16 +125,10 @@ impl BeforeResolvePlugin for NextFontLocalResolvePlugin {
.emit(); .emit();
return Ok(ResolveResultOption::some( return Ok(ResolveResultOption::some(
ResolveResult::primary_with_key( ResolveResult::primary(ResolveResultItem::Error(Vc::cell(
RequestKey::new(font_path.clone()), format!("Font file not found: Can't resolve {}'", font_path)
ResolveResultItem::Error(Vc::cell(
format!(
"Font file not found: Can't resolve {}'",
font_path
)
.into(), .into(),
)), )))
)
.into(), .into(),
)); ));
} }
......
...@@ -53,7 +53,7 @@ use crate::{ ...@@ -53,7 +53,7 @@ use crate::{
resolve::{ resolve::{
get_invalid_client_only_resolve_plugin, get_invalid_styled_jsx_resolve_plugin, get_invalid_client_only_resolve_plugin, get_invalid_styled_jsx_resolve_plugin,
ModuleFeatureReportResolvePlugin, NextExternalResolvePlugin, ModuleFeatureReportResolvePlugin, NextExternalResolvePlugin,
NextNodeSharedRuntimeResolvePlugin, UnsupportedModulesResolvePlugin, NextNodeSharedRuntimeResolvePlugin,
}, },
transforms::{ transforms::{
emotion::get_emotion_transform_rule, get_ecma_transform_rule, emotion::get_emotion_transform_rule, get_ecma_transform_rule,
...@@ -126,7 +126,6 @@ pub async fn get_server_resolve_options_context( ...@@ -126,7 +126,6 @@ pub async fn get_server_resolve_options_context(
foreign_code_context_condition(next_config, project_path).await?; foreign_code_context_condition(next_config, project_path).await?;
let root_dir = project_path.root().resolve().await?; let root_dir = project_path.root().resolve().await?;
let module_feature_report_resolve_plugin = ModuleFeatureReportResolvePlugin::new(project_path); let module_feature_report_resolve_plugin = ModuleFeatureReportResolvePlugin::new(project_path);
let unsupported_modules_resolve_plugin = UnsupportedModulesResolvePlugin::new(project_path);
let invalid_client_only_resolve_plugin = get_invalid_client_only_resolve_plugin(project_path); let invalid_client_only_resolve_plugin = get_invalid_client_only_resolve_plugin(project_path);
let invalid_styled_jsx_client_only_resolve_plugin = let invalid_styled_jsx_client_only_resolve_plugin =
get_invalid_styled_jsx_resolve_plugin(project_path); get_invalid_styled_jsx_resolve_plugin(project_path);
...@@ -202,26 +201,29 @@ pub async fn get_server_resolve_options_context( ...@@ -202,26 +201,29 @@ pub async fn get_server_resolve_options_context(
let next_node_shared_runtime_plugin = let next_node_shared_runtime_plugin =
NextNodeSharedRuntimeResolvePlugin::new(project_path, Value::new(ty)); NextNodeSharedRuntimeResolvePlugin::new(project_path, Value::new(ty));
let before_resolve_plugins = match ty { let mut before_resolve_plugins = match ty {
ServerContextType::Pages { .. } ServerContextType::Pages { .. }
| ServerContextType::AppSSR { .. } | ServerContextType::AppSSR { .. }
| ServerContextType::AppRSC { .. } => { | ServerContextType::AppRSC { .. } => {
vec![Vc::upcast(NextFontLocalResolvePlugin::new(project_path))] vec![
Vc::upcast(NextFontLocalResolvePlugin::new(project_path)),
Vc::upcast(module_feature_report_resolve_plugin),
]
} }
ServerContextType::PagesData { .. } ServerContextType::PagesData { .. }
| ServerContextType::PagesApi { .. } | ServerContextType::PagesApi { .. }
| ServerContextType::AppRoute { .. } | ServerContextType::AppRoute { .. }
| ServerContextType::Middleware { .. } | ServerContextType::Middleware { .. }
| ServerContextType::Instrumentation => vec![], | ServerContextType::Instrumentation => {
vec![Vc::upcast(module_feature_report_resolve_plugin)]
}
}; };
let mut after_resolve_plugins = match ty { let after_resolve_plugins = match ty {
ServerContextType::Pages { .. } ServerContextType::Pages { .. }
| ServerContextType::PagesApi { .. } | ServerContextType::PagesApi { .. }
| ServerContextType::PagesData { .. } => { | ServerContextType::PagesData { .. } => {
vec![ vec![
Vc::upcast(module_feature_report_resolve_plugin),
Vc::upcast(unsupported_modules_resolve_plugin),
Vc::upcast(next_node_shared_runtime_plugin), Vc::upcast(next_node_shared_runtime_plugin),
Vc::upcast(external_cjs_modules_plugin), Vc::upcast(external_cjs_modules_plugin),
Vc::upcast(next_external_plugin), Vc::upcast(next_external_plugin),
...@@ -231,24 +233,16 @@ pub async fn get_server_resolve_options_context( ...@@ -231,24 +233,16 @@ pub async fn get_server_resolve_options_context(
| ServerContextType::AppRSC { .. } | ServerContextType::AppRSC { .. }
| ServerContextType::AppRoute { .. } => { | ServerContextType::AppRoute { .. } => {
vec![ vec![
Vc::upcast(module_feature_report_resolve_plugin),
Vc::upcast(unsupported_modules_resolve_plugin),
Vc::upcast(next_node_shared_runtime_plugin), Vc::upcast(next_node_shared_runtime_plugin),
Vc::upcast(server_external_packages_plugin), Vc::upcast(server_external_packages_plugin),
Vc::upcast(next_external_plugin), Vc::upcast(next_external_plugin),
] ]
} }
ServerContextType::Middleware { .. } => { ServerContextType::Middleware { .. } => {
vec![ vec![Vc::upcast(next_node_shared_runtime_plugin)]
Vc::upcast(module_feature_report_resolve_plugin),
Vc::upcast(unsupported_modules_resolve_plugin),
Vc::upcast(next_node_shared_runtime_plugin),
]
} }
ServerContextType::Instrumentation { .. } => { ServerContextType::Instrumentation { .. } => {
vec![ vec![
Vc::upcast(module_feature_report_resolve_plugin),
Vc::upcast(unsupported_modules_resolve_plugin),
Vc::upcast(next_node_shared_runtime_plugin), Vc::upcast(next_node_shared_runtime_plugin),
Vc::upcast(next_external_plugin), Vc::upcast(next_external_plugin),
] ]
...@@ -271,8 +265,8 @@ pub async fn get_server_resolve_options_context( ...@@ -271,8 +265,8 @@ pub async fn get_server_resolve_options_context(
| ServerContextType::AppRoute { .. } | ServerContextType::AppRoute { .. }
| ServerContextType::Middleware { .. } | ServerContextType::Middleware { .. }
| ServerContextType::Instrumentation => { | ServerContextType::Instrumentation => {
after_resolve_plugins.push(Vc::upcast(invalid_client_only_resolve_plugin)); before_resolve_plugins.push(Vc::upcast(invalid_client_only_resolve_plugin));
after_resolve_plugins.push(Vc::upcast(invalid_styled_jsx_client_only_resolve_plugin)); before_resolve_plugins.push(Vc::upcast(invalid_styled_jsx_client_only_resolve_plugin));
} }
ServerContextType::AppSSR { .. } => { ServerContextType::AppSSR { .. } => {
//[TODO] Build error in this context makes rsc-build-error.ts fail which expects runtime error code //[TODO] Build error in this context makes rsc-build-error.ts fail which expects runtime error code
......
use std::collections::{HashMap, HashSet}; use std::collections::HashMap;
use anyhow::Result; use anyhow::Result;
use lazy_static::lazy_static; use lazy_static::lazy_static;
...@@ -9,15 +9,14 @@ use turbopack_binding::{ ...@@ -9,15 +9,14 @@ use turbopack_binding::{
turbopack::core::{ turbopack::core::{
diagnostics::DiagnosticExt, diagnostics::DiagnosticExt,
file_source::FileSource, file_source::FileSource,
issue::{ issue::{Issue, IssueExt, IssueSeverity, IssueStage, OptionStyledString, StyledString},
unsupported_module::UnsupportedModuleIssue, Issue, IssueExt, IssueSeverity, IssueStage,
OptionStyledString, StyledString,
},
reference_type::ReferenceType, reference_type::ReferenceType,
resolve::{ resolve::{
parse::Request, parse::Request,
pattern::Pattern, plugin::{
plugin::{AfterResolvePlugin, AfterResolvePluginCondition}, AfterResolvePlugin, AfterResolvePluginCondition, BeforeResolvePlugin,
BeforeResolvePluginCondition,
},
ExternalType, ResolveResult, ResolveResultItem, ResolveResultOption, ExternalType, ResolveResult, ResolveResultItem, ResolveResultOption,
}, },
}, },
...@@ -26,8 +25,6 @@ use turbopack_binding::{ ...@@ -26,8 +25,6 @@ use turbopack_binding::{
use crate::{next_server::ServerContextType, next_telemetry::ModuleFeatureTelemetry}; use crate::{next_server::ServerContextType, next_telemetry::ModuleFeatureTelemetry};
lazy_static! { lazy_static! {
static ref UNSUPPORTED_PACKAGES: HashSet<&'static str> = [].into();
static ref UNSUPPORTED_PACKAGE_PATHS: HashSet<(&'static str, &'static str)> = [].into();
// Set of the features we want to track, following existing references in webpack/plugins/telemetry-plugin. // Set of the features we want to track, following existing references in webpack/plugins/telemetry-plugin.
static ref FEATURE_MODULES: HashMap<&'static str, Vec<&'static str>> = HashMap::from([ static ref FEATURE_MODULES: HashMap<&'static str, Vec<&'static str>> = HashMap::from([
( (
...@@ -46,69 +43,6 @@ lazy_static! { ...@@ -46,69 +43,6 @@ lazy_static! {
]); ]);
} }
#[turbo_tasks::value]
pub(crate) struct UnsupportedModulesResolvePlugin {
root: Vc<FileSystemPath>,
}
#[turbo_tasks::value_impl]
impl UnsupportedModulesResolvePlugin {
#[turbo_tasks::function]
pub fn new(root: Vc<FileSystemPath>) -> Vc<Self> {
UnsupportedModulesResolvePlugin { root }.cell()
}
}
#[turbo_tasks::value_impl]
impl AfterResolvePlugin for UnsupportedModulesResolvePlugin {
#[turbo_tasks::function]
fn after_resolve_condition(&self) -> Vc<AfterResolvePluginCondition> {
AfterResolvePluginCondition::new(self.root.root(), Glob::new("**".into()))
}
#[turbo_tasks::function]
async fn after_resolve(
&self,
_fs_path: Vc<FileSystemPath>,
file_path: Vc<FileSystemPath>,
_reference_type: Value<ReferenceType>,
request: Vc<Request>,
) -> Result<Vc<ResolveResultOption>> {
if let Request::Module {
module,
path,
query: _,
fragment: _,
} = &*request.await?
{
// Warn if the package is known not to be supported by Turbopack at the moment.
if UNSUPPORTED_PACKAGES.contains(module.as_str()) {
UnsupportedModuleIssue {
file_path,
package: module.clone(),
package_path: None,
}
.cell()
.emit();
}
if let Pattern::Constant(path) = path {
if UNSUPPORTED_PACKAGE_PATHS.contains(&(module, path)) {
UnsupportedModuleIssue {
file_path,
package: module.clone(),
package_path: Some(path.to_owned()),
}
.cell()
.emit();
}
}
}
Ok(ResolveResultOption::none())
}
}
#[turbo_tasks::value(shared)] #[turbo_tasks::value(shared)]
pub struct InvalidImportModuleIssue { pub struct InvalidImportModuleIssue {
pub file_path: Vc<FileSystemPath>, pub file_path: Vc<FileSystemPath>,
...@@ -187,34 +121,34 @@ impl InvalidImportResolvePlugin { ...@@ -187,34 +121,34 @@ impl InvalidImportResolvePlugin {
} }
#[turbo_tasks::value_impl] #[turbo_tasks::value_impl]
impl AfterResolvePlugin for InvalidImportResolvePlugin { impl BeforeResolvePlugin for InvalidImportResolvePlugin {
#[turbo_tasks::function] #[turbo_tasks::function]
fn after_resolve_condition(&self) -> Vc<AfterResolvePluginCondition> { fn before_resolve_condition(&self) -> Vc<BeforeResolvePluginCondition> {
AfterResolvePluginCondition::new(self.root.root(), Glob::new("**".into())) BeforeResolvePluginCondition::from_modules(Vc::cell(vec![self.invalid_import.clone()]))
} }
#[turbo_tasks::function] #[turbo_tasks::function]
async fn after_resolve( async fn before_resolve(
&self, &self,
_fs_path: Vc<FileSystemPath>, lookup_path: Vc<FileSystemPath>,
context: Vc<FileSystemPath>,
_reference_type: Value<ReferenceType>, _reference_type: Value<ReferenceType>,
request: Vc<Request>, _request: Vc<Request>,
) -> Result<Vc<ResolveResultOption>> { ) -> Result<Vc<ResolveResultOption>> {
if let Request::Module { module, .. } = &*request.await? {
if module.as_str() == self.invalid_import.as_str() {
InvalidImportModuleIssue { InvalidImportModuleIssue {
file_path: context, file_path: lookup_path,
messages: self.message.clone(), messages: self.message.clone(),
// styled-jsx specific resolve error have own message // styled-jsx specific resolve error has its own message
skip_context_message: self.invalid_import == "styled-jsx", skip_context_message: self.invalid_import == "styled-jsx",
} }
.cell() .cell()
.emit(); .emit();
}
}
Ok(ResolveResultOption::none()) Ok(ResolveResultOption::some(
ResolveResult::primary(ResolveResultItem::Error(Vc::cell(
self.message.join("\n").into(),
)))
.cell(),
))
} }
} }
...@@ -402,17 +336,21 @@ impl ModuleFeatureReportResolvePlugin { ...@@ -402,17 +336,21 @@ impl ModuleFeatureReportResolvePlugin {
} }
#[turbo_tasks::value_impl] #[turbo_tasks::value_impl]
impl AfterResolvePlugin for ModuleFeatureReportResolvePlugin { impl BeforeResolvePlugin for ModuleFeatureReportResolvePlugin {
#[turbo_tasks::function] #[turbo_tasks::function]
fn after_resolve_condition(&self) -> Vc<AfterResolvePluginCondition> { fn before_resolve_condition(&self) -> Vc<BeforeResolvePluginCondition> {
AfterResolvePluginCondition::new(self.root.root(), Glob::new("**".into())) BeforeResolvePluginCondition::from_modules(Vc::cell(
FEATURE_MODULES
.keys()
.map(|k| (*k).into())
.collect::<Vec<RcStr>>(),
))
} }
#[turbo_tasks::function] #[turbo_tasks::function]
async fn after_resolve( async fn before_resolve(
&self, &self,
_fs_path: Vc<FileSystemPath>, _lookup_path: Vc<FileSystemPath>,
_context: Vc<FileSystemPath>,
_reference_type: Value<ReferenceType>, _reference_type: Value<ReferenceType>,
request: Vc<Request>, request: Vc<Request>,
) -> Result<Vc<ResolveResultOption>> { ) -> Result<Vc<ResolveResultOption>> {
......
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