Skip to content

Commit

Permalink
Alternate extract-errors that scrapes artifacts
Browse files Browse the repository at this point in the history
The build script outputs a special FIXME comment when it fails to minify
an error message. CI will detect these comments and fail the workflow.

The comments also include the expected error message. So I added an
alternate extract-errors that scrapes unminified messages from the
build artifacts and updates `codes.json`.

This is nice because it works on partial builds. And you can also run it
after the fact, instead of needing build all over again.
  • Loading branch information
acdlite committed Oct 6, 2021
1 parent 1e88679 commit 2721e61
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 3 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@
"lint": "node ./scripts/tasks/eslint.js",
"lint-build": "node ./scripts/rollup/validate/index.js",
"extract-errors": "yarn build --type=dev --extract-errors",
"extract-errors2": "node scripts/error-codes/extract-errors2.js",
"postinstall": "node node_modules/fbjs-scripts/node/check-dev-engines.js package.json && node ./scripts/flow/createFlowConfigs.js && node ./scripts/yarn/downloadReactIsForPrettyFormat.js",
"debug-test": "yarn test --deprecated 'yarn test --debug'",
"test": "node ./scripts/jest/jest-cli.js",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ NotAnError();"
exports[`error transform should output FIXME for errors that don't have a matching error code 1`] = `
"/*! FIXME (minify-errors-in-prod): Unminified error message in production build!*/
/*! Add to error code map: This is not a real error message.*/
/*! <expected-error-format>\\"This is not a real error message.\\"</expected-error-format>*/
Error('This is not a real error message.');"
`;

Expand Down
74 changes: 74 additions & 0 deletions scripts/error-codes/extract-errors2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
'use strict';

const fs = require('fs');
const path = require('path');
const {execSync} = require('child_process');

async function main() {
const originalJSON = JSON.parse(
fs.readFileSync(path.resolve(__dirname, '../error-codes/codes.json'))
);
const existingMessages = new Set();
const codes = Object.keys(originalJSON);
let nextCode = 0;
for (let i = 0; i < codes.length; i++) {
const codeStr = codes[i];
const message = originalJSON[codeStr];
const code = parseInt(codeStr, 10);
existingMessages.add(message);
if (code >= nextCode) {
nextCode = code + 1;
}
}

console.log('Searching `build` directory for unminified errors...\n');

let out;
try {
out = execSync(
"git --no-pager grep -n --untracked --no-exclude-standard '/*! <expected-error-format>' -- build"
).toString();
} catch (e) {
if (e.status === 1 && e.stdout.toString() === '') {
// No unminified errors found.
return;
}
throw e;
}

let newJSON = null;
const regex = /\<expected-error-format\>"(.+?)"\<\/expected-error-format\>/g;
do {
const match = regex.exec(out);
if (match === null) {
break;
} else {
const message = match[1].trim();
if (existingMessages.has(message)) {
// This probably means you ran the script twice.
continue;
}
existingMessages.add(message);

// Add to json map
if (newJSON === null) {
newJSON = Object.assign({}, originalJSON);
}
console.log(`"${nextCode}": "${message}"`);
newJSON[nextCode] = message;
nextCode += 1;
}
} while (true);

if (newJSON) {
fs.writeFileSync(
path.resolve(__dirname, '../error-codes/codes.json'),
JSON.stringify(newJSON, null, 2)
);
}
}

main().catch(error => {
console.error(error);
process.exit(1);
});
4 changes: 2 additions & 2 deletions scripts/error-codes/transform-error-messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ module.exports = function(babel) {
//
// Outputs:
// /* FIXME (minify-errors-in-prod): Unminified error message in production build! */
// /* Add to error code map: "A % message that contains %" */
// /* <expected-error-format>"A % message that contains %"</expected-error-format> */
// if (!condition) {
// throw Error(`A ${adj} message that contains ${noun}`);
// }
Expand All @@ -83,7 +83,7 @@ module.exports = function(babel) {

statementParent.addComment(
'leading',
'! Add to error code map: ' + errorMsgLiteral
`! <expected-error-format>"${errorMsgLiteral}"</expected-error-format>`
);
statementParent.addComment(
'leading',
Expand Down

0 comments on commit 2721e61

Please sign in to comment.