forked from microsoft/typescript-error-deltas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
395 lines (335 loc) · 15.6 KB
/
main.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
import ge = require("./getErrors");
import pu = require("./packageUtils");
import git = require("./gitUtils");
import ip = require("./installPackages");
import ur = require("./userRepos");
import cp = require("child_process");
import fs = require("fs");
import path = require("path");
export interface Params {
/** True to post the result to Github, false to print to console. */
postResult: boolean;
/** Store test repos on a tmpfs */
tmpfs: boolean;
/**
* Number of repos to test, undefined for the default.
* Git repos are chosen randomly; default is 100.
* User repos start at the top of the list; default is all of them.
*/
repoCount?: number | undefined;
oldTypescriptRepoUrl: string;
oldHeadRef: string;
sourceIssue: number;
requestingUser: string;
statusComment: number;
}
const skipRepos = [
"https://github.com/storybookjs/storybook", // Too big to fit on VM
"https://github.com/microsoft/frontend-bootcamp", // Can't be built twice in a row
];
const processCwd = process.cwd();
const processPid = process.pid;
const executionTimeout = 10 * 60 * 1000;
export async function innerloop(params: Params, downloadDir: string, userTestDir: string, repo: git.Repo, oldTscPath: string, newTscPath: string, outputs: string[]) {
if (params.tmpfs)
await execAsync(processCwd, "sudo mount -t tmpfs -o size=2g tmpfs " + downloadDir);
try {
if (repo.url) {
try {
console.log("Cloning if absent");
await git.cloneRepoIfNecessary(downloadDir, repo);
}
catch (err) {
reportError(err, "Error cloning " + repo.url);
return;
}
}
else {
await ur.copyUserRepo(downloadDir, userTestDir, repo);
}
const repoDir = path.join(downloadDir, repo.name);
try {
console.log("Installing packages if absent");
await withTimeout(executionTimeout, installPackages(repoDir, repo.types));
}
catch (err) {
reportError(err, "Error installing packages for " + repo.name);
await reportResourceUsage(downloadDir);
return;
}
try {
console.log(`Building with ${oldTscPath} (old)`);
const oldErrors = await buildAndGetErrors(repoDir, oldTscPath, /*skipLibCheck*/ true, params.postResult);
if (oldErrors.hasConfigFailure) {
console.log("Unable to build project graph");
console.log(`Skipping build with ${newTscPath} (new)`);
return;
}
const numProjects = oldErrors.projectErrors.length;
let numFailed = 0;
for (const oldProjectErrors of oldErrors.projectErrors) {
if (oldProjectErrors.hasBuildFailure || oldProjectErrors.errors.length) {
numFailed++;
}
}
// User tests ignores build failures.
let sawNewRepoErrors = false;
const owner = repo.owner ? `${repo.owner}/` : "";
const url = repo.url ? `(${repo.url})` : "";
let repoSummary = `# [${owner}${repo.name}]${url}\n`;
if (numFailed > 0) {
const oldFailuresMessage = `${numFailed} of ${numProjects} projects failed to build with the old tsc`;
console.log(oldFailuresMessage);
repoSummary += `**${oldFailuresMessage}**\n`;
}
console.log(`Building with ${newTscPath} (new)`);
const newErrors = await buildAndGetErrors(repoDir, newTscPath, /*skipLibCheck*/ true, params.postResult);
if (newErrors.hasConfigFailure) {
console.log("Unable to build project graph");
repoSummary += ":exclamation::exclamation: **Unable to build the project graph with the new tsc** :exclamation::exclamation:\n";
outputs.push(repoSummary)
return true;
}
console.log("Comparing errors");
for (const oldProjectErrors of oldErrors.projectErrors) {
// TS 5055 generally indicates that the project can't be built twice in a row without cleaning in between.
// Filter out errors reported already on "old".
const newProjectErrors = newErrors.projectErrors.find(pe => pe.projectUrl == oldProjectErrors.projectUrl)?.errors?.filter(e => e.code !== 5055)
.filter(ne => !oldProjectErrors.errors.find(oe => ge.errorEquals(oe, ne)));
if (!newProjectErrors?.length) {
continue;
}
sawNewRepoErrors = true;
const errorMessageMap = new Map<string, ge.Error[]>();
const errorMessages: string[] = [];
console.log(`New errors for ${oldProjectErrors.isComposite ? "composite" : "non-composite"} project ${oldProjectErrors.projectUrl}`);
for (const newError of newProjectErrors) {
const newErrorText = newError.text;
console.log(`\tTS${newError.code} at ${newError.fileUrl ?? "project scope"}${oldProjectErrors.isComposite ? ` in ${newError.projectUrl}` : ``}`);
console.log(`\t\t${newErrorText}`);
if (!errorMessageMap.has(newErrorText)) {
errorMessageMap.set(newErrorText, []);
errorMessages.push(newErrorText);
}
errorMessageMap.get(newErrorText)!.push(newError);
}
repoSummary += `### ${makeMarkdownLink(oldProjectErrors.projectUrl)}\n`
for (const errorMessage of errorMessages) {
repoSummary += ` - \`${errorMessage}\`\n`;
for (const error of errorMessageMap.get(errorMessage)!) {
repoSummary += ` - ${error.fileUrl ? makeMarkdownLink(error.fileUrl) : "Project Scope"}${oldProjectErrors.isComposite ? ` in ${makeMarkdownLink(error.projectUrl)}` : ``}\n`;
}
}
}
if (sawNewRepoErrors) {
// sawNewErrors = true;
// summary += repoSummary;
outputs.push(repoSummary)
return true;
}
}
catch (err) {
reportError(err, "Error building " + repo.url ?? repo.name);
return;
}
console.log("Done " + repo.url ?? repo.name);
}
finally {
// Throw away the repo so we don't run out of space
// Note that we specifically don't recover and attempt another repo if this fails
console.log("Cleaning up repo");
if (params.tmpfs) {
await execAsync(processCwd, "sudo umount " + downloadDir);
await reportResourceUsage(downloadDir);
}
}
}
export async function mainAsync(params: Params): Promise<git.Result | undefined> {
const downloadDir = params.tmpfs ? "/mnt/ts_downloads" : "./ts_downloads";
// TODO: check first whether the directory exists and skip downloading if possible
// TODO: Seems like this should come after the typescript download
if (params.tmpfs)
await execAsync(processCwd, "sudo mkdir " + downloadDir);
else
await execAsync(processCwd, "mkdir " + downloadDir);
// TODO: Only download if the commit has changed (need to map refs to commits and then download to typescript-COMMIT instead)
const { oldTscPath, oldTscResolvedVersion, newTscPath, newTscResolvedVersion } = await downloadTypeScriptAsync(processCwd, params);
// Get the name of the typescript folder.
const oldTscDirPath = path.resolve(oldTscPath, "../../");
const newTscDirPath = path.resolve(newTscPath, "../../");
console.log("Old version = " + oldTscResolvedVersion);
console.log("New version = " + newTscResolvedVersion);
const userTestDir = path.join(processCwd, "userTests");
const repos = ur.getUserTestsRepos(userTestDir);
const outputs: string[] = [];
// let summary = "";
let sawNewErrors: true | undefined = undefined;
let i = 0;
const maxCount = Math.min(typeof params.repoCount === 'number' ? params.repoCount : Infinity, repos.length)
for (const repo of repos) {
if (repo.url && skipRepos.includes(repo.url)) continue;
i++;
if (i > maxCount) break;
console.log(`Starting #${i} / ${maxCount}: ${repo.url ?? repo.name}`);
sawNewErrors = sawNewErrors || await innerloop(params, downloadDir, userTestDir, repo, oldTscPath, newTscPath, outputs)
}
const summary = outputs.join("")
if (params.tmpfs) {
await execAsync(processCwd, "sudo rm -rf " + downloadDir);
await execAsync(processCwd, "sudo rm -rf " + oldTscDirPath);
await execAsync(processCwd, "sudo rm -rf " + newTscDirPath);
}
else {
await execAsync(processCwd, "rm -rf " + downloadDir);
await execAsync(processCwd, "rm -rf " + oldTscDirPath);
await execAsync(processCwd, "rm -rf " + newTscDirPath);
}
const body = summary
? `@${params.requestingUser}\nThe results of the user tests run you requested are in!\n<details><summary> Here they are:</summary><p>\n<b>Comparison Report - ${oldTscResolvedVersion}..${newTscResolvedVersion}</b>\n\n${summary}</p></details>`
: `@${params.requestingUser}\nGreat news! no new errors were found between ${oldTscResolvedVersion}..${newTscResolvedVersion}`;
return git.createComment(params.sourceIssue, params.statusComment, params.postResult, body);
}
async function buildAndGetErrors(repoDir: string, tscPath: string, skipLibCheck: boolean, realTimeout: boolean): Promise<ge.RepoErrors> {
if (realTimeout) {
const p = new Promise<ge.RepoErrors>((resolve, reject) => {
const p = cp.fork(path.join(__dirname, "run-build.js"));
p.on('message', (m: 'ready' | ge.RepoErrors) =>
m === 'ready'
? p.send({ repoDir, tscPath, skipLibCheck })
: resolve(m));
p.on('exit', reject);
});
return withTimeout(executionTimeout, p);
}
else {
return ge.buildAndGetErrors(repoDir, tscPath, skipLibCheck)
}
}
async function installPackages(repoDir: string, types?: string[]) {
const commands = await ip.restorePackages(repoDir, types);
let usedYarn = false;
for (const { directory: packageRoot, tool, arguments: args } of commands) {
await new Promise<void>((resolve, reject) => {
usedYarn = usedYarn || tool === ip.InstallTool.Yarn;
cp.execFile(tool, args, { cwd: packageRoot }, err => err ? reject(err) : resolve());
});
}
if (usedYarn) {
await execAsync(repoDir, "yarn cache clean --all");
}
}
function withTimeout<T>(ms: number, promise: Promise<T>): Promise<T> {
let timeout: NodeJS.Timeout | undefined;
return Promise.race([
promise.finally(() => timeout && clearTimeout(timeout)),
new Promise<T>((_resolve, reject) =>
timeout = setTimeout(async () => {
await execAsync(processCwd, `./kill-children-of ${processPid} node`);
return reject(new Error(`Timed out after ${ms} ms`));
}, ms)),
]);
}
async function reportResourceUsage(downloadDir: string) {
try {
console.log("Memory");
await execAsync(processCwd, "free -h");
console.log("Disk");
await execAsync(processCwd, "df -h");
await execAsync(processCwd, "df -i");
console.log("Download Directory");
await execAsync(processCwd, "ls -lh " + downloadDir);
console.log("Home Directory");
await execAsync(processCwd, "du -csh ~/.[^.]*");
await execAsync(processCwd, "du -csh ~/.cache/*");
}
catch { } // noop
}
export function reportError(err: any, message: string) {
console.log(`${message}:`);
console.log(reduceSpew(err.message ?? "No message").replace(/(^|\n)/g, "$1> "));
console.log(reduceSpew(err.stack ?? "Unknown Stack").replace(/(^|\n)/g, "$1> "));
}
async function execAsync(cwd: string, command: string): Promise<string> {
return new Promise((resolve, reject) => {
console.log(`${cwd}> ${command}`);
cp.exec(command, { cwd }, (err, stdout, stderr) => {
if (stdout?.length) {
console.log(stdout);
}
if (stderr?.length) {
console.log(stderr); // To stdout to maintain order
}
if (err) {
return reject(err);
}
return resolve(stdout);
});
});
}
function reduceSpew(message: string): string {
// Since this is only a warning, it tends to be reported many (i.e. thousands of) times
const problemString = "npm WARN tar ENOSPC: no space left on device, write\n";
const index = message.indexOf(problemString);
if (index < 0) return message;
return message.substring(0, index) + problemString + replaceAll(message.substring(index), problemString, "");
}
function replaceAll(message: string, oldStr: string, newStr: string) {
let result = "";
let index = 0;
while (true) {
const newIndex = message.indexOf(oldStr, index);
if (newIndex < 0) {
return index === 0
? message
: result + message.substring(index);
}
result += message.substring(index, newIndex);
result += newStr;
index = newIndex + oldStr.length;
}
}
function makeMarkdownLink(url: string) {
const match = /\/blob\/[a-f0-9]+\/(.+)$/.exec(url);
return !match
? url
: `[${match[1]}](${url})`;
}
async function downloadTypeScriptAsync(cwd: string, params: Params): Promise<{ oldTscPath: string, oldTscResolvedVersion: string, newTscPath: string, newTscResolvedVersion: string }> {
const { tscPath: oldTscPath, resolvedVersion: oldTscResolvedVersion } = await downloadTypescriptRepoAsync(cwd, params.oldTypescriptRepoUrl, params.oldHeadRef);
// We need to handle the ref/pull/*/merge differently as it is not a branch and cannot be pulled during clone.
const { tscPath: newTscPath, resolvedVersion: newTscResolvedVersion } = await downloadTypescriptSourceIssueAsync(cwd, params.oldTypescriptRepoUrl, params.sourceIssue);
return {
oldTscPath,
oldTscResolvedVersion,
newTscPath,
newTscResolvedVersion
};
}
export async function downloadTypescriptRepoAsync(cwd: string, repoUrl: string, headRef: string): Promise<{ tscPath: string, resolvedVersion: string }> {
const repoName = `typescript-${headRef}`;
await git.cloneRepoIfNecessary(cwd, { name: repoName, url: repoUrl, branch: headRef });
const repoPath = path.join(cwd, repoName);
return {
tscPath: await buildTsc(repoPath),
resolvedVersion: headRef
};
}
async function downloadTypescriptSourceIssueAsync(cwd: string, repoUrl: string, sourceIssue: number): Promise<{ tscPath: string, resolvedVersion: string }> {
const repoName = `typescript-${sourceIssue}`;
await git.cloneRepoIfNecessary(cwd, { name: repoName, url: repoUrl });
const repoPath = path.join(cwd, repoName);
const headRef = `refs/pull/${sourceIssue}/merge`;
await git.checkout(repoPath, headRef);
return {
tscPath: await buildTsc(repoPath),
resolvedVersion: headRef
};
}
async function buildTsc(repoPath: string) {
await execAsync(repoPath, "npm ci");
await execAsync(repoPath, "npm run build:compiler");
await execAsync(repoPath, "npm install -g gulp-cli");
await execAsync(repoPath, "gulp configure-insiders");
await execAsync(repoPath, "gulp LKG");
return path.join(repoPath, "built", "local", "tsc.js");
}