Unverified Commit 92bbbb1b authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

Backport: don't define `process.cwd()` in node_modules (#84957)

Backport https://github.com/vercel/next.js/pull/83452
parent f895b727
export default function Root({ children }) {
return (
<html>
<head></head>
<body>{children}</body>
</html>
)
}
import path from 'path'
import 'foo'
const projectDir = process.cwd()
path.join(projectDir, 'app', 'static-from-app.txt')
export default function Page() {
return 'hello'
}
import path from 'path'
const projectDir = process.cwd()
path.join(projectDir, 'app', 'static-from-pkg.txt')
export default 'foo'
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {}
module.exports = nextConfig
import { nextTestSetup } from 'e2e-utils'
describe('standalone mode - NFT in node_modules', () => {
const dependencies = require('./package.json').dependencies
const { next, skipped } = nextTestSetup({
files: __dirname,
dependencies,
skipDeployment: true,
})
if (skipped) {
return
}
it('should not trace process.cwd calls in node_modules', async () => {
let trace = await next.readJSON('.next/server/app/page.js.nft.json')
expect(trace.files).toContain('../../../app/static-from-app.txt')
expect(trace.files).not.toContain('../../../app/static-from-pkg.txt')
})
})
{
"dependencies": {
"foo": "file:./foo"
}
}
...@@ -4032,7 +4032,7 @@ pub mod test_utils { ...@@ -4032,7 +4032,7 @@ pub mod test_utils {
} }
} }
_ => { _ => {
let (mut v, m1) = replace_well_known(v, compile_time_info).await?; let (mut v, m1) = replace_well_known(v, compile_time_info, true).await?;
let m2 = replace_builtin(&mut v); let m2 = replace_builtin(&mut v);
let m = m1 || m2 || v.make_nested_operations_unknown(); let m = m1 || m2 || v.make_nested_operations_unknown();
return Ok((v, m)); return Ok((v, m));
......
...@@ -15,6 +15,7 @@ use crate::analyzer::RequireContextValue; ...@@ -15,6 +15,7 @@ use crate::analyzer::RequireContextValue;
pub async fn replace_well_known( pub async fn replace_well_known(
value: JsValue, value: JsValue,
compile_time_info: Vc<CompileTimeInfo>, compile_time_info: Vc<CompileTimeInfo>,
define_process_cwd: bool,
) -> Result<(JsValue, bool)> { ) -> Result<(JsValue, bool)> {
Ok(match value { Ok(match value {
JsValue::Call(_, box JsValue::WellKnownFunction(kind), args) => ( JsValue::Call(_, box JsValue::WellKnownFunction(kind), args) => (
...@@ -23,6 +24,7 @@ pub async fn replace_well_known( ...@@ -23,6 +24,7 @@ pub async fn replace_well_known(
JsValue::unknown_empty(false, "this is not analyzed yet"), JsValue::unknown_empty(false, "this is not analyzed yet"),
args, args,
compile_time_info, compile_time_info,
define_process_cwd,
) )
.await?, .await?,
true, true,
...@@ -52,6 +54,7 @@ pub async fn well_known_function_call( ...@@ -52,6 +54,7 @@ pub async fn well_known_function_call(
_this: JsValue, _this: JsValue,
args: Vec<JsValue>, args: Vec<JsValue>,
compile_time_info: Vc<CompileTimeInfo>, compile_time_info: Vc<CompileTimeInfo>,
define_process_cwd: bool,
) -> Result<JsValue> { ) -> Result<JsValue> {
Ok(match kind { Ok(match kind {
WellKnownFunctionKind::ObjectAssign => object_assign(args), WellKnownFunctionKind::ObjectAssign => object_assign(args),
...@@ -85,7 +88,8 @@ pub async fn well_known_function_call( ...@@ -85,7 +88,8 @@ pub async fn well_known_function_call(
.as_str() .as_str()
.into(), .into(),
WellKnownFunctionKind::ProcessCwd => { WellKnownFunctionKind::ProcessCwd => {
if let Some(cwd) = &*compile_time_info.environment().cwd().await? { if define_process_cwd && let Some(cwd) = &*compile_time_info.environment().cwd().await?
{
cwd.clone().into() cwd.clone().into()
} else { } else {
JsValue::unknown( JsValue::unknown(
......
...@@ -429,6 +429,7 @@ struct AnalysisState<'a> { ...@@ -429,6 +429,7 @@ struct AnalysisState<'a> {
origin: ResolvedVc<Box<dyn ResolveOrigin>>, origin: ResolvedVc<Box<dyn ResolveOrigin>>,
compile_time_info: ResolvedVc<CompileTimeInfo>, compile_time_info: ResolvedVc<CompileTimeInfo>,
var_graph: &'a VarGraph, var_graph: &'a VarGraph,
define_process_cwd: bool,
/// This is the current state of known values of function /// This is the current state of known values of function
/// arguments. /// arguments.
fun_args_values: Mutex<FxHashMap<u32, Vec<JsValue>>>, fun_args_values: Mutex<FxHashMap<u32, Vec<JsValue>>>,
...@@ -458,6 +459,7 @@ impl AnalysisState<'_> { ...@@ -458,6 +459,7 @@ impl AnalysisState<'_> {
&self.free_var_references, &self.free_var_references,
self.var_graph, self.var_graph,
attributes, attributes,
self.define_process_cwd,
) )
}, },
&self.fun_args_values, &self.fun_args_values,
...@@ -988,6 +990,7 @@ pub(crate) async fn analyse_ecmascript_module_internal( ...@@ -988,6 +990,7 @@ pub(crate) async fn analyse_ecmascript_module_internal(
origin, origin,
compile_time_info, compile_time_info,
var_graph: &var_graph, var_graph: &var_graph,
define_process_cwd: !source.ident().path().await?.path.contains("/node_modules/"),
fun_args_values: Default::default(), fun_args_values: Default::default(),
var_cache: Default::default(), var_cache: Default::default(),
first_import_meta: true, first_import_meta: true,
...@@ -2868,6 +2871,7 @@ async fn value_visitor( ...@@ -2868,6 +2871,7 @@ async fn value_visitor(
>, >,
var_graph: &VarGraph, var_graph: &VarGraph,
attributes: &ImportAttributes, attributes: &ImportAttributes,
define_process_cwd: bool,
) -> Result<(JsValue, bool)> { ) -> Result<(JsValue, bool)> {
let (mut v, modified) = value_visitor_inner( let (mut v, modified) = value_visitor_inner(
origin, origin,
...@@ -2876,6 +2880,7 @@ async fn value_visitor( ...@@ -2876,6 +2880,7 @@ async fn value_visitor(
free_var_references, free_var_references,
var_graph, var_graph,
attributes, attributes,
define_process_cwd,
) )
.await?; .await?;
v.normalize_shallow(); v.normalize_shallow();
...@@ -2892,6 +2897,7 @@ async fn value_visitor_inner( ...@@ -2892,6 +2897,7 @@ async fn value_visitor_inner(
>, >,
var_graph: &VarGraph, var_graph: &VarGraph,
attributes: &ImportAttributes, attributes: &ImportAttributes,
define_process_cwd: bool,
) -> Result<(JsValue, bool)> { ) -> Result<(JsValue, bool)> {
let ImportAttributes { ignore, .. } = *attributes; let ImportAttributes { ignore, .. } = *attributes;
// This check is just an optimization // This check is just an optimization
...@@ -3021,7 +3027,8 @@ async fn value_visitor_inner( ...@@ -3021,7 +3027,8 @@ async fn value_visitor_inner(
v.into_unknown(true, "cross function analyzing is not yet supported") v.into_unknown(true, "cross function analyzing is not yet supported")
} }
_ => { _ => {
let (mut v, mut modified) = replace_well_known(v, compile_time_info).await?; let (mut v, mut modified) =
replace_well_known(v, compile_time_info, define_process_cwd).await?;
modified = replace_builtin(&mut v) || modified; modified = replace_builtin(&mut v) || modified;
modified = modified || v.make_nested_operations_unknown(); modified = modified || v.make_nested_operations_unknown();
return Ok((v, modified)); return Ok((v, modified));
......
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