Skip to content

cli: avoid os.Exit() in Run() funcs#7788

Merged
srenatus merged 2 commits intoopen-policy-agent:mainfrom
srenatus:sr/pnuszowyrsnp
Jul 23, 2025
Merged

cli: avoid os.Exit() in Run() funcs#7788
srenatus merged 2 commits intoopen-policy-agent:mainfrom
srenatus:sr/pnuszowyrsnp

Conversation

@srenatus
Copy link
Copy Markdown
Contributor

os.Exit immediately exits the program and doesn't run defer functions.
This can be problematic as any command.OnFinalize routines and any logic
after the command.Execute won't be run.

Also suppress all RunE cobra error and usage messages. These would be
printed twice otherwise.

This also wraps any Rego errors. In some follow-up work, we'll try to enhance
error messages according to whatever happened during opa eval. 🤞

@netlify
Copy link
Copy Markdown

netlify Bot commented Jul 23, 2025

Deploy Preview for openpolicyagent ready!

Name Link
🔨 Latest commit b369ea7
🔍 Latest deploy log https://app.netlify.com/projects/openpolicyagent/deploys/6880e5f6309e3f0008accdd1
😎 Deploy Preview https://deploy-preview-7788--openpolicyagent.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@srenatus srenatus marked this pull request as draft July 23, 2025 13:02
@srenatus srenatus force-pushed the sr/pnuszowyrsnp branch 3 times, most recently from 8e8bfbf to 679ca32 Compare July 23, 2025 13:26
kevinstyra and others added 2 commits July 23, 2025 15:38
…f OPA

`os.Exit` immediately exits the program and doesn't run defer functions.
This can be problematic as any command.OnFinalize routines and any logic
after the command.Execute won't be run.

Also suppress all RunE cobra error and usage messages. These would be
printed twice otherwise.

Signed-off-by: Stephan Renatus <stephan@styra.com>
Co-authored-by: Kevin St. Pierre <kevin@styra.com>
Signed-off-by: Stephan Renatus <stephan@styra.com>
@srenatus srenatus marked this pull request as ready for review July 23, 2025 13:48
Copy link
Copy Markdown
Contributor

@johanfylling johanfylling left a comment

Choose a reason for hiding this comment

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

LGTM 👍

Comment thread main.go
os.Exit(1)
var e *cmd.ExitError
if errors.As(err, &e) {
exit = e.Exit
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.

Any wrapped error will be unwrapped and reported by cobra?

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.

Or that is what cmd.SilenceErrors = true turns off? When do we benefit from wrapping an 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.

Not just yet -- but that's follow-up stuff. 🤞

Comment thread cmd/test.go
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.

startWatcher() still has an os.Exit(1), but it's run in a goroutine, so maybe it's tricky to pull out of there.

Comment thread cmd/run.go
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.

There is still an os.Exit(1) in init(). Dropping it wouldn't impact what you're doing here, from what I understand, but it raises the question: do we need that? If we fail to deprecate a flag, maybe we don't really care and should prioritize a non-bricked build(?).

Comment thread cmd/version.go
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceErrors = true
cmd.SilenceUsage = true
return generateCmdOutput(os.Stdout, check)
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.

Here we shouldn't wrap the 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.

Not really, it's not evaluating user-controlled rego code...

@srenatus srenatus merged commit e3f6be6 into open-policy-agent:main Jul 23, 2025
31 checks passed
@srenatus srenatus deleted the sr/pnuszowyrsnp branch July 23, 2025 17:04
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.

3 participants