From 9660c7ed088aa08fd4a7487f065fef69a9b7ddf3 Mon Sep 17 00:00:00 2001 From: Ray Strode Date: Mon, 9 Dec 2024 20:47:34 -0500 Subject: [PATCH] vscode: Ensure extension.js is rebuilt when deleted Right now, the vscode-extension:continue-ui:build task deletes extension.js as part of its prepackage.js script. This wouldn't normally be a problem, because extension.js is generated by the vscode-extension:esbuild, and vscode-extension:continue-ui:build is a dependency of vscode-extension:esbuild, implying it should always get run first. But, vscode-extension:esbuild is a background task, that stays running after it's been started once. Future calls to run that task become no-ops, because it's already active. This means running vscode-extension:esbuild -> vscode-extension:continue-ui:build -> vscode-extension:esbuild will lead to the last vscode-extension:esbuild not regenerating extension.js. In order to resolve that problem, this commit adds a new foreground task into the build pipeline: vscode-extension:esbuild-notify that tickles the watching background task to wake up and regenerate extension.js --- .vscode/tasks.json | 10 ++++++++++ extensions/vscode/package.json | 1 + extensions/vscode/scripts/esbuild.js | 24 +++++++++++++++++++++++- 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 968b98964..e3250d80a 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -28,6 +28,9 @@ "vscode-extension:continue-ui:build", // // To bundle the code the same way we do for publishing "vscode-extension:esbuild", + // To ensure extension.js is regenerated even if + // vscode-extension:esbuild was already running in background + "vscode-extension:esbuild-notify", // Start the React app that is used in the extension "gui:dev" ], @@ -36,6 +39,13 @@ "isDefault": true } }, + { + "label": "vscode-extension:esbuild-notify", + "dependsOn": ["vscode-extension:esbuild"], + "type": "npm", + "script": "esbuild-notify", + "path": "extensions/vscode" + }, { "label": "vscode-extension:esbuild", "dependsOn": ["vscode-extension:continue-ui:build"], diff --git a/extensions/vscode/package.json b/extensions/vscode/package.json index a109c52fe..4834c14e1 100644 --- a/extensions/vscode/package.json +++ b/extensions/vscode/package.json @@ -602,6 +602,7 @@ "vscode:prepublish": "npm run esbuild-base -- --minify", "esbuild": "npm run esbuild-base -- --sourcemap", "esbuild-watch": "npm run esbuild-base -- --sourcemap --watch", + "esbuild-notify": "npm run esbuild-base -- --sourcemap --notify", "esbuild:visualize": "esbuild-visualizer --metadata ./build/meta.json --filename ./build/stats.html --open", "tsc": "tsc -p ./", "tsc:check": "tsc -p ./ --noEmit", diff --git a/extensions/vscode/scripts/esbuild.js b/extensions/vscode/scripts/esbuild.js index bac602d79..e44fc60af 100644 --- a/extensions/vscode/scripts/esbuild.js +++ b/extensions/vscode/scripts/esbuild.js @@ -1,4 +1,5 @@ const esbuild = require("esbuild"); +const fs = require("fs"); const flags = process.argv.slice(2); @@ -31,7 +32,6 @@ const esbuildConfig = { throw new Error(result.errors); } else { try { - const fs = require("fs"); fs.writeFileSync( "./build/meta.json", JSON.stringify(result.metafile, null, 2), @@ -52,6 +52,28 @@ const esbuildConfig = { if (flags.includes("--watch")) { const ctx = await esbuild.context(esbuildConfig); await ctx.watch(); + } else if (flags.includes("--notify")) { + const inFile = esbuildConfig.entryPoints[0]; + const outFile = esbuildConfig.outfile; + + // The watcher automatically notices changes to source files + // so the only thing it needs to be notified about is if the + // output file gets removed. + if (fs.existsSync (outFile)) { + console.log("VS Code Extension esbuild up to date"); + return; + } + + fs.watchFile(outFile, (current, previous) => { + if (current.size > 0) { + console.log("VS Code Extension esbuild rebuild complete"); + fs.unwatchFile(outFile); + process.exit(0); + } + }); + + console.log("Triggering VS Code Extension esbuild rebuild..."); + fs.utimesSync(inFile, new Date(), new Date()); } else { await esbuild.build(esbuildConfig); }