Skip to content

Commit 93ca6da

Browse files
authored
Attempt to fix my-ts-node flakiness (#2029)
Attempt to fix failures like: - https://github.com/cursorless-dev/cursorless/actions/runs/6864301970/job/18665758970 - https://github.com/cursorless-dev/cursorless/actions/runs/6852067329/job/18629742778 Seems like we weren't properly waiting for esbuild to complete or something; switching to using their node.js api, which is easier to read anyway ## Checklist - [ ] I have added [tests](https://www.cursorless.org/docs/contributing/test-case-recorder/) - [ ] I have updated the [docs](https://github.com/cursorless-dev/cursorless/tree/main/docs) and [cheatsheet](https://github.com/cursorless-dev/cursorless/tree/main/cursorless-talon/src/cheatsheet) - [ ] I have not broken the cheatsheet
1 parent b941a3a commit 93ca6da

File tree

1 file changed

+26
-33
lines changed

1 file changed

+26
-33
lines changed

packages/common/scripts/my-ts-node.js

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// This script runs a TypeScript file using Node.js by first bundling it with
55
// esbuild.
66
import { spawn } from "cross-spawn";
7+
import { build } from "esbuild";
78
import { existsSync, mkdirSync, rmdirSync } from "node:fs";
89
import { fileURLToPath } from "node:url";
910
import { dirname, join } from "node:path";
@@ -12,7 +13,7 @@ import { dirname, join } from "node:path";
1213
* Run a command with arguments and return a child process
1314
* @param {string} command
1415
* @param {string[]} args
15-
* @param {NodeJS.ProcessEnv?} extraEnv
16+
* @param {Partial<NodeJS.ProcessEnv>?} extraEnv
1617
*/
1718
function runCommand(command, args, extraEnv = {}) {
1819
return spawn(command, args, {
@@ -47,7 +48,7 @@ function cleanupTempDirectory(tempDir) {
4748
}
4849

4950
// Main function to execute the script
50-
function main() {
51+
async function main() {
5152
const args = process.argv.slice(2);
5253

5354
// Check if the input file is specified
@@ -70,38 +71,30 @@ function main() {
7071
process.on("SIGTERM", () => cleanupTempDirectory(tempDir));
7172

7273
// Run esbuild to bundle the TypeScript file
73-
const esbuildProcess = runCommand("esbuild", [
74-
"--sourcemap",
75-
"--log-level=warning",
76-
"--conditions=cursorless:bundler",
77-
"--bundle",
78-
"--format=cjs",
79-
"--platform=node",
80-
fileToRun,
81-
"--outfile=" + outFile,
82-
]);
83-
84-
esbuildProcess.on("close", (code) => {
85-
if (code === 0) {
86-
// Execute the bundled file with Node, passing any additional arguments
87-
const nodeProcess = runCommand(
88-
process.execPath,
89-
["--enable-source-maps", outFile, ...childArgs],
90-
{
91-
["CURSORLESS_REPO_ROOT"]: join(
92-
dirname(fileURLToPath(import.meta.url)),
93-
"..",
94-
"..",
95-
"..",
96-
),
97-
},
98-
);
99-
nodeProcess.on("close", (code) => process.exit(code ?? undefined));
100-
} else {
101-
console.error(`esbuild failed with code ${code}`);
102-
process.exit(code ?? undefined);
103-
}
74+
await build({
75+
entryPoints: [fileToRun],
76+
sourcemap: true,
77+
conditions: ["cursorless:bundler"],
78+
logLevel: "warning",
79+
platform: "node",
80+
bundle: true,
81+
format: "cjs",
82+
outfile: outFile,
10483
});
84+
85+
const nodeProcess = runCommand(
86+
process.execPath,
87+
["--enable-source-maps", outFile, ...childArgs],
88+
{
89+
["CURSORLESS_REPO_ROOT"]: join(
90+
dirname(fileURLToPath(import.meta.url)),
91+
"..",
92+
"..",
93+
"..",
94+
),
95+
},
96+
);
97+
nodeProcess.on("close", (code) => process.exit(code ?? undefined));
10598
}
10699

107100
main();

0 commit comments

Comments
 (0)