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

PersistentPostRun{,E} are not executed when RunE or PostRunE returns an error #1893

Open
Luap99 opened this issue Jan 9, 2023 · 3 comments

Comments

@Luap99
Copy link
Contributor

Luap99 commented Jan 9, 2023

the code here contains the main logic when executing a command:

cobra/command.go

Lines 939 to 963 in 4fa4fdf

if c.RunE != nil {
if err := c.RunE(c, argWoFlags); err != nil {
return err
}
} else {
c.Run(c, argWoFlags)
}
if c.PostRunE != nil {
if err := c.PostRunE(c, argWoFlags); err != nil {
return err
}
} else if c.PostRun != nil {
c.PostRun(c, argWoFlags)
}
for p := c; p != nil; p = p.Parent() {
if p.PersistentPostRunE != nil {
if err := p.PersistentPostRunE(c, argWoFlags); err != nil {
return err
}
break
} else if p.PersistentPostRun != nil {
p.PersistentPostRun(c, argWoFlags)
break
}
}

Both PostRun and PersistentPostRun functions are not executed when the RunE 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.

@GiulianoDecesares
Copy link

Same problem here. I'm using the PersistentPostRun to do some cleanup but when the child command returns an error, the PersistentPostRun method is not called and therefore the cleanup is not performed properly. Possible workarounds for this are not pretty at all.

@beatrausch
Copy link

I run into the some issue. I have to do some proper cleanup and this is not executed in case run faces an error.

@zeisss
Copy link

zeisss commented Jun 2, 2023

#914 seems related.

I worked around this by moving cleanup code into cobra.OnFinalize()

UnseenWizzard added a commit to Dynatrace/dynatrace-configuration-as-code that referenced this issue Aug 25, 2023
…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.
UnseenWizzard added a commit to Dynatrace/dynatrace-configuration-as-code that referenced this issue Aug 28, 2023
…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.
UnseenWizzard added a commit to Dynatrace/dynatrace-configuration-as-code that referenced this issue Aug 28, 2023
…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.
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

No branches or pull requests

4 participants