diff --git a/backend/remote/backend_common.go b/backend/remote/backend_common.go index c2d6602dd..a2d5e07d8 100644 --- a/backend/remote/backend_common.go +++ b/backend/remote/backend_common.go @@ -269,6 +269,14 @@ func (b *Remote) costEstimate(stopCtx, cancelCtx context.Context, op *backend.Op return generalError("Failed to retrieve cost estimate", err) } + // If the run is canceled or errored, but the cost-estimate still has + // no result, there is nothing further to render. + if ce.Status != tfe.CostEstimateFinished { + if r.Status == tfe.RunCanceled || r.Status == tfe.RunErrored { + return nil + } + } + switch ce.Status { case tfe.CostEstimateFinished: delta, err := strconv.ParseFloat(ce.DeltaMonthlyCost, 64) @@ -324,18 +332,27 @@ func (b *Remote) checkPolicy(stopCtx, cancelCtx context.Context, op *backend.Ope b.CLI.Output("\n------------------------------------------------------------------------\n") } for i, pc := range r.PolicyChecks { - logs, err := b.client.PolicyChecks.Logs(stopCtx, pc.ID) - if err != nil { - return generalError("Failed to retrieve policy check logs", err) - } - reader := bufio.NewReaderSize(logs, 64*1024) - // Retrieve the policy check to get its current status. pc, err := b.client.PolicyChecks.Read(stopCtx, pc.ID) if err != nil { return generalError("Failed to retrieve policy check", err) } + // If the run is canceled or errored, but the policy check still has + // no result, there is nothing further to render. + if r.Status == tfe.RunCanceled || r.Status == tfe.RunErrored { + switch pc.Status { + case tfe.PolicyPending, tfe.PolicyQueued, tfe.PolicyUnreachable: + continue + } + } + + logs, err := b.client.PolicyChecks.Logs(stopCtx, pc.ID) + if err != nil { + return generalError("Failed to retrieve policy check logs", err) + } + reader := bufio.NewReaderSize(logs, 64*1024) + var msgPrefix string switch pc.Scope { case tfe.PolicyScopeOrganization: diff --git a/backend/remote/backend_plan.go b/backend/remote/backend_plan.go index c73c63f18..c83d91bc4 100644 --- a/backend/remote/backend_plan.go +++ b/backend/remote/backend_plan.go @@ -308,12 +308,11 @@ in order to capture the filesystem context the remote workspace expects: return r, generalError("Failed to retrieve run", err) } - // Return if the run is canceled or errored. We return without - // an error, even if the run errored, as the error is already - // displayed by the output of the remote run. - if r.Status == tfe.RunCanceled || r.Status == tfe.RunErrored { - return r, nil - } + // If the run is canceled or errored, we still continue to the + // cost-estimation and policy check phases to ensure we render any + // results available. In the case of a hard-failed policy check, the + // status of the run will be "errored", but there is still policy + // information which should be shown. // Show any cost estimation output. if r.CostEstimate != nil {