-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
PersistentPostRun{,E} are not executed when RunE or PostRunE returns an error #1893
Comments
Same problem here. I'm using the |
I run into the some issue. I have to do some proper cleanup and this is not executed in case run faces an error. |
#914 seems related. I worked around this by moving cleanup code into |
…error Due to a known limitiation of cobra, we did not create support-archives in case a command failed. PersistentPostRun methods of a command are not executed if running the command produced and error. See: spf13/cobra#1893 To ensure a support-archive is always created if requested, the method is instead registered as a global cobra finalizer and will run after any cobra command finished, regardless of status.
…error Due to a known limitiation of cobra, we did not create support-archives in case a command failed. PersistentPostRun methods of a command are not executed if running the command produced and error. See: spf13/cobra#1893 To ensure a support-archive is always created if requested, the method is instead registered as a global cobra finalizer and will run after any cobra command finished, regardless of status.
…error Due to a known limitiation of cobra, we did not create support-archives in case a command failed. PersistentPostRun methods of a command are not executed if running the command produced and error. See: spf13/cobra#1893 To ensure a support-archive is always created if requested, the method is instead registered as a global cobra finalizer and will run after any cobra command finished, regardless of status.
the code here contains the main logic when executing a command:
cobra/command.go
Lines 939 to 963 in 4fa4fdf
Both
PostRun
andPersistentPostRun
functions are not executed when theRunE
function returns an error.This is surprising behaviour for us in podman. Given the name we assumed that this function always runs after the run function.
The documentation for the PostRun function is very brief and this behaviour is not mentioned. So as first step we should fix this to help others not making the same mistake.
I understand that cobra is very stable and changing this behaviour could break others. It is also not clear how cobra could handle the case where both RunE and PostRunE would return an error since the execute function can only return single error.
While it is possible to work around this in our code it is certainly not pretty since we need to know the real child command that was executed to call the correct PersistentPostRunE function on that command.
I think this is a problem that should be fixed here in cobra instead, this likely requires the addition of a new option to make this opt in and keep backwards compat.
The text was updated successfully, but these errors were encountered: