Skip to content

Commit 1d90f68

Browse files
authored
fix: add better error messaging for builds (#1467)
* fix: add better error messaging for builds * test: cleanup
1 parent b36b011 commit 1d90f68

File tree

3 files changed

+94
-5
lines changed

3 files changed

+94
-5
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
"jest": {
8383
"testMatch": [
8484
"**/test/**/*.js",
85+
"**/test/**/*.ts",
8586
"!**/test/fixtures/**",
8687
"!**/test/sample/**"
8788
],

plugin/src/helpers/verification.ts

+13-5
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,18 @@ export const checkNextSiteHasBuilt = ({
6262
failBuild: FailBuild
6363
}): void | never => {
6464
if (!existsSync(path.join(publish, 'BUILD_ID'))) {
65-
const outWarning =
66-
path.basename(publish) === 'out'
67-
? `Your publish directory is set to "out", but in most cases it should be ".next".`
68-
: `In most cases it should be set to ".next", unless you have chosen a custom "distDir" in your Next config.`
65+
let outWarning
66+
67+
if (path.basename(publish) === 'out') {
68+
outWarning = `Your publish directory is set to "out", but in most cases it should be ".next".`
69+
} else if (path.basename(publish) !== '.next' && existsSync(path.join('.next', 'BUILD_ID'))) {
70+
outWarning = outdent`
71+
However, a '.next' directory was found with a production build.
72+
Consider changing your 'publish' directory to '.next'
73+
`
74+
} else {
75+
outWarning = `In most cases it should be set to ".next", unless you have chosen a custom "distDir" in your Next config.`
76+
}
6977

7078
return failBuild(outdent`
7179
The directory "${publish}" does not contain a Next.js production build. Perhaps the build command was not run, or you specified the wrong publish directory.
@@ -74,7 +82,7 @@ export const checkNextSiteHasBuilt = ({
7482
`)
7583
}
7684
if (existsSync(path.join(publish, 'export-detail.json'))) {
77-
failBuild(outdent`
85+
return failBuild(outdent`
7886
Detected that "next export" was run, but site is incorrectly publishing the ".next" directory.
7987
The publish directory should be set to "out", and you should set the environment variable NETLIFY_NEXT_PLUGIN_SKIP to "true".
8088
`)

test/helpers/verification.spec.ts

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import { checkNextSiteHasBuilt } from '../../plugin/src/helpers/verification'
2+
import { outdent } from 'outdent'
3+
4+
import type { NetlifyPluginUtils } from '@netlify/build'
5+
type FailBuild = NetlifyPluginUtils['build']['failBuild']
6+
7+
jest.mock('fs', () => {
8+
return {
9+
existsSync: jest.fn()
10+
}
11+
})
12+
13+
describe('checkNextSiteHasBuilt', () => {
14+
let failBuildMock
15+
const { existsSync } = require('fs')
16+
17+
beforeEach(() => {
18+
failBuildMock = (jest.fn() as unknown) as FailBuild
19+
})
20+
21+
afterEach(() => {
22+
jest.clearAllMocks()
23+
jest.resetAllMocks()
24+
})
25+
26+
it('returns error message about incorrectly publishing the ".next" directory when "next export" was run', () => {
27+
existsSync.mockReturnValue(true)
28+
29+
const expectedFailureMessage = outdent`
30+
Detected that "next export" was run, but site is incorrectly publishing the ".next" directory.
31+
The publish directory should be set to "out", and you should set the environment variable NETLIFY_NEXT_PLUGIN_SKIP to "true".
32+
`
33+
34+
checkNextSiteHasBuilt({ publish: '.next', failBuild: failBuildMock })
35+
36+
expect(failBuildMock).toHaveBeenCalledWith(expectedFailureMessage)
37+
})
38+
39+
it('returns error message prompt to change publish directory to ".next"', () => {
40+
// False for not initially finding the specified 'publish' directory,
41+
// True for successfully finding a '.next' directory with a production build
42+
existsSync.mockReturnValueOnce(false).mockReturnValueOnce(true)
43+
44+
const expectedFailureMessage = outdent`
45+
The directory "someCustomDir" does not contain a Next.js production build. Perhaps the build command was not run, or you specified the wrong publish directory.
46+
However, a '.next' directory was found with a production build.
47+
Consider changing your 'publish' directory to '.next'
48+
If you are using "next export" then you should set the environment variable NETLIFY_NEXT_PLUGIN_SKIP to "true".
49+
`
50+
51+
checkNextSiteHasBuilt({ publish: 'someCustomDir', failBuild: failBuildMock })
52+
53+
expect(failBuildMock).toHaveBeenCalledWith(expectedFailureMessage)
54+
})
55+
56+
it('returns error message prompt when publish directory is set to "out"', () => {
57+
existsSync.mockReturnValue(false)
58+
59+
const expectedFailureMessage = outdent`
60+
The directory "out" does not contain a Next.js production build. Perhaps the build command was not run, or you specified the wrong publish directory.
61+
Your publish directory is set to "out", but in most cases it should be ".next".
62+
If you are using "next export" then you should set the environment variable NETLIFY_NEXT_PLUGIN_SKIP to "true".
63+
`
64+
checkNextSiteHasBuilt({ publish: 'out', failBuild: failBuildMock })
65+
66+
expect(failBuildMock).toHaveBeenCalledWith(expectedFailureMessage)
67+
})
68+
69+
it('returns default error message when production build was not found', () => {
70+
existsSync.mockReturnValue(false)
71+
const expectedFailureMessage = outdent`
72+
The directory ".next" does not contain a Next.js production build. Perhaps the build command was not run, or you specified the wrong publish directory.
73+
In most cases it should be set to ".next", unless you have chosen a custom "distDir" in your Next config.
74+
If you are using "next export" then you should set the environment variable NETLIFY_NEXT_PLUGIN_SKIP to "true".
75+
`
76+
checkNextSiteHasBuilt({ publish: '.next', failBuild: failBuildMock })
77+
78+
expect(failBuildMock).toHaveBeenCalledWith(expectedFailureMessage)
79+
})
80+
})

0 commit comments

Comments
 (0)