Skip to content

fix(cli): prevent handled errors from producing duplicate output [CLI-1765] - #7130

Open
bdemeo12 wants to merge 3 commits into
mainfrom
CLI-1765/prevent-duplicate-error-output
Open

fix(cli): prevent handled errors from producing duplicate output [CLI-1765]#7130
bdemeo12 wants to merge 3 commits into
mainfrom
CLI-1765/prevent-duplicate-error-output

Conversation

@bdemeo12

@bdemeo12 bdemeo12 commented Aug 13, 2026

Copy link
Copy Markdown
Contributor

Pull Request Submission Checklist

  • Follows CONTRIBUTING guidelines
  • Commit messages
    are release-note ready, emphasizing
    what was changed, not how.
  • Includes detailed description of changes
  • Contains risk assessment (Low | Medium | High)
  • Highlights breaking API changes (if applicable) — none
  • Links to automated tests covering new functionality
  • Includes manual testing instructions (if necessary)
  • Updates relevant GitBook documentation (PR link: ___) — n/a, no user-facing docs change
  • Includes product update to be announced in the next stable release notes

What does this PR do?

Fixes:

json output being unparseable because an extra error was printed after the result. This happens when the CLI receives an error (aside from exit status 1), it prints it — so once teardown joins the exit error together with the handled network errors, the result is no longer exit status 1 and gets printed after the real output.

Also pins snyk-docker-plugin to 9.20.0 so CI exercises the failure being fixed. (Original PR: #7047 + discussion: https://snyksec.atlassian.net/servicedesk/customer/portal/64/CLIA-1576)

Where should the reviewer start?

cliv2/pkg/core/main.goshouldSuppressDisplay and the third return value from processError, then the guarded displayError call in tearDown.

Then cliv2/pkg/core/main_test.go for the two new cases in Test_processError and the new Test_shouldSuppressDisplay.

How should this be manually tested?

Against an image whose provenance fetch fails (SDP version 9.20.0 +):

snyk container test <image> --json > out.json
jq . out.json

The container.spec.ts acceptance job is the automated equivalent — it fails on 9.20.0 without this fix, which is why the pin is included here.

What's the product update that needs to be communicated to CLI users?

n/a

@snyk-io

snyk-io Bot commented Aug 13, 2026

Copy link
Copy Markdown

Snyk checks have passed. No issues have been found so far.

Status Scan Engine Critical High Medium Low Total (0)
Open Source Security 0 0 0 0 0 issues
Licenses 0 0 0 0 0 issues
Code Security 0 0 0 0 0 issues

💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse.

@bdemeo12

Copy link
Copy Markdown
Contributor Author

This PR originates from: #7047

Comment thread cliv2/pkg/core/main.go Outdated

for _, err := range unwrappedErrs {
var exitErr *exec.ExitError
if errors.As(err, &exitErr) && exitErr.ExitCode() < constants.SNYK_EXIT_CODE_ERROR {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An exit code below SNYK_EXIT_CODE_ERROR means the command worked and anything alongside it is noise

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found a second reproduction after this PR was opened: running snyk code test can successfully return findings while an auxiliary organization-slug lookup returns 403. The scan returns ErrorWithExitCode{1}, which is then joined with the captured Forbidden error and printed after the results.

This implementation appears to fix the Container case (exec.ExitError{1}), but not this Code case because the early suppression only checks exec.ExitError. Could we determine suppression from the original command error before joining, covering both error types?

Comment thread cliv2/pkg/core/main.go
//
// Joined errors match no direct type assertion, so they are unwrapped and checked
// one at a time.
func shouldSuppressDisplay(err error) bool {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All errors are combined here:

resultErrorList = append([]error{resultError}, resultErrorList...)

shouldSuppressDisplay returns true when there's nothing to show:

if shouldSuppressDisplay(err) {

Previously displayError checked the error's type directly. it didnt account for a combined error, so it fell through to the printing code and emitted a second JSON object.:

if isExitError || isErrorWithCode || errorHasBeenShown(err) {

Now this check suppresses the display err by returning early, before the printing

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Code reproduction also makes me think the original error is the better place to make this decision. Before processError, we know whether the command returned exec.ExitError or ErrorWithExitCode; after joining, we have to infer that recursively and risk suppressing unrelated sibling errors.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could calculate suppression from the original error in tearDown func, before processError joins anything?

suppressDisplay := shouldSuppressDisplay(err)
if err != nil {
    allErrors, outputError = processError(err, errorList)
    // If processError selected a different error, such as maintenance or the
    // captured cause of TS_CLI_TERMINATED, that error still needs displaying.
    suppressDisplay = suppressDisplay && errors.Is(outputError, err)
}
if !suppressDisplay {
    displayError(outputError, ...)
}

shouldSuppressDisplay could then retain the simple direct checks for exec.ExitError, ErrorWithExitCode, and already-displayed errors. This covers both cases while avoiding recursive unwrapping.

@bdemeo12
bdemeo12 force-pushed the CLI-1765/prevent-duplicate-error-output branch from 668cf76 to bc48bab Compare August 17, 2026 00:33
Comment thread package.json
"snyk-config": "^5.0.0",
"snyk-cpp-plugin": "^2.24.3",
"snyk-docker-plugin": "9.19.0",
"snyk-docker-plugin": "9.20.0",

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bumped SDP to use as an acceptance test

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Errors are joined before they reach displayError, and a joined error
matched none of its direct type assertions, so an already-handled error
was printed after the command's real output - emitting a second JSON
object and breaking JSON.parse(stdout).

displayError now unwraps joined errors and checks them one at a time.

Pins snyk-docker-plugin to 9.20.0 so CI exercises the failure being fixed.

CLI-1765

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@bdemeo12
bdemeo12 force-pushed the CLI-1765/prevent-duplicate-error-output branch from bc48bab to 33b1438 Compare August 17, 2026 01:08
@bdemeo12
bdemeo12 marked this pull request as ready for review August 17, 2026 01:59
@bdemeo12
bdemeo12 requested a review from a team as a code owner August 17, 2026 01:59
@snyk-pr-review-bot

This comment has been minimized.

…765]

Evaluate suppression on the original command error (exec.ExitError or
ErrorWithExitCode, exit code < SNYK_EXIT_CODE_ERROR) instead of the joined
error, covering the snyk code test case, and guard with errors.Is so a
promoted error is still displayed.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@github-actions

github-actions Bot commented Aug 18, 2026

Copy link
Copy Markdown
Contributor
Warnings
⚠️ There are multiple commits on your branch, please squash them locally before merging!
⚠️

"[fix(cli): prevent handled errors from producing duplicate output CLI-1765](#7130)" is too long. Keep the first line of your commit message under 72 characters.

⚠️

"[fix(cli): decide error suppression from the original exit code CLI-1765](https://api.github.com/repos/snyk/cli/git/commits/e0ef82686807ad624443322b8ab6ed86fae5f447)" is too long. Keep the first line of your commit message under 72 characters.

Generated by 🚫 dangerJS against 1319e14

@snyk-pr-review-bot

This comment has been minimized.

@snyk-pr-review-bot

Copy link
Copy Markdown

PR Reviewer Guide 🔍

🧪 PR contains tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Tight Suppression Coupling 🟡 [minor]

The check errors.Is(outputError, err) in tearDown ensures suppression only occurs if the error processed by processError is semantically equivalent to the input error. If processError wraps the error in a way that errors.Is returns false (e.g. if it uses a wrapper that doesn't properly unwrap), the suppression logic will be bypassed, leading to the duplicate output this PR intends to fix. Given processError isn't visible in the diff but is known to aggregate errors, ensure it uses errors.Join or similar standard wrapping.

suppressDisplay = shouldSuppressDisplay(err) && errors.Is(outputError, err)
📚 Repository Context Analyzed

This review considered 9 relevant code sections from 6 files (average relevance: 0.88)

🤖 Repository instructions applied (from AGENTS.md)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants