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 = [
testing = { version = "0.35.25" }
# 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..
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
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
......
......@@ -49,7 +49,7 @@ use crate::{
next_shared::{
resolve::{
get_invalid_server_only_resolve_plugin, ModuleFeatureReportResolvePlugin,
NextSharedRuntimeResolvePlugin, UnsupportedModulesResolvePlugin,
NextSharedRuntimeResolvePlugin,
},
transforms::{
emotion::get_emotion_transform_rule, relay::get_relay_transform_rule,
......@@ -158,13 +158,14 @@ pub async fn get_client_resolve_options_context(
resolved_map: Some(next_client_resolved_map),
browser: true,
module: true,
before_resolve_plugins: vec![Vc::upcast(NextFontLocalResolvePlugin::new(project_path))],
after_resolve_plugins: vec![
Vc::upcast(ModuleFeatureReportResolvePlugin::new(project_path)),
Vc::upcast(UnsupportedModulesResolvePlugin::new(project_path)),
Vc::upcast(NextSharedRuntimeResolvePlugin::new(project_path)),
before_resolve_plugins: vec![
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()
};
Ok(ResolveOptionsContext {
......
......@@ -28,7 +28,6 @@ use crate::{
next_shared::resolve::{
get_invalid_client_only_resolve_plugin, get_invalid_styled_jsx_resolve_plugin,
ModuleFeatureReportResolvePlugin, NextSharedRuntimeResolvePlugin,
UnsupportedModulesResolvePlugin,
},
util::{foreign_code_context_condition, NextRuntime},
};
......@@ -101,44 +100,37 @@ pub async fn get_edge_resolve_options_context(
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::AppSSR { .. }
| ServerContextType::AppRSC { .. } => {
vec![Vc::upcast(NextFontLocalResolvePlugin::new(project_path))]
}
ServerContextType::PagesData { .. }
| ServerContextType::PagesApi { .. }
| ServerContextType::AppRoute { .. }
| ServerContextType::Middleware { .. }
| ServerContextType::Instrumentation => vec![],
};
| ServerContextType::AppSSR { .. }
| ServerContextType::AppRSC { .. }
) {
before_resolve_plugins.push(Vc::upcast(NextFontLocalResolvePlugin::new(project_path)));
}
let mut after_resolve_plugins = match ty {
ServerContextType::Pages { .. }
| ServerContextType::PagesApi { .. }
| ServerContextType::AppSSR { .. } => {
vec![]
}
if matches!(
ty,
ServerContextType::AppRSC { .. }
| ServerContextType::AppRoute { .. }
| ServerContextType::PagesData { .. }
| ServerContextType::Middleware { .. }
| ServerContextType::Instrumentation => {
vec![
Vc::upcast(get_invalid_client_only_resolve_plugin(project_path)),
Vc::upcast(get_invalid_styled_jsx_resolve_plugin(project_path)),
]
}
};
let base_plugins = vec![
Vc::upcast(ModuleFeatureReportResolvePlugin::new(project_path)),
Vc::upcast(UnsupportedModulesResolvePlugin::new(project_path)),
Vc::upcast(NextSharedRuntimeResolvePlugin::new(project_path)),
];
| ServerContextType::AppRoute { .. }
| ServerContextType::PagesData { .. }
| ServerContextType::Middleware { .. }
| ServerContextType::Instrumentation
) {
before_resolve_plugins.push(Vc::upcast(get_invalid_client_only_resolve_plugin(
project_path,
)));
before_resolve_plugins.push(Vc::upcast(get_invalid_styled_jsx_resolve_plugin(
project_path,
)));
}
after_resolve_plugins.extend_from_slice(&base_plugins);
let after_resolve_plugins = vec![Vc::upcast(NextSharedRuntimeResolvePlugin::new(
project_path,
))];
// 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()];
......
......@@ -15,7 +15,7 @@ use turbopack_binding::{
resolve::{
parse::Request,
plugin::{BeforeResolvePlugin, BeforeResolvePluginCondition},
RequestKey, ResolveResult, ResolveResultItem, ResolveResultOption,
ResolveResult, ResolveResultItem, ResolveResultOption,
},
virtual_source::VirtualSource,
},
......@@ -70,7 +70,7 @@ impl NextFontLocalResolvePlugin {
impl BeforeResolvePlugin for NextFontLocalResolvePlugin {
#[turbo_tasks::function]
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(),
))
}
......@@ -125,16 +125,10 @@ impl BeforeResolvePlugin for NextFontLocalResolvePlugin {
.emit();
return Ok(ResolveResultOption::some(
ResolveResult::primary_with_key(
RequestKey::new(font_path.clone()),
ResolveResultItem::Error(Vc::cell(
format!(
"Font file not found: Can't resolve {}'",
font_path
)
ResolveResult::primary(ResolveResultItem::Error(Vc::cell(
format!("Font file not found: Can't resolve {}'", font_path)
.into(),
)),
)
)))
.into(),
));
}
......
......@@ -53,7 +53,7 @@ use crate::{
resolve::{
get_invalid_client_only_resolve_plugin, get_invalid_styled_jsx_resolve_plugin,
ModuleFeatureReportResolvePlugin, NextExternalResolvePlugin,
NextNodeSharedRuntimeResolvePlugin, UnsupportedModulesResolvePlugin,
NextNodeSharedRuntimeResolvePlugin,
},
transforms::{
emotion::get_emotion_transform_rule, get_ecma_transform_rule,
......@@ -126,7 +126,6 @@ pub async fn get_server_resolve_options_context(
foreign_code_context_condition(next_config, project_path).await?;
let root_dir = project_path.root().resolve().await?;
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_styled_jsx_client_only_resolve_plugin =
get_invalid_styled_jsx_resolve_plugin(project_path);
......@@ -202,26 +201,29 @@ pub async fn get_server_resolve_options_context(
let next_node_shared_runtime_plugin =
NextNodeSharedRuntimeResolvePlugin::new(project_path, Value::new(ty));
let before_resolve_plugins = match ty {
let mut before_resolve_plugins = match ty {
ServerContextType::Pages { .. }
| ServerContextType::AppSSR { .. }
| 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::PagesApi { .. }
| ServerContextType::AppRoute { .. }
| 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::PagesApi { .. }
| ServerContextType::PagesData { .. } => {
vec![
Vc::upcast(module_feature_report_resolve_plugin),
Vc::upcast(unsupported_modules_resolve_plugin),
Vc::upcast(next_node_shared_runtime_plugin),
Vc::upcast(external_cjs_modules_plugin),
Vc::upcast(next_external_plugin),
......@@ -231,24 +233,16 @@ pub async fn get_server_resolve_options_context(
| ServerContextType::AppRSC { .. }
| ServerContextType::AppRoute { .. } => {
vec![
Vc::upcast(module_feature_report_resolve_plugin),
Vc::upcast(unsupported_modules_resolve_plugin),
Vc::upcast(next_node_shared_runtime_plugin),
Vc::upcast(server_external_packages_plugin),
Vc::upcast(next_external_plugin),
]
}
ServerContextType::Middleware { .. } => {
vec![
Vc::upcast(module_feature_report_resolve_plugin),
Vc::upcast(unsupported_modules_resolve_plugin),
Vc::upcast(next_node_shared_runtime_plugin),
]
vec![Vc::upcast(next_node_shared_runtime_plugin)]
}
ServerContextType::Instrumentation { .. } => {
vec![
Vc::upcast(module_feature_report_resolve_plugin),
Vc::upcast(unsupported_modules_resolve_plugin),
Vc::upcast(next_node_shared_runtime_plugin),
Vc::upcast(next_external_plugin),
]
......@@ -271,8 +265,8 @@ pub async fn get_server_resolve_options_context(
| ServerContextType::AppRoute { .. }
| ServerContextType::Middleware { .. }
| ServerContextType::Instrumentation => {
after_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_client_only_resolve_plugin));
before_resolve_plugins.push(Vc::upcast(invalid_styled_jsx_client_only_resolve_plugin));
}
ServerContextType::AppSSR { .. } => {
//[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 lazy_static::lazy_static;
......@@ -9,15 +9,14 @@ use turbopack_binding::{
turbopack::core::{
diagnostics::DiagnosticExt,
file_source::FileSource,
issue::{
unsupported_module::UnsupportedModuleIssue, Issue, IssueExt, IssueSeverity, IssueStage,
OptionStyledString, StyledString,
},
issue::{Issue, IssueExt, IssueSeverity, IssueStage, OptionStyledString, StyledString},
reference_type::ReferenceType,
resolve::{
parse::Request,
pattern::Pattern,
plugin::{AfterResolvePlugin, AfterResolvePluginCondition},
plugin::{
AfterResolvePlugin, AfterResolvePluginCondition, BeforeResolvePlugin,
BeforeResolvePluginCondition,
},
ExternalType, ResolveResult, ResolveResultItem, ResolveResultOption,
},
},
......@@ -26,8 +25,6 @@ use turbopack_binding::{
use crate::{next_server::ServerContextType, next_telemetry::ModuleFeatureTelemetry};
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.
static ref FEATURE_MODULES: HashMap<&'static str, Vec<&'static str>> = HashMap::from([
(
......@@ -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)]
pub struct InvalidImportModuleIssue {
pub file_path: Vc<FileSystemPath>,
......@@ -187,34 +121,34 @@ impl InvalidImportResolvePlugin {
}
#[turbo_tasks::value_impl]
impl AfterResolvePlugin for InvalidImportResolvePlugin {
impl BeforeResolvePlugin for InvalidImportResolvePlugin {
#[turbo_tasks::function]
fn after_resolve_condition(&self) -> Vc<AfterResolvePluginCondition> {
AfterResolvePluginCondition::new(self.root.root(), Glob::new("**".into()))
fn before_resolve_condition(&self) -> Vc<BeforeResolvePluginCondition> {
BeforeResolvePluginCondition::from_modules(Vc::cell(vec![self.invalid_import.clone()]))
}
#[turbo_tasks::function]
async fn after_resolve(
async fn before_resolve(
&self,
_fs_path: Vc<FileSystemPath>,
context: Vc<FileSystemPath>,
lookup_path: Vc<FileSystemPath>,
_reference_type: Value<ReferenceType>,
request: Vc<Request>,
_request: Vc<Request>,
) -> Result<Vc<ResolveResultOption>> {
if let Request::Module { module, .. } = &*request.await? {
if module.as_str() == self.invalid_import.as_str() {
InvalidImportModuleIssue {
file_path: context,
messages: self.message.clone(),
// styled-jsx specific resolve error have own message
skip_context_message: self.invalid_import == "styled-jsx",
}
.cell()
.emit();
}
InvalidImportModuleIssue {
file_path: lookup_path,
messages: self.message.clone(),
// styled-jsx specific resolve error has its own message
skip_context_message: self.invalid_import == "styled-jsx",
}
.cell()
.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 {
}
#[turbo_tasks::value_impl]
impl AfterResolvePlugin for ModuleFeatureReportResolvePlugin {
impl BeforeResolvePlugin for ModuleFeatureReportResolvePlugin {
#[turbo_tasks::function]
fn after_resolve_condition(&self) -> Vc<AfterResolvePluginCondition> {
AfterResolvePluginCondition::new(self.root.root(), Glob::new("**".into()))
fn before_resolve_condition(&self) -> Vc<BeforeResolvePluginCondition> {
BeforeResolvePluginCondition::from_modules(Vc::cell(
FEATURE_MODULES
.keys()
.map(|k| (*k).into())
.collect::<Vec<RcStr>>(),
))
}
#[turbo_tasks::function]
async fn after_resolve(
async fn before_resolve(
&self,
_fs_path: Vc<FileSystemPath>,
_context: Vc<FileSystemPath>,
_lookup_path: Vc<FileSystemPath>,
_reference_type: Value<ReferenceType>,
request: Vc<Request>,
) -> 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