Skip to content

Commit

Permalink
Proper externals fallback for opentelemetry
Browse files Browse the repository at this point in the history
  • Loading branch information
mischnic authored and arlyon committed Oct 31, 2024
1 parent 18df156 commit 57dd7e8
Show file tree
Hide file tree
Showing 10 changed files with 181 additions and 73 deletions.
4 changes: 3 additions & 1 deletion crates/next-core/src/next_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ pub async fn get_postcss_package_mapping(project_path: Vc<FileSystemPath>) -> Vc

#[turbo_tasks::function]
pub async fn get_external_next_compiled_package_mapping(
project_path: Vc<FileSystemPath>,
package_name: Vc<RcStr>,
) -> Result<Vc<ImportMapping>> {
Ok(ImportMapping::Alternatives(vec![ImportMapping::External(
Some(format!("next/dist/compiled/{}", &*package_name.await?).into()),
ExternalType::CommonJs,
ExternalTraced::Traced,
ExternalTraced::Traced(project_path),
None,
)
.into()])
.cell())
Expand Down
46 changes: 33 additions & 13 deletions crates/next-core/src/next_import_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ pub async fn get_next_client_import_map(

/// Computes the Next-specific client import map.
#[turbo_tasks::function]
pub fn get_next_build_import_map() -> Vc<ImportMap> {
pub fn get_next_build_import_map(project_path: Vc<FileSystemPath>) -> Vc<ImportMap> {
let mut import_map = ImportMap::empty();

insert_package_alias(
Expand All @@ -248,8 +248,13 @@ pub fn get_next_build_import_map() -> Vc<ImportMap> {
next_js_fs().root(),
);

let external =
ImportMapping::External(None, ExternalType::CommonJs, ExternalTraced::Traced).cell();
let external = ImportMapping::External(
None,
ExternalType::CommonJs,
ExternalTraced::Traced(project_path),
None,
)
.cell();

import_map.insert_exact_alias("next", external);
import_map.insert_wildcard_alias("next/", external);
Expand All @@ -259,7 +264,8 @@ pub fn get_next_build_import_map() -> Vc<ImportMap> {
ImportMapping::External(
Some("styled-jsx/style.js".into()),
ExternalType::CommonJs,
ExternalTraced::Traced,
ExternalTraced::Traced(project_path),
None,
)
.cell(),
);
Expand Down Expand Up @@ -324,8 +330,13 @@ pub async fn get_next_server_import_map(

let ty = ty.into_value();

let external: Vc<ImportMapping> =
ImportMapping::External(None, ExternalType::CommonJs, ExternalTraced::Traced).cell();
let external: Vc<ImportMapping> = ImportMapping::External(
None,
ExternalType::CommonJs,
ExternalTraced::Traced(project_path),
None,
)
.cell();

import_map.insert_exact_alias("next/dist/server/require-hook", external);
match ty {
Expand All @@ -343,7 +354,8 @@ pub async fn get_next_server_import_map(
ImportMapping::External(
Some("styled-jsx/style.js".into()),
ExternalType::CommonJs,
ExternalTraced::Traced,
ExternalTraced::Traced(project_path),
None,
)
.cell(),
);
Expand Down Expand Up @@ -565,11 +577,11 @@ async fn insert_next_server_special_aliases(
) -> Result<()> {
let external_cjs_if_node = move |context_dir: Vc<FileSystemPath>, request: &str| match runtime {
NextRuntime::Edge => request_to_import_mapping(context_dir, request),
NextRuntime::NodeJs => external_request_to_cjs_import_mapping(request),
NextRuntime::NodeJs => external_request_to_cjs_import_mapping(context_dir, request),
};
let external_esm_if_node = move |context_dir: Vc<FileSystemPath>, request: &str| match runtime {
NextRuntime::Edge => request_to_import_mapping(context_dir, request),
NextRuntime::NodeJs => external_request_to_esm_import_mapping(request),
NextRuntime::NodeJs => external_request_to_esm_import_mapping(context_dir, request),
};

import_map.insert_exact_alias(
Expand Down Expand Up @@ -1097,22 +1109,30 @@ fn request_to_import_mapping(context_path: Vc<FileSystemPath>, request: &str) ->

/// Creates a direct import mapping to the result of resolving an external
/// request.
fn external_request_to_cjs_import_mapping(request: &str) -> Vc<ImportMapping> {
fn external_request_to_cjs_import_mapping(
context_dir: Vc<FileSystemPath>,
request: &str,
) -> Vc<ImportMapping> {
ImportMapping::External(
Some(request.into()),
ExternalType::CommonJs,
ExternalTraced::Traced,
ExternalTraced::Traced(context_dir),
Some(context_dir),
)
.into()
}

/// Creates a direct import mapping to the result of resolving an external
/// request.
fn external_request_to_esm_import_mapping(request: &str) -> Vc<ImportMapping> {
fn external_request_to_esm_import_mapping(
context_dir: Vc<FileSystemPath>,
request: &str,
) -> Vc<ImportMapping> {
ImportMapping::External(
Some(request.into()),
ExternalType::EcmaScriptModule,
ExternalTraced::Traced,
ExternalTraced::Traced(context_dir),
Some(context_dir),
)
.into()
}
2 changes: 1 addition & 1 deletion crates/next-core/src/next_server/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ impl AfterResolvePlugin for ExternalCjsModulesResolvePlugin {
ResolveResult::primary(ResolveResultItem::External {
name: request_str.into(),
ty: external_type,
traced: ExternalTraced::Traced,
traced: ExternalTraced::Traced(self.project_path),
})
.cell(),
))
Expand Down
10 changes: 5 additions & 5 deletions crates/next-core/src/next_shared/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,14 +204,14 @@ pub(crate) fn get_invalid_styled_jsx_resolve_plugin(

#[turbo_tasks::value]
pub(crate) struct NextExternalResolvePlugin {
root: Vc<FileSystemPath>,
project_path: Vc<FileSystemPath>,
}

#[turbo_tasks::value_impl]
impl NextExternalResolvePlugin {
#[turbo_tasks::function]
pub fn new(root: Vc<FileSystemPath>) -> Vc<Self> {
NextExternalResolvePlugin { root }.cell()
pub fn new(project_path: Vc<FileSystemPath>) -> Vc<Self> {
NextExternalResolvePlugin { project_path }.cell()
}
}

Expand All @@ -220,7 +220,7 @@ impl AfterResolvePlugin for NextExternalResolvePlugin {
#[turbo_tasks::function]
fn after_resolve_condition(&self) -> Vc<AfterResolvePluginCondition> {
AfterResolvePluginCondition::new(
self.root.root(),
self.project_path.root(),
Glob::new("**/next/dist/**/*.{external,runtime.dev,runtime.prod}.js".into()),
)
}
Expand All @@ -245,7 +245,7 @@ impl AfterResolvePlugin for NextExternalResolvePlugin {
ResolveResult::primary(ResolveResultItem::External {
name: specifier.clone(),
ty: ExternalType::CommonJs,
traced: ExternalTraced::Traced,
traced: ExternalTraced::Traced(self.project_path),
})
.into(),
)))
Expand Down
15 changes: 11 additions & 4 deletions crates/next-core/src/next_shared/webpack_rules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ use anyhow::Result;
use turbo_tasks::{RcStr, Vc};
use turbo_tasks_fs::FileSystemPath;
use turbopack::module_options::WebpackLoadersOptions;
use turbopack_core::resolve::options::ImportMapping;
use turbopack_core::resolve::{options::ImportMapping, ExternalTraced, ExternalType};

use self::{babel::maybe_add_babel_loader, sass::maybe_add_sass_loader};
use crate::{next_build::get_external_next_compiled_package_mapping, next_config::NextConfig};
use crate::next_config::NextConfig;

pub(crate) mod babel;
pub(crate) mod sass;
Expand Down Expand Up @@ -33,6 +33,13 @@ pub async fn webpack_loader_options(
}

#[turbo_tasks::function]
fn loader_runner_package_mapping() -> Vc<ImportMapping> {
get_external_next_compiled_package_mapping(Vc::cell("loader-runner".into()))
async fn loader_runner_package_mapping() -> Result<Vc<ImportMapping>> {
Ok(ImportMapping::Alternatives(vec![ImportMapping::External(
Some("next/dist/compiled/loader-runner".into()),
ExternalType::CommonJs,
ExternalTraced::Untraced,
None,
)
.into()])
.cell())
}
70 changes: 57 additions & 13 deletions turbopack/crates/turbopack-core/src/resolve/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ use crate::{
raw_module::RawModule,
reference_type::ReferenceType,
resolve::{
node::{node_cjs_resolve_options, node_esm_resolve_options},
pattern::{read_matches, PatternMatch},
plugin::AfterResolvePlugin,
},
Expand Down Expand Up @@ -374,20 +375,21 @@ impl ModuleResolveResult {
}
}

#[derive(
Copy, Clone, Debug, Eq, PartialEq, Hash, Serialize, Deserialize, TraceRawVcs, TaskInput,
)]
#[derive(Copy, Clone)]
#[turbo_tasks::value(shared)]
pub enum ExternalTraced {
Untraced,
Traced,
Traced(Vc<FileSystemPath>),
}

impl Display for ExternalTraced {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
ExternalTraced::Untraced => write!(f, "Untraced"),
ExternalTraced::Traced => write!(f, "Traced"),
}
impl ExternalTraced {
async fn as_string(&self) -> Result<String> {
Ok(match self {
ExternalTraced::Untraced => "untraced".to_string(),
ExternalTraced::Traced(context) => {
format!("traced from {}", context.to_string().await?)
}
})
}
}

Expand All @@ -411,7 +413,7 @@ impl Display for ExternalType {
}

#[turbo_tasks::value(shared)]
#[derive(Clone, Debug)]
#[derive(Clone)]
pub enum ResolveResultItem {
Source(Vc<Box<dyn Source>>),
External {
Expand Down Expand Up @@ -470,7 +472,7 @@ impl RequestKey {
}

#[turbo_tasks::value(shared)]
#[derive(Clone, Debug)]
#[derive(Clone)]
pub struct ResolveResult {
pub primary: FxIndexMap<RequestKey, ResolveResultItem>,
pub affecting_sources: Vec<Vc<Box<dyn Source>>>,
Expand Down Expand Up @@ -506,7 +508,7 @@ impl ValueToString for ResolveResult {
} => {
result.push_str("external ");
result.push_str(s);
write!(result, " ({}, {})", ty, traced)?;
write!(result, " ({}, {})", ty, traced.as_string().await?)?;
}
ResolveResultItem::Ignore => {
result.push_str("ignore");
Expand Down Expand Up @@ -2612,6 +2614,48 @@ async fn resolve_import_map_result(
))
}
}
ImportMapResult::External(name, ty, traced, primary_alt) => {
let result = Some(
ResolveResult::primary(ResolveResultItem::External {
name: name.clone(),
ty: *ty,
traced: *traced,
})
.cell(),
);

if let Some(context_dir) = primary_alt {
let request = Request::parse_string(name.clone());

// We must avoid cycles during resolving
if request.resolve().await? == *original_request
&& context_dir.resolve().await? == *original_lookup_path
{
None
} else {
let resolve_internal = resolve_internal(
*context_dir,
request,
match ty {
ExternalType::Url => options,
// TODO is that root correct?
ExternalType::CommonJs => node_cjs_resolve_options(context_dir.root()),
ExternalType::EcmaScriptModule => {
node_esm_resolve_options(context_dir.root())
}
},
);

if *resolve_internal.is_unresolvable().await? {
None
} else {
result
}
}
} else {
result
}
}
ImportMapResult::Alternatives(list) => {
let results = list
.iter()
Expand Down
Loading

0 comments on commit 57dd7e8

Please sign in to comment.