Unverified Commit da54d104 authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

Turbopack: don't define `process.cwd()` in node_modules (#83452)

Don't compile-time-define `process.cwd()` inside of node_modules.

This means that node_modules cannot break outside of their individual packages anymore, so in the worst case, a given package includes all of itself, but not anymore the whole project.

This means that a `readFileSync(path.join(process.cwd(), "something.txt")))` won't cause that file to be listed in the NFT file anymore.
parent d7c6ff6c
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"
}
}
......@@ -3747,7 +3747,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,
......@@ -67,6 +69,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),
......@@ -100,7 +103,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(
......
......@@ -432,6 +432,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>>>,
......@@ -461,6 +462,7 @@ impl AnalysisState<'_> {
&self.free_var_references,
self.var_graph,
attributes,
self.define_process_cwd,
)
},
&self.fun_args_values,
......@@ -1001,6 +1003,7 @@ pub 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,
......@@ -2927,6 +2930,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,
......@@ -2935,6 +2939,7 @@ async fn value_visitor(
free_var_references,
var_graph,
attributes,
define_process_cwd,
)
.await?;
v.normalize_shallow();
......@@ -2951,6 +2956,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
......@@ -3076,7 +3082,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