Skip to content

Commit da54d10

Browse files
authored
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.
1 parent d7c6ff6 commit da54d10

File tree

12 files changed

+77
-3
lines changed

12 files changed

+77
-3
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export default function Root({ children }) {
2+
return (
3+
<html>
4+
<head></head>
5+
<body>{children}</body>
6+
</html>
7+
)
8+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import path from 'path'
2+
3+
import 'foo'
4+
5+
const projectDir = process.cwd()
6+
path.join(projectDir, 'app', 'static-from-app.txt')
7+
8+
export default function Page() {
9+
return 'hello'
10+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hello
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hello
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import path from 'path'
2+
3+
const projectDir = process.cwd()
4+
path.join(projectDir, 'app', 'static-from-pkg.txt')
5+
6+
export default 'foo'
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "foo",
3+
"version": "0.0.0"
4+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/**
2+
* @type {import('next').NextConfig}
3+
*/
4+
const nextConfig = {}
5+
6+
module.exports = nextConfig
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { nextTestSetup } from 'e2e-utils'
2+
3+
describe('standalone mode - NFT in node_modules', () => {
4+
const dependencies = require('./package.json').dependencies
5+
6+
const { next, skipped } = nextTestSetup({
7+
files: __dirname,
8+
dependencies,
9+
skipDeployment: true,
10+
})
11+
12+
if (skipped) {
13+
return
14+
}
15+
16+
it('should not trace process.cwd calls in node_modules', async () => {
17+
let trace = await next.readJSON('.next/server/app/page.js.nft.json')
18+
19+
expect(trace.files).toContain('../../../app/static-from-app.txt')
20+
expect(trace.files).not.toContain('../../../app/static-from-pkg.txt')
21+
})
22+
})
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"dependencies": {
3+
"foo": "file:./foo"
4+
}
5+
}

turbopack/crates/turbopack-ecmascript/src/analyzer/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3747,7 +3747,7 @@ pub mod test_utils {
37473747
}
37483748
}
37493749
_ => {
3750-
let (mut v, m1) = replace_well_known(v, compile_time_info).await?;
3750+
let (mut v, m1) = replace_well_known(v, compile_time_info, true).await?;
37513751
let m2 = replace_builtin(&mut v);
37523752
let m = m1 || m2 || v.make_nested_operations_unknown();
37533753
return Ok((v, m));

0 commit comments

Comments
 (0)