cli-wrapper.mjs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import { fileURLToPath } from 'url';
  2. import { p as picocolors, E as EXIT_CODE_RESTART } from './chunk-constants.71e8a211.mjs';
  3. import { e as execa } from './vendor-index.fbec8a81.mjs';
  4. import 'tty';
  5. import 'path';
  6. import 'buffer';
  7. import 'child_process';
  8. import 'process';
  9. import './vendor-index.2ae8040a.mjs';
  10. import './vendor-_commonjsHelpers.4da45ef5.mjs';
  11. import 'fs';
  12. import 'stream';
  13. import 'util';
  14. import 'os';
  15. import './vendor-index.29636037.mjs';
  16. import 'assert';
  17. import 'events';
  18. const ENTRY = new URL("./cli.mjs", import.meta.url);
  19. const NODE_ARGS = [
  20. "--inspect",
  21. "--inspect-brk",
  22. "--trace-deprecation",
  23. "--experimental-wasm-threads",
  24. "--wasm-atomics-on-non-shared-memory"
  25. ];
  26. const SegfaultErrors = [
  27. {
  28. trigger: "Check failed: result.second.",
  29. url: "https://github.com/nodejs/node/issues/43617"
  30. },
  31. {
  32. trigger: "FATAL ERROR: v8::FromJust Maybe value is Nothing.",
  33. url: "https://github.com/vitest-dev/vitest/issues/1191"
  34. },
  35. {
  36. trigger: "FATAL ERROR: v8::ToLocalChecked Empty MaybeLocal.",
  37. url: "https://github.com/nodejs/node/issues/42407"
  38. }
  39. ];
  40. main();
  41. async function main() {
  42. var _a;
  43. let retries = 0;
  44. const args = process.argv.slice(2);
  45. if (process.env.VITEST_SEGFAULT_RETRY) {
  46. retries = +process.env.VITEST_SEGFAULT_RETRY;
  47. } else {
  48. for (let i = 0; i < args.length; i++) {
  49. if (args[i].startsWith("--segfault-retry=")) {
  50. retries = +args[i].split("=")[1];
  51. break;
  52. } else if (args[i] === "--segfault-retry" && ((_a = args[i + 1]) == null ? void 0 : _a.match(/^\d+$/))) {
  53. retries = +args[i + 1];
  54. break;
  55. }
  56. }
  57. }
  58. const nodeArgs = [];
  59. const vitestArgs = [];
  60. for (let i = 0; i < args.length; i++) {
  61. let matched = false;
  62. for (const nodeArg of NODE_ARGS) {
  63. if (args[i] === nodeArg || args[i].startsWith(`${nodeArg}=`)) {
  64. matched = true;
  65. nodeArgs.push(args[i]);
  66. break;
  67. }
  68. }
  69. if (!matched)
  70. vitestArgs.push(args[i]);
  71. }
  72. retries = Math.max(1, retries || 1);
  73. for (let i = 1; i <= retries; i++) {
  74. const result = await start(nodeArgs, vitestArgs);
  75. if (result === "restart") {
  76. i -= 1;
  77. continue;
  78. }
  79. if (i === 1 && retries === 1) {
  80. console.log(picocolors.exports.yellow(`It seems to be an upstream bug of Node.js. To improve the test stability,
  81. you could pass ${picocolors.exports.bold(picocolors.exports.green("--segfault-retry=3"))} or set env ${picocolors.exports.bold(picocolors.exports.green("VITEST_SEGFAULT_RETRY=3"))} to
  82. have Vitest auto retries on flaky segfaults.
  83. `));
  84. }
  85. if (i !== retries)
  86. console.log(`${picocolors.exports.inverse(picocolors.exports.bold(picocolors.exports.magenta(" Retrying ")))} vitest ${args.join(" ")} ${picocolors.exports.gray(`(${i + 1} of ${retries})`)}`);
  87. }
  88. process.exit(1);
  89. }
  90. async function start(preArgs, postArgs) {
  91. var _a;
  92. const child = execa(
  93. "node",
  94. [
  95. ...preArgs,
  96. fileURLToPath(ENTRY),
  97. ...postArgs
  98. ],
  99. {
  100. reject: false,
  101. stderr: "pipe",
  102. stdout: "inherit",
  103. stdin: "inherit",
  104. env: {
  105. ...process.env,
  106. VITEST_CLI_WRAPPER: "true"
  107. }
  108. }
  109. );
  110. (_a = child.stderr) == null ? void 0 : _a.pipe(process.stderr);
  111. const { stderr = "" } = await child;
  112. if (child.exitCode === EXIT_CODE_RESTART)
  113. return "restart";
  114. for (const error of SegfaultErrors) {
  115. if (stderr.includes(error.trigger)) {
  116. if (process.env.GITHUB_ACTIONS)
  117. console.log(`::warning:: Segmentfault Error Detected: ${error.trigger}
  118. Refer to ${error.url}`);
  119. const RED_BLOCK = picocolors.exports.inverse(picocolors.exports.red(" "));
  120. console.log(`
  121. ${picocolors.exports.inverse(picocolors.exports.bold(picocolors.exports.red(" Segmentfault Error Detected ")))}
  122. ${RED_BLOCK} ${picocolors.exports.red(error.trigger)}
  123. ${RED_BLOCK} ${picocolors.exports.red(`Refer to ${error.url}`)}
  124. `);
  125. return "error";
  126. }
  127. }
  128. process.exit(child.exitCode);
  129. }