Skip to content

Commit

Permalink
fix esbuild path used to bundle next.config.js on Windows (#7555)
Browse files Browse the repository at this point in the history
* use path from npx which to run esbuild

* fix test

* prevent findEsbuildPath to throw when npx which returns undefined

* prevent throw when version not found in getGlobalEsbuildVersion

* changelog
  • Loading branch information
leoortizz authored Aug 13, 2024
1 parent f9fff1b commit 5b59e22
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Fix esbuild path used to bundle next.config.js on Windows (#7555)
7 changes: 5 additions & 2 deletions src/frameworks/next/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -573,17 +573,20 @@ describe("Next.js utils", () => {

it("should warn if global esbuild version does not match required version", () => {
const mockBinaryPath = "/path/to/.bin/esbuild";
const mockGlobalVersion = "1.2.3";
execSyncStub
.withArgs("npx which esbuild", { encoding: "utf8" })
.returns(mockBinaryPath + "\n");
execSyncStub.withArgs("esbuild --version", { encoding: "utf8" }).returns("0.18.0\n");
execSyncStub
.withArgs(`${mockBinaryPath} --version`, { encoding: "utf8" })
.returns(`${mockGlobalVersion}\n`);

const consoleWarnStub = sinon.stub(console, "warn");

findEsbuildPath();
expect(
consoleWarnStub.calledWith(
`Warning: Global esbuild version (0.18.0) does not match the required version (${ESBUILD_VERSION}).`,
`Warning: Global esbuild version (${mockGlobalVersion}) does not match the required version (${ESBUILD_VERSION}).`,
),
).to.be.true;

Expand Down
16 changes: 12 additions & 4 deletions src/frameworks/next/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -498,8 +498,12 @@ export async function whichNextConfigFile(dir: string): Promise<NextConfigFileNa
*/
export function findEsbuildPath(): string | null {
try {
const esbuildBinPath = execSync("npx which esbuild", { encoding: "utf8" }).trim();
const globalVersion = getGlobalEsbuildVersion();
const esbuildBinPath = execSync("npx which esbuild", { encoding: "utf8" })?.trim();
if (!esbuildBinPath) {
return null;
}

const globalVersion = getGlobalEsbuildVersion(esbuildBinPath);
if (globalVersion && !satisfies(globalVersion, ESBUILD_VERSION)) {
console.warn(
`Warning: Global esbuild version (${globalVersion}) does not match the required version (${ESBUILD_VERSION}).`,
Expand All @@ -515,9 +519,13 @@ export function findEsbuildPath(): string | null {
/**
* Helper function to get the global esbuild version
*/
export function getGlobalEsbuildVersion(): string | null {
export function getGlobalEsbuildVersion(binPath: string): string | null {
try {
const versionOutput = execSync("esbuild --version", { encoding: "utf8" }).trim();
const versionOutput = execSync(`${binPath} --version`, { encoding: "utf8" })?.trim();
if (!versionOutput) {
return null;
}

const versionMatch = versionOutput.match(/(\d+\.\d+\.\d+)/);
return versionMatch ? versionMatch[0] : null;
} catch (error) {
Expand Down

0 comments on commit 5b59e22

Please sign in to comment.