diff --git a/src/helpers/verification.ts b/src/helpers/verification.ts index 2ab7181bdb..afcd736d3d 100644 --- a/src/helpers/verification.ts +++ b/src/helpers/verification.ts @@ -61,12 +61,17 @@ export const checkNextSiteHasBuilt = ({ failBuild: FailBuild }): void | never => { if (!existsSync(path.join(publish, 'BUILD_ID'))) { + const outWarning = + path.basename(publish) === 'out' + ? `Your publish directory is set to "out", but in most cases it should be ".next".` + : `In most cases it should be set to ".next", unless you have chosen a custom "distDir" in your Next config.` + return failBuild(outdent` The directory "${path.relative( process.cwd(), publish, - )}" does not contain a Next.js production build. Perhaps the build command was not run, or you specified the wrong publish directory. - In most cases it should be set to the site's ".next" directory path, unless you have chosen a custom "distDir" in your Next config. + )}" does not contain a Next.js production build. Perhaps the build command was not run, or you specified the wrong publish directory. + ${outWarning} If you are using "next export" then the Essential Next.js plugin should be removed. See https://ntl.fyi/remove-plugin for details. `) } diff --git a/test/index.js b/test/index.js index 3249c3a805..350690f356 100644 --- a/test/index.js +++ b/test/index.js @@ -208,11 +208,26 @@ describe('onBuild()', () => { test("fails if BUILD_ID doesn't exist", async () => { await moveNextDist() await unlink(path.join(process.cwd(), '.next/BUILD_ID')) - const failBuild = jest.fn().mockImplementation(() => { - throw new Error('BUILD_ID does not exist') + const failBuild = jest.fn().mockImplementation((err) => { + throw new Error(err) }) + expect(() => plugin.onBuild({ ...defaultArgs, utils: { ...utils, build: { failBuild } } })).rejects.toThrow( + `In most cases it should be set to ".next", unless you have chosen a custom "distDir" in your Next config.`, + ) + expect(failBuild).toHaveBeenCalled() + }) + + test("fails with helpful warning if BUILD_ID doesn't exist and publish is 'out'", async () => { + await moveNextDist() + await unlink(path.join(process.cwd(), '.next/BUILD_ID')) + const failBuild = jest.fn().mockImplementation((err) => { + throw new Error(err) + }) + netlifyConfig.build.publish = path.resolve('out') - expect(() => plugin.onBuild({ ...defaultArgs, utils: { ...utils, build: { failBuild } } })).rejects.toThrow() + expect(() => plugin.onBuild({ ...defaultArgs, utils: { ...utils, build: { failBuild } } })).rejects.toThrow( + `Your publish directory is set to "out", but in most cases it should be ".next".`, + ) expect(failBuild).toHaveBeenCalled() })