-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
yarn.ts
301 lines (283 loc) · 10.2 KB
/
yarn.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
import is from '@sindresorhus/is';
import semver from 'semver';
import { quote } from 'shlex';
import upath from 'upath';
import { GlobalConfig } from '../../../../config/global';
import {
SYSTEM_INSUFFICIENT_DISK_SPACE,
TEMPORARY_ERROR,
} from '../../../../constants/error-messages';
import { logger } from '../../../../logger';
import { ExternalHostError } from '../../../../types/errors/external-host-error';
import { exec } from '../../../../util/exec';
import type {
ExecOptions,
ExtraEnv,
ToolConstraint,
} from '../../../../util/exec/types';
import {
localPathIsFile,
readLocalFile,
writeLocalFile,
} from '../../../../util/fs';
import { newlineRegex, regEx } from '../../../../util/regex';
import { uniqueStrings } from '../../../../util/string';
import { NpmDatasource } from '../../../datasource/npm';
import type { PostUpdateConfig, Upgrade } from '../../types';
import type { NpmManagerData } from '../types';
import { getNodeToolConstraint } from './node-version';
import type { GenerateLockFileResult } from './types';
export async function checkYarnrc(
lockFileDir: string
): Promise<{ offlineMirror: boolean; yarnPath: string | null }> {
let offlineMirror = false;
let yarnPath: string | null = null;
try {
const yarnrc = await readLocalFile(
upath.join(lockFileDir, '.yarnrc'),
'utf8'
);
if (is.string(yarnrc)) {
const mirrorLine = yarnrc
.split(newlineRegex)
.find((line) => line.startsWith('yarn-offline-mirror '));
offlineMirror = !!mirrorLine;
const pathLine = yarnrc
.split(newlineRegex)
.find((line) => line.startsWith('yarn-path '));
if (pathLine) {
yarnPath = pathLine.replace(regEx(/^yarn-path\s+"?(.+?)"?$/), '$1');
}
if (yarnPath) {
// resolve binary relative to `yarnrc`
yarnPath = upath.join(lockFileDir, yarnPath);
}
const yarnBinaryExists = yarnPath
? await localPathIsFile(yarnPath)
: false;
if (!yarnBinaryExists) {
const scrubbedYarnrc = yarnrc.replace(
regEx(/^yarn-path\s+"?.+?"?$/gm),
''
);
await writeLocalFile(
upath.join(lockFileDir, '.yarnrc'),
scrubbedYarnrc
);
yarnPath = null;
}
}
} catch (err) /* istanbul ignore next */ {
// not found
}
return { offlineMirror, yarnPath };
}
export function getOptimizeCommand(
fileName = '/home/ubuntu/.npm-global/lib/node_modules/yarn/lib/cli.js'
): string {
return `sed -i 's/ steps,/ steps.slice(0,1),/' ${quote(fileName)}`;
}
export function isYarnUpdate(upgrade: Upgrade): boolean {
return upgrade.depType === 'packageManager' && upgrade.depName === 'yarn';
}
export async function generateLockFile(
lockFileDir: string,
env: NodeJS.ProcessEnv,
config: Partial<PostUpdateConfig<NpmManagerData>> = {},
upgrades: Upgrade[] = []
): Promise<GenerateLockFileResult> {
const lockFileName = upath.join(lockFileDir, 'yarn.lock');
logger.debug(`Spawning yarn install to create ${lockFileName}`);
let lockFile: string | null = null;
try {
const toolConstraints: ToolConstraint[] = [
await getNodeToolConstraint(config, upgrades),
];
const yarnUpdate = upgrades.find(isYarnUpdate);
const yarnCompatibility = yarnUpdate
? yarnUpdate.newValue
: config.constraints?.yarn;
const minYarnVersion =
semver.validRange(yarnCompatibility) &&
semver.minVersion(yarnCompatibility);
const isYarn1 = !minYarnVersion || minYarnVersion.major === 1;
const isYarnDedupeAvailable =
minYarnVersion && semver.gte(minYarnVersion, '2.2.0');
const isYarnModeAvailable =
minYarnVersion && semver.gte(minYarnVersion, '3.0.0');
const yarnTool: ToolConstraint = {
toolName: 'yarn',
constraint: '^1.22.18', // needs to be a v1 yarn, otherwise v2 will be installed
};
if (!isYarn1 && config.managerData?.hasPackageManager) {
toolConstraints.push({ toolName: 'corepack' });
} else {
toolConstraints.push(yarnTool);
if (isYarn1 && minYarnVersion) {
yarnTool.constraint = yarnCompatibility;
}
}
const extraEnv: ExtraEnv = {
NPM_CONFIG_CACHE: env.NPM_CONFIG_CACHE,
npm_config_store: env.npm_config_store,
CI: 'true',
};
const commands: string[] = [];
let cmdOptions = ''; // should have a leading space
if (config.skipInstalls !== false) {
if (isYarn1) {
const { offlineMirror, yarnPath } = await checkYarnrc(lockFileDir);
if (!offlineMirror) {
logger.debug('Updating yarn.lock only - skipping node_modules');
// The following change causes Yarn 1.x to exit gracefully after updating the lock file but without installing node_modules
yarnTool.toolName = 'yarn-slim';
if (yarnPath) {
commands.push(getOptimizeCommand(yarnPath) + ' || true');
}
}
} else if (isYarnModeAvailable) {
// Don't run the link step and only fetch what's necessary to compute an updated lockfile
cmdOptions += ' --mode=update-lockfile';
}
}
if (isYarn1) {
cmdOptions +=
' --ignore-engines --ignore-platform --network-timeout 100000';
extraEnv.YARN_CACHE_FOLDER = env.YARN_CACHE_FOLDER;
} else {
extraEnv.YARN_ENABLE_IMMUTABLE_INSTALLS = 'false';
extraEnv.YARN_HTTP_TIMEOUT = '100000';
extraEnv.YARN_GLOBAL_FOLDER = env.YARN_GLOBAL_FOLDER;
if (!config.managerData?.yarnZeroInstall) {
logger.debug('Enabling global cache as zero-install is not detected');
extraEnv.YARN_ENABLE_GLOBAL_CACHE = '1';
}
}
if (!GlobalConfig.get('allowScripts') || config.ignoreScripts) {
if (isYarn1) {
cmdOptions += ' --ignore-scripts';
} else if (isYarnModeAvailable) {
if (config.skipInstalls === false) {
// Don't run the build scripts
cmdOptions += ' --mode=skip-build';
}
} else {
extraEnv.YARN_ENABLE_SCRIPTS = '0';
}
}
const execOptions: ExecOptions = {
cwdFile: lockFileName,
extraEnv,
docker: {
image: 'sidecar',
},
toolConstraints,
};
// istanbul ignore if
if (GlobalConfig.get('exposeAllEnv')) {
extraEnv.NPM_AUTH = env.NPM_AUTH;
extraEnv.NPM_EMAIL = env.NPM_EMAIL;
}
if (yarnUpdate && !isYarn1) {
logger.debug('Updating Yarn binary');
// TODO: types (#7154)
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
commands.push(`yarn set version ${yarnUpdate.newValue}`);
}
// This command updates the lock file based on package.json
commands.push(`yarn install${cmdOptions}`);
// rangeStrategy = update-lockfile
const lockUpdates = upgrades.filter((upgrade) => upgrade.isLockfileUpdate);
if (lockUpdates.length) {
logger.debug('Performing lockfileUpdate (yarn)');
if (isYarn1) {
// `yarn upgrade` updates based on the version range specified in the package file
// note - this can hit a yarn bug, see https://github.com/yarnpkg/yarn/issues/8236
commands.push(
`yarn upgrade ${lockUpdates
.map((update) => update.depName)
.filter(is.string)
.filter(uniqueStrings)
.join(' ')}${cmdOptions}`
);
} else {
// `yarn up` updates to the latest release, so the range should be specified
commands.push(
`yarn up ${lockUpdates
// TODO: types (#7154)
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
.map((update) => `${update.depName}@${update.newValue}`)
.filter(uniqueStrings)
.join(' ')}${cmdOptions}`
);
}
}
// postUpdateOptions
['fewer', 'highest'].forEach((s) => {
if (
config.postUpdateOptions?.includes(
`yarnDedupe${s.charAt(0).toUpperCase()}${s.slice(1)}`
)
) {
logger.debug(`Performing yarn dedupe ${s}`);
if (isYarn1) {
commands.push(`npx yarn-deduplicate --strategy ${s}`);
// Run yarn again in case any changes are necessary
commands.push(`yarn install${cmdOptions}`);
} else if (isYarnDedupeAvailable && s === 'highest') {
commands.push(`yarn dedupe --strategy ${s}${cmdOptions}`);
} else {
logger.debug(`yarn dedupe ${s} not available`);
}
}
});
if (upgrades.find((upgrade) => upgrade.isLockFileMaintenance)) {
logger.debug(
`Removing ${lockFileName} first due to lock file maintenance upgrade`
);
// Note: Instead of just deleting the `yarn.lock` file, we just wipe it
// and keep an empty lock file. Deleting the lock file could result in different
// Yarn semantics. e.g. Yarn 2+ will error when `yarn install` is executed in
// a subdirectory which is not part of a Yarn workspace. Yarn suggests to create
// an empty lock file if a subdirectory should be treated as its own workspace.
// https://github.com/yarnpkg/berry/blob/20612e82d26ead5928cc27bf482bb8d62dde87d3/packages/yarnpkg-core/sources/Project.ts#L284.
try {
await writeLocalFile(lockFileName, '');
} catch (err) /* istanbul ignore next */ {
logger.debug(
{ err, lockFileName },
'Error clearing `yarn.lock` for lock file maintenance'
);
}
}
// Run the commands
await exec(commands, execOptions);
// Read the result
lockFile = await readLocalFile(lockFileName, 'utf8');
} catch (err) /* istanbul ignore next */ {
if (err.message === TEMPORARY_ERROR) {
throw err;
}
logger.debug(
{
err,
type: 'yarn',
},
'lock file error'
);
if (err.stderr) {
if (err.stderr.includes('ENOSPC: no space left on device')) {
throw new Error(SYSTEM_INSUFFICIENT_DISK_SPACE);
}
if (
err.stderr.includes('The registry may be down.') ||
err.stderr.includes('getaddrinfo ENOTFOUND registry.yarnpkg.com') ||
err.stderr.includes('getaddrinfo ENOTFOUND registry.npmjs.org')
) {
throw new ExternalHostError(err, NpmDatasource.id);
}
}
return { error: true, stderr: err.stderr, stdout: err.stdout };
}
return { lockFile };
}