Skip to content

Commit f253613

Browse files
authored
fix(pnp): disable patching binding.fstat when possible (#5402)
1 parent 638fba9 commit f253613

5 files changed

Lines changed: 97 additions & 60 deletions

File tree

ā€Ž.pnp.loader.mjsā€Ž

Lines changed: 29 additions & 26 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ā€Ž.yarn/versions/75340348.ymlā€Ž

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
releases:
2+
"@yarnpkg/cli": patch
3+
"@yarnpkg/plugin-pnp": patch
4+
"@yarnpkg/pnp": patch
5+
6+
declined:
7+
- "@yarnpkg/plugin-compat"
8+
- "@yarnpkg/plugin-constraints"
9+
- "@yarnpkg/plugin-dlx"
10+
- "@yarnpkg/plugin-essentials"
11+
- "@yarnpkg/plugin-init"
12+
- "@yarnpkg/plugin-interactive-tools"
13+
- "@yarnpkg/plugin-nm"
14+
- "@yarnpkg/plugin-npm-cli"
15+
- "@yarnpkg/plugin-pack"
16+
- "@yarnpkg/plugin-patch"
17+
- "@yarnpkg/plugin-pnpm"
18+
- "@yarnpkg/plugin-stage"
19+
- "@yarnpkg/plugin-typescript"
20+
- "@yarnpkg/plugin-version"
21+
- "@yarnpkg/plugin-workspace-tools"
22+
- "@yarnpkg/builder"
23+
- "@yarnpkg/core"
24+
- "@yarnpkg/doctor"
25+
- "@yarnpkg/nm"
26+
- "@yarnpkg/pnpify"
27+
- "@yarnpkg/sdks"

ā€Žpackages/yarnpkg-pnp/sources/esm-loader/built-loader.jsā€Ž

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
import fs from 'fs';
1+
import fs from 'fs';
2+
3+
import {HAS_LAZY_LOADED_TRANSLATORS} from './loaderFlags';
24

35
//#region ESM to CJS support
4-
/*
6+
if (!HAS_LAZY_LOADED_TRANSLATORS) {
7+
/*
58
In order to import CJS files from ESM Node does some translating
69
internally[1]. This translator calls an unpatched `readFileSync`[2]
710
which itself calls an internal `tryStatSync`[3] which calls
@@ -16,34 +19,33 @@ import fs from 'fs';
1619
5: https://github.com/nodejs/node/pull/39513
1720
*/
1821

19-
const binding = (process as any).binding(`fs`) as {
20-
fstat: (fd: number, useBigint: false, req: any, ctx: object) => Float64Array;
21-
};
22-
const originalfstat = binding.fstat;
22+
const binding = (process as any).binding(`fs`) as {
23+
fstat: (fd: number, useBigint: false, req: any, ctx: object) => Float64Array;
24+
};
25+
const originalfstat = binding.fstat;
2326

24-
// Those values must be synced with packages/yarnpkg-fslib/sources/ZipOpenFS.ts
25-
//
26-
const ZIP_MASK = 0xff000000;
27-
const ZIP_MAGIC = 0x2a000000;
27+
// Those values must be synced with packages/yarnpkg-fslib/sources/ZipOpenFS.ts
28+
const ZIP_MASK = 0xff000000;
29+
const ZIP_MAGIC = 0x2a000000;
2830

29-
binding.fstat = function(...args) {
30-
const [fd, useBigint, req] = args;
31-
if ((fd & ZIP_MASK) === ZIP_MAGIC && useBigint === false && req === undefined) {
32-
try {
33-
const stats = fs.fstatSync(fd);
34-
// The reverse of this internal util
35-
// https://github.com/nodejs/node/blob/8886b63cf66c29d453fdc1ece2e489dace97ae9d/lib/internal/fs/utils.js#L542-L551
36-
return new Float64Array([
37-
stats.dev,
38-
stats.mode,
39-
stats.nlink,
40-
stats.uid,
41-
stats.gid,
42-
stats.rdev,
43-
stats.blksize,
44-
stats.ino,
45-
stats.size,
46-
stats.blocks,
31+
binding.fstat = function(...args) {
32+
const [fd, useBigint, req] = args;
33+
if ((fd & ZIP_MASK) === ZIP_MAGIC && useBigint === false && req === undefined) {
34+
try {
35+
const stats = fs.fstatSync(fd);
36+
// The reverse of this internal util
37+
// https://github.com/nodejs/node/blob/8886b63cf66c29d453fdc1ece2e489dace97ae9d/lib/internal/fs/utils.js#L542-L551
38+
return new Float64Array([
39+
stats.dev,
40+
stats.mode,
41+
stats.nlink,
42+
stats.uid,
43+
stats.gid,
44+
stats.rdev,
45+
stats.blksize,
46+
stats.ino,
47+
stats.size,
48+
stats.blocks,
4749
// atime sec
4850
// atime ns
4951
// mtime sec
@@ -52,10 +54,11 @@ binding.fstat = function(...args) {
5254
// ctime ns
5355
// birthtime sec
5456
// birthtime ns
55-
]);
56-
} catch {}
57-
}
57+
]);
58+
} catch {}
59+
}
5860

59-
return originalfstat.apply(this, args);
60-
};
61+
return originalfstat.apply(this, args);
62+
};
63+
}
6164
//#endregion

ā€Žpackages/yarnpkg-pnp/sources/esm-loader/loaderFlags.tsā€Ž

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,7 @@ export const HAS_JSON_IMPORT_ASSERTION_REQUIREMENT = major > 17 || (major === 17
1111

1212
// The message switched to using an array in https://github.com/nodejs/node/pull/45348
1313
export const WATCH_MODE_MESSAGE_USES_ARRAYS = major > 19 || (major === 19 && minor >= 2) || (major === 18 && minor >= 13);
14+
15+
// https://github.com/nodejs/node/pull/45659 changed the internal translators to be lazy loaded
16+
// TODO: Update the version range if https://github.com/nodejs/node/pull/46425 lands.
17+
export const HAS_LAZY_LOADED_TRANSLATORS = major > 19 || (major === 19 && minor >= 3);

0 commit comments

Comments
Ā (0)
⚔