From 7d9533677909f29ca92f2ed5f40f99bb916ea400 Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Thu, 21 Mar 2024 18:43:58 +0100 Subject: [PATCH] Revert "feat(next-core): support unsupported module runtime error (#63491)" (#63575) ### What? It's causing `__import_unsupported` is not defined in `instrumentation` context --- .../crates/next-core/src/next_edge/mod.rs | 1 - .../next-core/src/next_edge/unsupported.rs | 77 ------------------- .../crates/next-core/src/next_import_map.rs | 72 ----------------- .../edge-runtime-module-errors/test/utils.js | 5 +- test/turbopack-dev-tests-manifest.json | 24 +++--- 5 files changed, 15 insertions(+), 164 deletions(-) delete mode 100644 packages/next-swc/crates/next-core/src/next_edge/unsupported.rs diff --git a/packages/next-swc/crates/next-core/src/next_edge/mod.rs b/packages/next-swc/crates/next-core/src/next_edge/mod.rs index 332bda64270ce..6c72552f20d87 100644 --- a/packages/next-swc/crates/next-core/src/next_edge/mod.rs +++ b/packages/next-swc/crates/next-core/src/next_edge/mod.rs @@ -1,4 +1,3 @@ pub mod context; pub mod entry; pub mod route_regex; -pub mod unsupported; diff --git a/packages/next-swc/crates/next-core/src/next_edge/unsupported.rs b/packages/next-swc/crates/next-core/src/next_edge/unsupported.rs deleted file mode 100644 index 1318c95c8f9da..0000000000000 --- a/packages/next-swc/crates/next-core/src/next_edge/unsupported.rs +++ /dev/null @@ -1,77 +0,0 @@ -use anyhow::Result; -use indoc::formatdoc; -use turbo_tasks::Vc; -use turbo_tasks_fs::File; -use turbopack_binding::{ - turbo::tasks_fs::FileSystemPath, - turbopack::{ - core::{ - asset::AssetContent, - resolve::{ - options::{ImportMapResult, ImportMapping, ImportMappingReplacement}, - parse::Request, - ResolveResult, - }, - virtual_source::VirtualSource, - }, - node::execution_context::ExecutionContext, - }, -}; - -/// Intercepts requests for the given request to `unsupported` error messages -/// by returning a VirtualSource proxies to any import request to raise a -/// runtime error. -/// -/// This can be used by import map alias, refer `next_import_map` for the setup. -#[turbo_tasks::value(shared)] -pub struct NextEdgeUnsupportedModuleReplacer { - project_path: Vc, - execution_context: Vc, -} - -#[turbo_tasks::value_impl] -impl NextEdgeUnsupportedModuleReplacer { - #[turbo_tasks::function] - pub fn new( - project_path: Vc, - execution_context: Vc, - ) -> Vc { - Self::cell(NextEdgeUnsupportedModuleReplacer { - project_path, - execution_context, - }) - } -} - -#[turbo_tasks::value_impl] -impl ImportMappingReplacement for NextEdgeUnsupportedModuleReplacer { - #[turbo_tasks::function] - fn replace(&self, _capture: String) -> Vc { - ImportMapping::Ignore.into() - } - - #[turbo_tasks::function] - async fn result( - &self, - context: Vc, - request: Vc, - ) -> Result> { - let request = &*request.await?; - if let Request::Module { module, .. } = request { - // packages/next/src/server/web/globals.ts augments global with - // `__import_unsupported` and necessary functions. - let code = formatdoc! { - r#" - __turbopack_export_namespace__(__import_unsupported(`{module}`)); - "# - }; - let content = AssetContent::file(File::from(code).into()); - let source = VirtualSource::new(context, content); - return Ok( - ImportMapResult::Result(ResolveResult::source(Vc::upcast(source)).into()).into(), - ); - }; - - Ok(ImportMapResult::NoEntry.into()) - } -} diff --git a/packages/next-swc/crates/next-core/src/next_import_map.rs b/packages/next-swc/crates/next-core/src/next_import_map.rs index 4ae35b2d36cd8..0a8e8ca90fec7 100644 --- a/packages/next-swc/crates/next-core/src/next_import_map.rs +++ b/packages/next-swc/crates/next-core/src/next_import_map.rs @@ -26,7 +26,6 @@ use crate::{ mode::NextMode, next_client::context::ClientContextType, next_config::NextConfig, - next_edge::unsupported::NextEdgeUnsupportedModuleReplacer, next_font::{ google::{ NextFontGoogleCssModuleReplacer, NextFontGoogleFontFileReplacer, NextFontGoogleReplacer, @@ -39,57 +38,6 @@ use crate::{ util::NextRuntime, }; -const NODE_INTERNALS: [&str; 48] = [ - "assert", - "async_hooks", - "child_process", - "cluster", - "console", - "constants", - "dgram", - "diagnostics_channel", - "dns", - "dns/promises", - "domain", - "events", - "fs", - "fs/promises", - "http", - "http2", - "https", - "inspector", - "module", - "net", - "os", - "path", - "path/posix", - "path/win32", - "perf_hooks", - "process", - "punycode", - "querystring", - "readline", - "repl", - "stream", - "stream/promises", - "stream/web", - "string_decoder", - "sys", - "timers", - "timers/promises", - "tls", - "trace_events", - "tty", - "util", - "util/types", - "v8", - "vm", - "wasi", - "worker_threads", - "zlib", - "pnpapi", -]; - // Make sure to not add any external requests here. /// Computes the Next-specific client import map. #[turbo_tasks::function] @@ -468,29 +416,9 @@ pub async fn get_next_edge_import_map( ) .await?; - insert_unsupported_node_internal_aliases(&mut import_map, project_path, execution_context); - Ok(import_map.cell()) } -/// Insert default aliases for the node.js's internal to raise unsupported -/// runtime errors. User may provide polyfills for their own by setting user -/// config's alias. -fn insert_unsupported_node_internal_aliases( - import_map: &mut ImportMap, - project_path: Vc, - execution_context: Vc, -) { - let unsupported_replacer = ImportMapping::Dynamic(Vc::upcast( - NextEdgeUnsupportedModuleReplacer::new(project_path, execution_context), - )) - .into(); - - NODE_INTERNALS.iter().for_each(|module| { - import_map.insert_alias(AliasPattern::exact(*module), unsupported_replacer); - }); -} - pub fn get_next_client_resolved_map( _context: Vc, _root: Vc, diff --git a/test/integration/edge-runtime-module-errors/test/utils.js b/test/integration/edge-runtime-module-errors/test/utils.js index 83e07121a9383..b0ad33d59459a 100644 --- a/test/integration/edge-runtime-module-errors/test/utils.js +++ b/test/integration/edge-runtime-module-errors/test/utils.js @@ -55,10 +55,7 @@ export function expectUnsupportedModuleDevError( output = context.logs.output ) { expectUnsupportedModuleProdError(moduleName, output) - // turbopack have correct error overly, but doesn't emit those into cli - if (!process.env.TURBOPACK) { - expect(stripAnsi(output)).toContain(importStatement) - } + expect(stripAnsi(output)).toContain(importStatement) const moduleNotSupportedMessage = getUnsupportedModule(moduleName) expect(responseText).toContain(escapeLF(moduleNotSupportedMessage)) diff --git a/test/turbopack-dev-tests-manifest.json b/test/turbopack-dev-tests-manifest.json index a4a90d0c29108..13501f2cc6a30 100644 --- a/test/turbopack-dev-tests-manifest.json +++ b/test/turbopack-dev-tests-manifest.json @@ -9531,17 +9531,21 @@ "test/integration/edge-runtime-module-errors/test/index.test.js": { "passed": [ "Edge runtime code with imports Edge API importing vanilla 3rd party module does not throw in dev at runtime", + "Edge runtime code with imports Edge API using Buffer polyfill does not throw in dev at runtime", "Edge runtime code with imports Middleware importing vanilla 3rd party module does not throw in dev at runtime", - "Edge runtime code with imports Edge API dynamically importing node.js module development mode throws unsupported module error in dev at runtime and highlights the faulty line", - "Edge runtime code with imports Middleware dynamically importing node.js module development mode throws unsupported module error in dev at runtime and highlights the faulty line", - "Edge runtime code with imports Edge API dynamically importing node.js module in a lib development mode throws unsupported module error in dev at runtime and highlights the faulty line", - "Edge runtime code with imports Middleware dynamically importing node.js module in a lib development mode throws unsupported module error in dev at runtime and highlights the faulty line", "Edge runtime code with imports Edge API using Buffer polyfill does not throw in dev at runtime", "Edge runtime code with imports Middleware using Buffer polyfill does not throw in dev at runtime", "Edge runtime code with imports Edge API statically importing 3rd party module throws not-found module error in dev at runtime and highlights the faulty line", "Edge runtime code with imports Middleware statically importing 3rd party module throws not-found module error in dev at runtime and highlights the faulty line" ], - "failed": [], + "failed": [ + "Edge runtime code with imports Edge API dynamically importing node.js module development mode throws unsupported module error in dev at runtime and highlights the faulty line", + "Edge runtime code with imports Middleware dynamically importing node.js module development mode throws unsupported module error in dev at runtime and highlights the faulty line", + "Edge runtime code with imports Edge API dynamically importing node.js module in a lib development mode throws unsupported module error in dev at runtime and highlights the faulty line", + "Edge runtime code with imports Middleware dynamically importing node.js module in a lib development mode throws unsupported module error in dev at runtime and highlights the faulty line", + "Edge runtime code with imports Middleware dynamically importing node.js module development mode throws unsupported module error in dev at runtime and highlights the faulty line", + "Edge runtime code with imports Middleware dynamically importing node.js module in a lib development mode throws unsupported module error in dev at runtime and highlights the faulty line" + ], "pending": [ "Edge runtime code with imports Edge API dynamically importing node.js module in a lib production mode throws unsupported module error in production at runtime and prints error on logs", "Edge runtime code with imports Edge API dynamically importing node.js module production mode throws unsupported module error in production at runtime and prints error on logs", @@ -9559,16 +9563,16 @@ }, "test/integration/edge-runtime-module-errors/test/module-imports.test.js": { "passed": [ - "Edge runtime code with imports Edge API importing unused node.js module does not throw in dev at runtime", - "Edge runtime code with imports Middleware importing unused node.js module does not throw in dev at runtime", - "Edge runtime code with imports Edge API statically importing node.js module throws unsupported module error in dev at runtime and highlights the faulty line", - "Edge runtime code with imports Middleware statically importing node.js module throws unsupported module error in dev at runtime and highlights the faulty line" + "Edge runtime code with imports Edge API importing unused node.js module does not throw in dev at runtime" ], "failed": [ "Edge runtime code with imports Edge API dynamically importing 3rd party module throws not-found module error in dev at runtime and highlights the faulty line", "Edge runtime code with imports Edge API importing unused 3rd party module throws not-found module error in dev at runtime and highlights the faulty line", + "Edge runtime code with imports Edge API statically importing node.js module throws unsupported module error in dev at runtime and highlights the faulty line", "Edge runtime code with imports Middleware dynamically importing 3rd party module throws not-found module error in dev at runtime and highlights the faulty line", - "Edge runtime code with imports Middleware importing unused 3rd party module throws not-found module error in dev at runtime and highlights the faulty line" + "Edge runtime code with imports Middleware importing unused 3rd party module throws not-found module error in dev at runtime and highlights the faulty line", + "Edge runtime code with imports Middleware importing unused node.js module does not throw in dev at runtime", + "Edge runtime code with imports Middleware statically importing node.js module throws unsupported module error in dev at runtime and highlights the faulty line" ], "pending": [ "Edge runtime code with imports Edge API dynamically importing 3rd party module production mode does not build and reports module not found error",