From 8d39e22a70e53b3c0a288d15e80e4ae17f6a886e Mon Sep 17 00:00:00 2001 From: Orzelius <33936483+Orzelius@users.noreply.github.com> Date: Thu, 6 Jul 2023 16:10:10 +0300 Subject: [PATCH] fix(exec): show error on failed commands * fix: show error on failed exec commands * chore: fix error message templating --- core/src/plugins/exec/build.ts | 8 +++++--- core/src/plugins/exec/common.ts | 17 +++++++++++++++-- core/src/plugins/exec/deploy.ts | 17 ++++++++++------- core/src/plugins/exec/run.ts | 8 ++++---- core/src/plugins/exec/test.ts | 15 +++++++-------- core/src/tasks/base.ts | 5 ++--- 6 files changed, 43 insertions(+), 27 deletions(-) diff --git a/core/src/plugins/exec/build.ts b/core/src/plugins/exec/build.ts index 6c7491896c..53b5d21ea9 100644 --- a/core/src/plugins/exec/build.ts +++ b/core/src/plugins/exec/build.ts @@ -50,6 +50,7 @@ export const execBuildHandler = execBuild.addHandler("build", async ({ action, l const command = action.getSpec("command") const { chalk } = sdk.util + let success = true if (command?.length) { const result = await execRunCommand({ command, action, ctx, log }) @@ -59,7 +60,8 @@ export const execBuildHandler = execBuild.addHandler("build", async ({ action, l } output.detail.fresh = true - output.detail.buildLog = result.all || result.stdout + result.stderr + output.detail.buildLog = result.outputLog + success = result.success } if (output.detail?.buildLog) { @@ -70,11 +72,11 @@ export const execBuildHandler = execBuild.addHandler("build", async ({ action, l renderMessageWithDivider({ prefix, msg: output.detail?.buildLog, - isError: false, + isError: !success, color: chalk.gray, }) ) } - return output + return { ...output, state: success ? "ready" : "failed" } }) diff --git a/core/src/plugins/exec/common.ts b/core/src/plugins/exec/common.ts index a84a88ab5e..5e81dc2ca8 100644 --- a/core/src/plugins/exec/common.ts +++ b/core/src/plugins/exec/common.ts @@ -56,7 +56,10 @@ export async function execRunCommand({ } const outputStream = split2() - outputStream.on("error", () => {}) + outputStream.on("error", (line: Buffer) => { + log.error(line.toString()) + ctx.events.emit("log", { timestamp: new Date().toISOString(), msg: line.toString(), ...logEventContext }) + }) outputStream.on("data", (line: Buffer) => { log.verbose(line.toString()) ctx.events.emit("log", { timestamp: new Date().toISOString(), msg: line.toString(), ...logEventContext }) @@ -72,7 +75,7 @@ export async function execRunCommand({ log.debug(`Running command: ${cmd}`) - return exec(cmd, args, { + const result = await exec(cmd, args, { ...opts, shell, cwd: action.getBuildPath(), @@ -80,6 +83,16 @@ export async function execRunCommand({ stdout: outputStream, stderr: outputStream, }) + + // Comes from error object + const shortMessage = (result as any).shortMessage || "" + + return { + ...result, + outputLog: ((result.stdout || "") + "\n" + (result.stderr || "") + "\n" + shortMessage).trim(), + completedAt: new Date(), + success: result.exitCode === 0, + } } export async function copyArtifacts( diff --git a/core/src/plugins/exec/deploy.ts b/core/src/plugins/exec/deploy.ts index 9f2214bc9f..81954b885b 100644 --- a/core/src/plugins/exec/deploy.ts +++ b/core/src/plugins/exec/deploy.ts @@ -31,12 +31,13 @@ import { execStaticOutputsSchema, } from "./config" import { deployStateToActionState, DeployStatus } from "../../plugin/handlers/Deploy/get-status" -import { Resolved } from "../../actions/types" +import { ActionState, Resolved } from "../../actions/types" import { convertCommandSpec, execRunCommand, getDefaultEnvVars } from "./common" import { isRunning, killRecursive } from "../../process" import { sdk } from "../../plugin/sdk" import { execProvider } from "./exec" import { getTracePropagationEnvVars } from "../../util/tracing/propagation" +import { DeployState } from "../../types/service" const persistentLocalProcRetryIntervalMs = 2500 @@ -193,22 +194,24 @@ execDeploy.addHandler("deploy", async (params) => { opts: { reject: true }, }) - const outputLog = (result.stdout + result.stderr).trim() - if (outputLog) { + if (result.outputLog) { const prefix = `Finished deploying service ${chalk.white(action.name)}. Here is the output:` log.verbose( renderMessageWithDivider({ prefix, - msg: outputLog, - isError: false, + msg: result.outputLog, + isError: !result.success, color: chalk.gray, }) ) } + const deployState: DeployState = result.success ? "ready" : "unhealthy" + const actionState: ActionState = result.success ? "ready" : "failed" + return { - state: "ready", - detail: { state: "ready", detail: { deployCommandOutput: result.all } }, + state: actionState, + detail: { state: deployState, detail: { deployCommandOutput: result.all } }, outputs: {}, } } diff --git a/core/src/plugins/exec/run.ts b/core/src/plugins/exec/run.ts index 554e9802f8..5d69fceb2e 100644 --- a/core/src/plugins/exec/run.ts +++ b/core/src/plugins/exec/run.ts @@ -38,9 +38,9 @@ execRun.addHandler("run", async ({ artifactsPath, log, action, ctx }) => { if (command && command.length) { const commandResult = await execRunCommand({ command, action, ctx, log, env, opts: { reject: false } }) - completedAt = new Date() - outputLog = commandResult.all?.trim() || "" - success = commandResult.exitCode === 0 + completedAt = commandResult.completedAt + outputLog = commandResult.outputLog + success = commandResult.success } else { completedAt = startedAt outputLog = "" @@ -54,7 +54,7 @@ execRun.addHandler("run", async ({ artifactsPath, log, action, ctx }) => { renderMessageWithDivider({ prefix, msg: outputLog, - isError: false, + isError: !success, color: chalk.gray, }) ) diff --git a/core/src/plugins/exec/test.ts b/core/src/plugins/exec/test.ts index abacb2f93b..6565b86ed1 100644 --- a/core/src/plugins/exec/test.ts +++ b/core/src/plugins/exec/test.ts @@ -42,14 +42,13 @@ execTest.addHandler("run", async ({ log, action, artifactsPath, ctx }) => { const { chalk } = sdk.util - const outputLog = result.all?.trim() || "" - if (outputLog) { + if (result.outputLog) { const prefix = `Finished executing ${chalk.white(action.key())}. Here is the full output:` log.verbose( renderMessageWithDivider({ prefix, - msg: outputLog, - isError: false, + msg: result.outputLog, + isError: !result.success, color: chalk.gray, }) ) @@ -60,17 +59,17 @@ execTest.addHandler("run", async ({ log, action, artifactsPath, ctx }) => { command, testName: action.name, version: action.versionString(), - success: result.exitCode === 0, + success: result.success, startedAt, - completedAt: new Date(), - log: outputLog, + completedAt: result.completedAt, + log: result.outputLog, } return { state: runResultToActionState(detail), detail, outputs: { - log: outputLog, + log: result.outputLog, }, } }) diff --git a/core/src/tasks/base.ts b/core/src/tasks/base.ts index 547cf92776..e103ea6c3b 100644 --- a/core/src/tasks/base.ts +++ b/core/src/tasks/base.ts @@ -272,9 +272,8 @@ export abstract class BaseActionTask