Skip to content

Commit 1c79a81

Browse files
committed
build(compat): fix building TypeScript patches (#6186)
**What's the problem this PR addresses?** Depending on the version of Node.js and npm you have available; building the TypeScript patches might not work. **How did you fix it?** - Since "recent" versions of TypeScript have a Volta config specifying the Node.js and npm versions to use, change the script to always use Volta to run `node` and `npm`. - For older versions without a complete Volta config I've added one. - Install using `npm ci` when a lockfile is present as using `npm install` causes some TypeScript builds to fail. **Checklist** - [x] I have read the [Contributing Guide](https://yarnpkg.com/advanced/contributing). - [x] I have set the packages that need to be released for my changes to be effective. - [x] I will check that all automated PR checks pass before the PR gets reviewed.
1 parent 4308dca commit 1c79a81

File tree

4 files changed

+248
-176
lines changed

4 files changed

+248
-176
lines changed

.yarn/versions/72ecc1e8.yml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
declined:
2+
- "@yarnpkg/plugin-compat"

packages/plugin-compat/extra/typescript/gen-typescript-patch.js

+85-15
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const assert = require(`assert`);
12
const cp = require(`child_process`);
23
const crypto = require(`crypto`);
34
const fs = require(`fs`);
@@ -27,91 +28,141 @@ const SLICES = [
2728
to: `426f5a7`,
2829
onto: `e39bdc3`,
2930
range: `>=3.2 <3.5`,
31+
volta: {
32+
node: `14.15.5`,
33+
npm: `6.14.11`,
34+
},
3035
},
3136
{
3237
from: `5d50de3`,
3338
to: `426f5a7`,
3439
onto: `cf7b2d4`,
3540
range: `>=3.5 <=3.6`,
41+
volta: {
42+
node: `14.15.5`,
43+
npm: `6.14.11`,
44+
},
3645
},
3746
{
3847
from: `5d50de3`,
3948
to: `426f5a7`,
4049
onto: `cda54b8`,
4150
range: `>3.6 <3.7`,
51+
volta: {
52+
node: `14.15.5`,
53+
npm: `6.14.11`,
54+
},
4255
},
4356
{
4457
from: `5d50de3`,
4558
to: `2f85932`,
4659
onto: `e39bdc3`,
4760
range: `>=3.7 <3.9`,
61+
volta: {
62+
node: `14.15.5`,
63+
npm: `6.14.11`,
64+
},
4865
},
4966
{
5067
from: `5d50de3`,
5168
to: `3af06df`,
5269
onto: `551f0dd`,
5370
range: `>=3.9 <4.0`,
71+
volta: {
72+
node: `14.15.5`,
73+
npm: `6.14.11`,
74+
},
5475
},
5576
{
5677
from: `6dbdd2f`,
5778
to: `6dbdd2f`,
5879
onto: `56865f7`,
5980
range: `>=4.0 <4.1`,
81+
volta: {
82+
node: `14.15.5`,
83+
npm: `6.14.11`,
84+
},
6085
},
6186
{
6287
from: `746d79b`,
6388
to: `746d79b`,
6489
onto: `69972a3`,
6590
range: `>=4.1 <4.2`,
91+
volta: {
92+
node: `14.15.5`,
93+
npm: `6.14.11`,
94+
},
6695
},
6796
// https://github.com/merceyz/TypeScript/tree/merceyz/pnp-4.2
6897
{
6998
from: `8e0e8703b9c95013aec7819e4593d099cdf7763a`,
7099
to: `178a67b4663d80b0fcbea542e7255b4499b51708`,
71100
onto: `bfc55b5762443c37ecdef08a3b5a4e057b4d1e85`,
72101
range: `>=4.2 <4.3`,
102+
volta: {
103+
node: `14.15.5`,
104+
npm: `6.14.11`,
105+
},
73106
},
74107
// https://github.com/merceyz/TypeScript/tree/merceyz/pnp-4.3
75108
{
76109
from: `530aad19e4ac19d35cb6b200168c91ce86cb0050`,
77110
to: `ffa54c5a104e7940b5c23666ddffbf44878f9d9f`,
78111
onto: `28e3e6ff2f49f1dbf06d31809ec73dbe42f1aa63`,
79112
range: `>=4.3 <4.4`,
113+
volta: {
114+
npm: `6.14.11`,
115+
},
80116
},
81117
// https://github.com/merceyz/TypeScript/tree/merceyz/pnp-4.4
82118
{
83119
from: `793bfe32745bf6797924354b0fd5be62cf01950c`,
84120
to: `20ffca2f3c48591c971e6606a55b7b1820d8a64f`,
85121
onto: `a10409ccaa3604790dc45f52ef0402eb49015dcf`,
86122
range: `>=4.4 <4.5`,
123+
volta: {
124+
npm: `6.14.11`,
125+
},
87126
},
88127
// https://github.com/merceyz/TypeScript/tree/merceyz/pnp-4.5
89128
{
90-
from: `79fe5d206f89e5f1585807bdcd7feeb567a69646`,
91-
to: `fff89a12766fa485e35b1c65d8631d109c4e2c0e`,
92-
onto: `7893f689fedcfe28a499d119d34844840bce9160`,
129+
from: `9232978f8e54f073b5451d0bf2737d42a0fd672f`,
130+
to: `3a2388d39d41d000b5c5f9bcd48096b39fcedf8f`,
131+
onto: `55e13e9115b3cc5458d76c39da1211dc28d7b51f`,
93132
range: `>=4.5.2 <4.6`,
133+
volta: {
134+
npm: `6.14.11`,
135+
},
94136
},
95137
// https://github.com/merceyz/TypeScript/tree/merceyz/pnp-4.6
96138
{
97139
from: `fbec717ef33fc2db5791f2a1d5f9a315e293a50a`,
98140
to: `fbec717ef33fc2db5791f2a1d5f9a315e293a50a`,
99141
onto: `83efc9f0d646bf86a3469e00c5ef5e4f7ab7cb95`,
100142
range: `>=4.6.1-rc <4.7`,
143+
volta: {
144+
npm: `6.14.11`,
145+
},
101146
},
102147
// https://github.com/merceyz/TypeScript/tree/merceyz/pnp-4.7
103148
{
104149
from: `cd8d000510ed2d2910e0ebaa903a51adda546a0a`,
105150
to: `cd8d000510ed2d2910e0ebaa903a51adda546a0a`,
106151
onto: `6e62273fa1e7469b89b589667c2c233789c62176`,
107152
range: `>=4.7.0-beta <4.8`,
153+
volta: {
154+
npm: `6.14.11`,
155+
},
108156
},
109157
// https://github.com/merceyz/TypeScript/tree/merceyz/pnp-4.8.0-beta
110158
{
111159
from: `3287098f4785fd652112beadf3b33a960fcd19aa`,
112160
to: `3287098f4785fd652112beadf3b33a960fcd19aa`,
113161
onto: `9a09c37878a45b06994485fdb510eb4d24587dcb`,
114162
range: `>=4.8.0-beta <4.8.1-rc`,
163+
volta: {
164+
npm: `6.14.11`,
165+
},
115166
},
116167
// https://github.com/merceyz/TypeScript/tree/merceyz/pnp-4.8-stable
117168
{
@@ -344,21 +395,16 @@ async function cloneRepository() {
344395
async function resetGit(hash) {
345396
await execFile(`git`, [`reset`, `--hard`, hash], TS_REPO_SPAWN);
346397
await execFile(`git`, [`clean`, `-df`], TS_REPO_SPAWN);
347-
348-
if (fs.existsSync(path.join(TS_REPO, `package-lock.json`))) {
349-
await execFile(`npm`, [`install`], TS_REPO_SPAWN);
350-
} else {
351-
const date = await execFile(`git`, [`show`, `-s`, `--format=%ci`], TS_REPO_SPAWN);
352-
await execFile(`npm`, [`install`, `--before`, date.toString().trim()], TS_REPO_SPAWN);
353-
}
354398
}
355399

356-
async function buildRepository({from, to, onto}) {
400+
async function buildRepository({from, to, onto, volta}) {
357401
const code = Math.floor(Math.random() * 0x100000000).toString(16).padStart(8, `0`);
358402
const tmpDir = path.join(TMP_DIR, `${code}`);
359403

360404
await resetGit(onto);
361405

406+
const date = await execFile(`git`, [`show`, `-s`, `--format=%ci`], TS_REPO_SPAWN);
407+
362408
if (to) {
363409
let isAncestor;
364410
try {
@@ -375,7 +421,31 @@ async function buildRepository({from, to, onto}) {
375421
}
376422
}
377423

378-
await execFile(fs.existsSync(`${TS_REPO}/node_modules/.bin/hereby`) ? `./node_modules/.bin/hereby` : `./node_modules/.bin/gulp`, [`local`, `LKG`], TS_REPO_SPAWN);
424+
{
425+
const pkgPath = path.join(TS_REPO, `package.json`);
426+
const pkg = JSON.parse(await fs.promises.readFile(pkgPath, `utf8`));
427+
428+
assert(!(pkg.volta?.node && volta?.node), `node version is already set for ${pkg.version}`);
429+
assert(!(pkg.volta?.npm && volta?.npm), `npm version is already set for ${pkg.version}`);
430+
431+
const voltaConfig = {
432+
...volta,
433+
...pkg.volta,
434+
};
435+
assert(voltaConfig.node && voltaConfig.npm, `Missing complete volta configuration for ${pkg.version}, current config: ${JSON.stringify(voltaConfig)}`);
436+
437+
if (JSON.stringify(pkg.volta) !== JSON.stringify(voltaConfig)) {
438+
pkg.volta = voltaConfig;
439+
await fs.promises.writeFile(pkgPath, JSON.stringify(pkg, null, 4));
440+
}
441+
}
442+
443+
if (fs.existsSync(path.join(TS_REPO, `package-lock.json`)))
444+
await execFile(`volta`, [`run`, `npm`, `ci`], TS_REPO_SPAWN);
445+
else
446+
await execFile(`volta`, [`run`, `npm`, `install`, `--before`, date.toString().trim()], TS_REPO_SPAWN);
447+
448+
await execFile(`volta`, [`run`, `node`, fs.existsSync(`${TS_REPO}/node_modules/.bin/hereby`) ? `./node_modules/.bin/hereby` : `./node_modules/.bin/gulp`, `local`, `LKG`], TS_REPO_SPAWN);
379449

380450
// It seems that in some circumstances the build can produce incorrect artifacts. When
381451
// that happens, the final binary is very small. We try to detect that.
@@ -392,7 +462,7 @@ async function buildRepository({from, to, onto}) {
392462
return tmpDir;
393463
}
394464

395-
async function run({from, to, onto, range}) {
465+
async function run({from, to, onto, range, volta}) {
396466
const hash = crypto
397467
.createHash(`md5`)
398468
.update(JSON.stringify({from, to, onto}))
@@ -414,8 +484,8 @@ async function run({from, to, onto, range}) {
414484

415485
await cloneRepository();
416486

417-
const base = await buildRepository({onto});
418-
const patched = await buildRepository({from, to, onto});
487+
const base = await buildRepository({onto, volta});
488+
const patched = await buildRepository({from, to, onto, volta});
419489

420490
const buffer = await execFile(`git`, [`diff`, `--no-index`, base, patched], {checkExitCode: false});
421491

0 commit comments

Comments
 (0)