Skip to content

Commit 76c3009

Browse files
fix: show deploy errors when --json is used (#7649)
1 parent 47e14cd commit 76c3009

File tree

3 files changed

+41
-4
lines changed

3 files changed

+41
-4
lines changed

src/commands/deploy/deploy.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -687,10 +687,12 @@ const handleBuild = async ({
687687
currentDir,
688688
deployHandler,
689689
})
690-
const { configMutations, exitCode, newConfig } = await runBuild(resolvedOptions)
690+
const { configMutations, exitCode, newConfig, logs } = await runBuild(resolvedOptions)
691691
// Without this, the deploy command fails silently
692692
if (options.json && exitCode !== 0) {
693-
logAndThrowError('Error while running build')
693+
const message = logs?.stderr.length ? `: ${logs.stderr.join('')}` : ''
694+
695+
logAndThrowError(`Error while running build${message}`)
694696
}
695697
if (exitCode !== 0) {
696698
exit(exitCode)

src/lib/build.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,14 @@ export const getRunBuildOptions = async ({
194194
}
195195
}
196196

197-
export const runBuild = async (options: RunBuildOptions) => {
197+
export const runBuild = async (
198+
options: RunBuildOptions,
199+
): Promise<{
200+
exitCode: number
201+
newConfig: NetlifyConfig
202+
configMutations: Record<string, string>
203+
logs?: { stdout: string[]; stderr: string[] }
204+
}> => {
198205
// If netlify NETLIFY_API_URL is set we need to pass this information to @netlify/build
199206
// TODO don't use testOpts, but add real properties to do this.
200207
if (process.env.NETLIFY_API_URL) {
@@ -212,7 +219,8 @@ export const runBuild = async (options: RunBuildOptions) => {
212219
configMutations,
213220
netlifyConfig: newConfig,
214221
severityCode: exitCode,
222+
logs,
215223
// TODO(serhalp): Upstream the type fixes above into @netlify/build and remove this type assertion
216224
} = await (build as unknown as (opts: RunBuildOptions) => Promise<ReturnType<typeof build>>)(options)
217-
return { exitCode, newConfig, configMutations }
225+
return { exitCode, newConfig, configMutations, logs }
218226
}

tests/integration/commands/deploy/deploy.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,33 @@ describe.skipIf(process.env.NETLIFY_TEST_DISABLE_LIVE === 'true').concurrent('co
452452
})
453453

454454
test('should throw error when build fails with --json option', async (t) => {
455+
await withSiteBuilder(t, async (builder) => {
456+
builder
457+
.withContentFile({
458+
path: 'public/index.html',
459+
content: '<h1>Test content</h1>',
460+
})
461+
.withNetlifyToml({
462+
config: {
463+
build: {
464+
publish: 'public',
465+
command: 'echo "Build failed with custom error" >&2 && exit 1',
466+
},
467+
},
468+
})
469+
470+
await builder.build()
471+
472+
await expect(
473+
callCli(['deploy', '--json'], {
474+
cwd: builder.directory,
475+
env: { NETLIFY_SITE_ID: context.siteId },
476+
}),
477+
).rejects.toThrow(/Error while running build.*Build failed with custom error/)
478+
})
479+
})
480+
481+
test('should throw error without stderr details when build fails with --json option and no stderr output', async (t) => {
455482
await withSiteBuilder(t, async (builder) => {
456483
builder
457484
.withContentFile({

0 commit comments

Comments
 (0)