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 {
}
}
_ => {
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 m = m1 || m2 || v.make_nested_operations_unknown();
return Ok((v, m));
......
......@@ -15,6 +15,7 @@ use crate::analyzer::RequireContextValue;
pub async fn replace_well_known(
value: JsValue,
compile_time_info: Vc<CompileTimeInfo>,
define_process_cwd: bool,
) -> Result<(JsValue, bool)> {
Ok(match value {
JsValue::Call(_, box JsValue::WellKnownFunction(kind), args) => (
......@@ -23,6 +24,7 @@ pub async fn replace_well_known(
JsValue::unknown_empty(false, "this is not analyzed yet"),
args,
compile_time_info,
define_process_cwd,
)
.await?,
true,
......@@ -52,6 +54,7 @@ pub async fn well_known_function_call(
_this: JsValue,
args: Vec<JsValue>,
compile_time_info: Vc<CompileTimeInfo>,
define_process_cwd: bool,
) -> Result<JsValue> {
Ok(match kind {
WellKnownFunctionKind::ObjectAssign => object_assign(args),
......@@ -85,7 +88,8 @@ pub async fn well_known_function_call(
.as_str()
.into(),
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()
} else {
JsValue::unknown(
......
......@@ -429,6 +429,7 @@ struct AnalysisState<'a> {
origin: ResolvedVc<Box<dyn ResolveOrigin>>,
compile_time_info: ResolvedVc<CompileTimeInfo>,
var_graph: &'a VarGraph,
define_process_cwd: bool,
/// This is the current state of known values of function
/// arguments.
fun_args_values: Mutex<FxHashMap<u32, Vec<JsValue>>>,
......@@ -458,6 +459,7 @@ impl AnalysisState<'_> {
&self.free_var_references,
self.var_graph,
attributes,
self.define_process_cwd,
)
},
&self.fun_args_values,
......@@ -988,6 +990,7 @@ pub(crate) async fn analyse_ecmascript_module_internal(
origin,
compile_time_info,
var_graph: &var_graph,
define_process_cwd: !source.ident().path().await?.path.contains("/node_modules/"),
fun_args_values: Default::default(),
var_cache: Default::default(),
first_import_meta: true,
......@@ -2868,6 +2871,7 @@ async fn value_visitor(
>,
var_graph: &VarGraph,
attributes: &ImportAttributes,
define_process_cwd: bool,
) -> Result<(JsValue, bool)> {
let (mut v, modified) = value_visitor_inner(
origin,
......@@ -2876,6 +2880,7 @@ async fn value_visitor(
free_var_references,
var_graph,
attributes,
define_process_cwd,
)
.await?;
v.normalize_shallow();
......@@ -2892,6 +2897,7 @@ async fn value_visitor_inner(
>,
var_graph: &VarGraph,
attributes: &ImportAttributes,
define_process_cwd: bool,
) -> Result<(JsValue, bool)> {
let ImportAttributes { ignore, .. } = *attributes;
// This check is just an optimization
......@@ -3021,7 +3027,8 @@ async fn value_visitor_inner(
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 = modified || v.make_nested_operations_unknown();
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