Skip to content

Commit b3b5562

Browse files
committed
Set env in tracing context
1 parent 21e98e3 commit b3b5562

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

turbopack/crates/turbopack-core/src/compile_time_info.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ impl From<serde_json::Value> for CompileTimeDefineValue {
138138
}
139139
}
140140

141+
#[turbo_tasks::value(transparent)]
142+
#[derive(Debug, Clone)]
143+
pub struct OptionCompileTimeDefineValue(pub Option<CompileTimeDefineValue>);
144+
141145
#[turbo_tasks::value(serialization = "auto_for_input")]
142146
#[derive(Debug, Clone, Hash)]
143147
pub enum DefineableNameSegment {
@@ -299,6 +303,13 @@ impl CompileTimeInfo {
299303
pub fn environment(&self) -> Vc<Environment> {
300304
*self.environment
301305
}
306+
307+
#[turbo_tasks::function]
308+
pub async fn process_env_node_env(&self) -> Result<Vc<OptionCompileTimeDefineValue>> {
309+
let key: Vec<DefineableNameSegment> =
310+
vec![("process".into()), ("env".into()), "NODE_ENV".into()];
311+
Ok(Vc::cell(self.defines.await?.get(&key).cloned()))
312+
}
302313
}
303314

304315
pub struct CompileTimeInfoBuilder {

turbopack/crates/turbopack/src/lib.rs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ use graph::{aggregate, AggregatedGraph, AggregatedGraphNodeContent};
2525
use module_options::{ModuleOptions, ModuleOptionsContext, ModuleRuleEffect, ModuleType};
2626
use tracing::{field::Empty, Instrument};
2727
use turbo_rcstr::RcStr;
28-
use turbo_tasks::{ResolvedVc, Value, ValueToString, Vc};
28+
use turbo_tasks::{FxIndexMap, ResolvedVc, Value, ValueToString, Vc};
2929
use turbo_tasks_fs::{glob::Glob, FileSystemPath};
3030
pub use turbopack_core::condition;
3131
use turbopack_core::{
3232
asset::Asset,
3333
chunk::SourceMapsType,
34-
compile_time_info::CompileTimeInfo,
34+
compile_time_info::{CompileTimeInfo, OptionCompileTimeDefineValue},
3535
context::{AssetContext, ProcessResult},
3636
environment::{Environment, ExecutionEnvironment, NodeJsEnvironment},
3737
issue::{module::ModuleIssue, IssueExt, StyledString},
@@ -659,7 +659,10 @@ async fn process_default_internal(
659659
}
660660

661661
#[turbo_tasks::function]
662-
async fn externals_tracing_module_context(ty: ExternalType) -> Result<Vc<ModuleAssetContext>> {
662+
async fn externals_tracing_module_context(
663+
ty: ExternalType,
664+
process_env_node_env: Vc<OptionCompileTimeDefineValue>,
665+
) -> Result<Vc<ModuleAssetContext>> {
663666
let env = Environment::new(Value::new(ExecutionEnvironment::NodeJsLambda(
664667
NodeJsEnvironment::default().resolved_cell(),
665668
)))
@@ -677,9 +680,23 @@ async fn externals_tracing_module_context(ty: ExternalType) -> Result<Vc<ModuleA
677680
..Default::default()
678681
};
679682

683+
let mut compile_time_info = CompileTimeInfo::builder(env);
684+
if let Some(env) = &*process_env_node_env.await? {
685+
compile_time_info = compile_time_info.defines(ResolvedVc::cell(FxIndexMap::from_iter([
686+
(
687+
[("process".into()), ("env".into()), "NODE_ENV".into()].into(),
688+
env.clone(),
689+
),
690+
(
691+
[("process".into()), ("env".into()), "TURBOPACK".into()].into(),
692+
"1".into(),
693+
),
694+
])));
695+
}
696+
680697
Ok(ModuleAssetContext::new_without_replace_externals(
681698
Default::default(),
682-
CompileTimeInfo::builder(env).cell().await?,
699+
compile_time_info.cell().await?,
683700
ModuleOptionsContext {
684701
ecmascript: EcmascriptOptionsContext {
685702
source_maps: SourceMapsType::None,
@@ -805,7 +822,10 @@ impl AssetContext for ModuleAssetContext {
805822
.await?
806823
.enable_externals_tracing,
807824
) {
808-
let externals_context = externals_tracing_module_context(ty);
825+
let externals_context = externals_tracing_module_context(
826+
ty,
827+
self.compile_time_info().process_env_node_env(),
828+
);
809829
let root_origin = tracing_root.join("_".into());
810830

811831
let external_result = externals_context

0 commit comments

Comments
 (0)