From aaebf326a6ca860e42a93522ec45eb41c983f98b Mon Sep 17 00:00:00 2001 From: tanav29 Date: Sun, 27 Sep 2026 12:33:26 +0530 Subject: [PATCH 1/3] fix: fallback to .bunx and .exe in windows Signed-off-by: tanav29 --- scripts/run-gatsby.js | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/scripts/run-gatsby.js b/scripts/run-gatsby.js index 92c7c28a7c50e1..69e040a55416b0 100755 --- a/scripts/run-gatsby.js +++ b/scripts/run-gatsby.js @@ -109,13 +109,17 @@ console.info( `workers=${env.GATSBY_CPU_COUNT} sharp=${env.SHARP_CONCURRENCY} heap=${heapMb}MB`, ); -const gatsbyBin = path.join( - __dirname, - "..", - "node_modules", - ".bin", - process.platform === "win32" ? "gatsby.cmd" : "gatsby", -); +const gatsbyBin = (() => { + const binDir = path.join(__dirname, "..", "node_modules", ".bin"); + if (process.platform === "win32") { + // npm creates gatsby.cmd, but pnpm/bun may only create gatsby.exe or gatsby.bunx + for (const candidate of ["gatsby.cmd", "gatsby.exe", "gatsby.bunx"]) { + const full = path.join(binDir, candidate); + if (require("fs").existsSync(full)) return full; + } + } + return path.join(binDir, "gatsby"); +})(); const child = spawn(gatsbyBin, args, { env, From afd579db9b7e7c5e621cb1cc67cc0a2a38a821a1 Mon Sep 17 00:00:00 2001 From: tanav29 Date: Wed, 30 Sep 2026 23:15:27 +0530 Subject: [PATCH 2/3] refactor: added a small test for run-gatsby Signed-off-by: tanav29 --- .lintstagedrc.js | 2 +- scripts/run-gatsby.js | 74 ++++++++++++++++++++++---------------- scripts/run-gatsby.test.js | 72 +++++++++++++++++++++++++++++++++++++ 3 files changed, 117 insertions(+), 31 deletions(-) create mode 100644 scripts/run-gatsby.test.js diff --git a/.lintstagedrc.js b/.lintstagedrc.js index 717b7424a3c85a..74436ca10182cd 100644 --- a/.lintstagedrc.js +++ b/.lintstagedrc.js @@ -1,7 +1,7 @@ module.exports = { // Lint & Prettify TS and JS files - only the staged files "**/*.(ts|tsx|js|jsx)": (filenames) => [ - `cross-env NODE_ENV=test npx eslint --fix --max-warnings=0 ${filenames.map((f) => `'${f.replace(/'/g, "'\\''")}'`).join(" ")}`, + `cross-env NODE_ENV=test npx eslint --fix --max-warnings=0 --no-warn-ignored ${filenames.map((f) => `'${f.replace(/'/g, "'\\''")}'`).join(" ")}`, `npx prettier --write ${filenames.map((f) => `'${f.replace(/'/g, "'\\''")}'`).join(" ")}`, ], diff --git a/scripts/run-gatsby.js b/scripts/run-gatsby.js index 69e040a55416b0..578bdc46cedce1 100755 --- a/scripts/run-gatsby.js +++ b/scripts/run-gatsby.js @@ -103,38 +103,52 @@ if (!/--max-old-space-size/.test(env.NODE_OPTIONS || "")) { env.NODE_OPTIONS = `${env.NODE_OPTIONS || ""} ${heapFlag}`.trim(); } -const args = process.argv.slice(2); -console.info( - `[run-gatsby] ${totalGb.toFixed(1)} GB RAM / ${cores} cores -> ` + - `workers=${env.GATSBY_CPU_COUNT} sharp=${env.SHARP_CONCURRENCY} heap=${heapMb}MB`, -); - -const gatsbyBin = (() => { - const binDir = path.join(__dirname, "..", "node_modules", ".bin"); - if (process.platform === "win32") { - // npm creates gatsby.cmd, but pnpm/bun may only create gatsby.exe or gatsby.bunx +/** + * Resolve the Gatsby binary, preferring Windows shims in order. + * + * npm creates gatsby.cmd, but pnpm/bun may only create gatsby.exe or + * gatsby.bunx. Non-Windows behavior is unchanged (plain `gatsby`). + */ +const resolveGatsbyBin = ({ + platform = process.platform, + existsSync = require("fs").existsSync, + binDir = path.join(__dirname, "..", "node_modules", ".bin"), +} = {}) => { + if (platform === "win32") { for (const candidate of ["gatsby.cmd", "gatsby.exe", "gatsby.bunx"]) { const full = path.join(binDir, candidate); - if (require("fs").existsSync(full)) return full; + if (existsSync(full)) return full; } } return path.join(binDir, "gatsby"); -})(); - -const child = spawn(gatsbyBin, args, { - env, - stdio: "inherit", - shell: process.platform === "win32", -}); - -const forward = (signal) => () => child.kill(signal); -process.on("SIGINT", forward("SIGINT")); -process.on("SIGTERM", forward("SIGTERM")); - -child.on("exit", (code, signal) => { - if (signal) { - process.kill(process.pid, signal); - return; - } - process.exit(code ?? 0); -}); +}; + +const gatsbyBin = resolveGatsbyBin(); + +if (require.main === module) { + const args = process.argv.slice(2); + console.info( + `[run-gatsby] ${totalGb.toFixed(1)} GB RAM / ${cores} cores -> ` + + `workers=${env.GATSBY_CPU_COUNT} sharp=${env.SHARP_CONCURRENCY} heap=${heapMb}MB`, + ); + + const child = spawn(gatsbyBin, args, { + env, + stdio: "inherit", + shell: process.platform === "win32", + }); + + const forward = (signal) => () => child.kill(signal); + process.on("SIGINT", forward("SIGINT")); + process.on("SIGTERM", forward("SIGTERM")); + + child.on("exit", (code, signal) => { + if (signal) { + process.kill(process.pid, signal); + return; + } + process.exit(code ?? 0); + }); +} + +module.exports = { resolveGatsbyBin }; diff --git a/scripts/run-gatsby.test.js b/scripts/run-gatsby.test.js new file mode 100644 index 00000000000000..37df42e5cf8231 --- /dev/null +++ b/scripts/run-gatsby.test.js @@ -0,0 +1,72 @@ +/* eslint-env node */ +"use strict"; + +/** + * Covers the Windows Gatsby binary fallback: gatsby.cmd -> gatsby.exe -> + * gatsby.bunx -> plain gatsby. Non-Windows behavior stays unchanged. + * + * Run with: node --test scripts/run-gatsby.test.js + */ +const { describe, it } = require("node:test"); +const assert = require("node:assert/strict"); +const path = require("node:path"); + +const { resolveGatsbyBin } = require("./run-gatsby"); + +const binDir = path.join("node_modules", ".bin"); + +const stubExists = (presentFiles) => { + const present = new Set(presentFiles); + return (file) => present.has(path.basename(file)); +}; + +describe("resolveGatsbyBin (win32)", () => { + it("prefers gatsby.cmd when all shims exist", () => { + const bin = resolveGatsbyBin({ + platform: "win32", + binDir, + existsSync: stubExists(["gatsby.cmd", "gatsby.exe", "gatsby.bunx"]), + }); + assert.equal(bin, path.join(binDir, "gatsby.cmd")); + }); + + it("falls back to gatsby.exe when gatsby.cmd is missing", () => { + const bin = resolveGatsbyBin({ + platform: "win32", + binDir, + existsSync: stubExists(["gatsby.exe", "gatsby.bunx"]), + }); + assert.equal(bin, path.join(binDir, "gatsby.exe")); + }); + + it("falls back to gatsby.bunx when only bun modules are installed", () => { + const bin = resolveGatsbyBin({ + platform: "win32", + binDir, + existsSync: stubExists(["gatsby.bunx"]), + }); + assert.equal(bin, path.join(binDir, "gatsby.bunx")); + }); + + it("falls back to plain gatsby when no Windows shim exists", () => { + const bin = resolveGatsbyBin({ + platform: "win32", + binDir, + existsSync: stubExists([]), + }); + assert.equal(bin, path.join(binDir, "gatsby")); + }); +}); + +describe("resolveGatsbyBin (non-Windows)", () => { + for (const platform of ["linux", "darwin"]) { + it(`returns plain gatsby on ${platform} even when shims exist`, () => { + const bin = resolveGatsbyBin({ + platform, + binDir, + existsSync: stubExists(["gatsby.cmd", "gatsby.exe", "gatsby.bunx"]), + }); + assert.equal(bin, path.join(binDir, "gatsby")); + }); + } +}); From 86031534c8fe78bc2bf6c8684f02fd82c2e191fa Mon Sep 17 00:00:00 2001 From: tanav29 Date: Wed, 30 Sep 2026 23:37:23 +0530 Subject: [PATCH 3/3] fix: improve Windows binary resolution for Gatsby CLI Signed-off-by: tanav29 --- scripts/run-gatsby.js | 25 +++++++++++++++++-------- scripts/run-gatsby.test.js | 7 ++++--- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/scripts/run-gatsby.js b/scripts/run-gatsby.js index 578bdc46cedce1..cf8cddde6109b4 100755 --- a/scripts/run-gatsby.js +++ b/scripts/run-gatsby.js @@ -106,8 +106,9 @@ if (!/--max-old-space-size/.test(env.NODE_OPTIONS || "")) { /** * Resolve the Gatsby binary, preferring Windows shims in order. * - * npm creates gatsby.cmd, but pnpm/bun may only create gatsby.exe or - * gatsby.bunx. Non-Windows behavior is unchanged (plain `gatsby`). + * npm creates gatsby.cmd, while pnpm may create gatsby.exe. Bun's .bunx file + * is metadata, so use Gatsby's Node CLI entrypoint when that is the only shim. + * Non-Windows behavior is unchanged (plain `gatsby`). */ const resolveGatsbyBin = ({ platform = process.platform, @@ -115,10 +116,13 @@ const resolveGatsbyBin = ({ binDir = path.join(__dirname, "..", "node_modules", ".bin"), } = {}) => { if (platform === "win32") { - for (const candidate of ["gatsby.cmd", "gatsby.exe", "gatsby.bunx"]) { + for (const candidate of ["gatsby.cmd", "gatsby.exe"]) { const full = path.join(binDir, candidate); if (existsSync(full)) return full; } + if (existsSync(path.join(binDir, "gatsby.bunx"))) { + return path.resolve(binDir, "..", "gatsby-cli", "cli.js"); + } } return path.join(binDir, "gatsby"); }; @@ -132,11 +136,16 @@ if (require.main === module) { `workers=${env.GATSBY_CPU_COUNT} sharp=${env.SHARP_CONCURRENCY} heap=${heapMb}MB`, ); - const child = spawn(gatsbyBin, args, { - env, - stdio: "inherit", - shell: process.platform === "win32", - }); + const isNodeEntrypoint = gatsbyBin.endsWith(".js"); + const child = spawn( + isNodeEntrypoint ? process.execPath : gatsbyBin, + isNodeEntrypoint ? [gatsbyBin, ...args] : args, + { + env, + stdio: "inherit", + shell: process.platform === "win32" && !isNodeEntrypoint, + }, + ); const forward = (signal) => () => child.kill(signal); process.on("SIGINT", forward("SIGINT")); diff --git a/scripts/run-gatsby.test.js b/scripts/run-gatsby.test.js index 37df42e5cf8231..90635a7524a8c1 100644 --- a/scripts/run-gatsby.test.js +++ b/scripts/run-gatsby.test.js @@ -3,7 +3,8 @@ /** * Covers the Windows Gatsby binary fallback: gatsby.cmd -> gatsby.exe -> - * gatsby.bunx -> plain gatsby. Non-Windows behavior stays unchanged. + * Gatsby's Node CLI when only gatsby.bunx metadata exists -> plain gatsby. + * Non-Windows behavior stays unchanged. * * Run with: node --test scripts/run-gatsby.test.js */ @@ -39,13 +40,13 @@ describe("resolveGatsbyBin (win32)", () => { assert.equal(bin, path.join(binDir, "gatsby.exe")); }); - it("falls back to gatsby.bunx when only bun modules are installed", () => { + it("uses Gatsby's Node CLI when only the Bun metadata file is installed", () => { const bin = resolveGatsbyBin({ platform: "win32", binDir, existsSync: stubExists(["gatsby.bunx"]), }); - assert.equal(bin, path.join(binDir, "gatsby.bunx")); + assert.equal(bin, path.resolve(binDir, "..", "gatsby-cli", "cli.js")); }); it("falls back to plain gatsby when no Windows shim exists", () => {