Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ensure nested legacycli invocations forward errors #5709

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

CatalinSnyk
Copy link
Contributor

@CatalinSnyk CatalinSnyk commented Feb 4, 2025

Pull Request Submission Checklist

  • Follows CONTRIBUTING guidelines
  • Includes detailed description of changes
  • Contains risk assessment (Low | Medium | High)
  • Highlights breaking API changes (if applicable)
  • Links to automated tests covering new functionality
  • Includes manual testing instructions (if necessary)
  • Updates relevant GitBook documentation (PR link: ___)

What does this PR do?

Following the work from CLI-646 that added the capability of capturing errors from TS CLI invocations, this adds PR aims to ensure nested invocations of the TS CLI are forwarded properly to debug logs, analytics and output.

One current gap left from CLI-646 is the fact that if the --sarif or --json flags are set, we don't send this errors to the IPC. We can retrieve error messages from the JSON payload, but in the case of SARIF, there is no error message present.
Extracting the error message from the JSON payload is just about parsing and accessing the "error" field. I added a fallback case and in case parsing fails, we default back to the normal Error.message field.

Here's an example of an error caught when the --json flag is set:

Error{
    message: "{\"ok\":false,\"error\":\"This is the actual error message\",\"path":\"./\"}"
    ...otherFields
}

Where should the reviewer start?

  • main.ts - Removed JSON from the list of IPC exceptions, and moved the output case above the rest, just to simplify the conditions a bit and since the TS CLI will always output the errors in JSON format, while also sending them to the IPC.
  • ipc.ts - Added a new argument to the sendError function: isJson is set when the --json flag is set, this will add a meta field "suppressJsonOutput" to the error in order to signal the Golang CLI to now show this error. Other things added was just around extracting the error message from the stringified JSON payload from the messsage field of the caught error.
  • main.go - The output workflow configuration values were not set correctly in some cases because the flags were not added as persistent. The other thing added here was generating the right error message for the JSON output; if the error is Error Catalog, the proper message is the detail field. This also takes into account the suppressJsonOutput field.

How should this be manually tested?

Using the snyk-macos-arm64 sbom --format cyclonedx1.4+json --file=./foldernotthere command, the expected error message would surface the fact that the specified file is not present, instead of the "An error occurred while running the underlying analysis needed to generate the SBOM." message. Other commands that make use of TS CLI workflow invocations should also surface the errors captured though the IPC.

Related

Copy link
Contributor

github-actions bot commented Feb 4, 2025

Warnings
⚠️

Since the CLI is unifying on a standard and improved tooling, we're starting to migrate old-style imports and exports to ES6 ones.
A file you've modified is using either module.exports or require(). If you can, please update them to ES6 import syntax and export syntax.
Files found:

  • src/cli/ipc.ts
  • src/cli/main.ts
  • test/setup-jest.ts

Generated by 🚫 dangerJS against f48415e

@CatalinSnyk CatalinSnyk changed the title fix: propagate errors when --json is used fix: ensure nested legacycli invocations forward errors Feb 4, 2025
@CatalinSnyk CatalinSnyk force-pushed the fix/ensure-nested-legacycli-invocations-forward-errors branch from c4f87b9 to c24b51e Compare February 10, 2025 07:46
@CatalinSnyk CatalinSnyk marked this pull request as ready for review February 10, 2025 09:24
@CatalinSnyk CatalinSnyk requested a review from a team as a code owner February 10, 2025 09:24
@@ -1,7 +1,9 @@
const stripAnsi = require('strip-ansi');
Copy link
Member

Choose a reason for hiding this comment

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

issue: Use imports here rather than commonjs require format.

src/cli/ipc.ts Outdated
err.metadata.status = 0;
}

const data = (err as ProblemError)
.toJsonApi('error-catalog-ipc-instance?')
.toJsonApi('error-catalog-ipc-instance')
Copy link
Member

Choose a reason for hiding this comment

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

question: Why remove the ? character from this string?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added that in the previous PR when I was reading about the instance parameter (and I was confused about what it did), and I missed removing that 😅

Copy link
Member

Choose a reason for hiding this comment

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

Can we remove that as part of this change?


// NOTE: Only set if the invocation uses the --json flag. There's no need for output suppression if the invocation does NOT use StdIO
// (meaning the TS CLI is handling the user output directly), so we can disable the suppression in order to surface this errors properly.
useStdIO := c.globalConfig.GetBool(configuration.WORKFLOW_USE_STDIO)
Copy link
Member

Choose a reason for hiding this comment

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

question: The comment here mentions: Only set if the invocation uses the --json flag. But below it looks like we are checking, configuration.WORKFLOW_USE_STDIO, how does that relate to the --json flag?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was referring to the "suppressJsonOutput" field being set, will update this to be a bit more expressive about that.

@@ -539,6 +562,9 @@ func MainWithErrorCode() (int, []error) {
outputWorkflow, _ := globalEngine.GetWorkflow(localworkflows.WORKFLOWID_OUTPUT_WORKFLOW)
outputFlags := workflow.FlagsetFromConfigurationOptions(outputWorkflow.GetConfigurationOptions())
rootCommand.PersistentFlags().AddFlagSet(outputFlags)
// add output flags as persistent flags
_ = rootCommand.ParseFlags(os.Args)
Copy link
Member

Choose a reason for hiding this comment

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

Question: What is the incorrect behaviour you were seeing which required this change? I am not sure I understand how it is related to the change you are looking to make here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The configuration values were not being populated properly (eg: even if the --json flag was in the args, the configuration flag for the JSON output would have the default value of false).
This would result in incorrect output for errors sent thought IPC: we would get the normal human readable output even if JSON formatted output was expected.

@CatalinSnyk CatalinSnyk force-pushed the fix/ensure-nested-legacycli-invocations-forward-errors branch from c24b51e to f48415e Compare February 12, 2025 13:44
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