Skip to content

cmd/rm(i): exit code return fix#1770

Open
omegaji wants to merge 1 commit intocontainers:mainfrom
omegaji:fix-1731
Open

cmd/rm(i): exit code return fix#1770
omegaji wants to merge 1 commit intocontainers:mainfrom
omegaji:fix-1731

Conversation

@omegaji
Copy link

@omegaji omegaji commented Mar 14, 2026

This PR aims to fix: #1731

Added error counting to both rm and rmi functions, in order to return an error if any removal operation failed. This also ensures the command returns a non-zero exit code for such a case.

tested it locally:
Screenshot From 2026-03-14 00-47-48

@omegaji omegaji requested a review from debarshiray as a code owner March 14, 2026 05:21
@gemini-code-assist
Copy link

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request enhances the robustness of the rm and rmi commands by introducing a mechanism to correctly signal partial or complete failures. Previously, these commands might have exited with a zero status even if some removal operations failed. The changes ensure that any failure in removing containers or images is reflected in the command's exit status, providing clearer feedback to the user or calling scripts.

Highlights

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • src/cmd/rm.go
    • Introduced an errCount variable to tally failed container removal attempts.
    • Incremented errCount when podman.RemoveContainer fails during bulk deletion.
    • Incremented errCount when container inspection fails or a non-Toolbx container is targeted.
    • Added a final check to return an error with a descriptive message if errCount is greater than zero.
  • src/cmd/rmi.go
    • Introduced an errCount variable to tally failed image removal attempts.
    • Incremented errCount when podman.RemoveImage fails during bulk deletion.
    • Incremented errCount when podman.IsToolboxImage fails or podman.RemoveImage fails for specific images.
    • Added a final check to return an error with a descriptive message if errCount is greater than zero.
Activity
  • No human activity has been recorded on this pull request yet.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request correctly addresses the issue of ensuring a non-zero exit code when rm or rmi commands encounter errors during execution. The approach of counting errors and returning an error at the end is sound. My feedback includes a couple of suggestions to improve the clarity of the final error messages and to use more idiomatic Go for error formatting. Overall, this is a good fix.

Comment on lines +118 to +119
errMsg := fmt.Sprintf("failed to remove %d containers", errCount)
return errors.New(errMsg)

Choose a reason for hiding this comment

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

medium

The error message failed to remove might not be fully accurate in all cases, as errCount is also incremented for inspection failures or if a container is not a Toolbx container. A more generic message would be more appropriate. Also, you can use fmt.Errorf to create a formatted error directly, which is more idiomatic in Go.

Suggested change
errMsg := fmt.Sprintf("failed to remove %d containers", errCount)
return errors.New(errMsg)
return fmt.Errorf("encountered errors with %d containers", errCount)

Copy link
Author

Choose a reason for hiding this comment

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

It is true that we can encounter errors that may prevent us from even calling podman to remove them

But my reasoning for still sticking with the failed to remove error was:
Any error encountered during the removal process prevents the container or image from being removed. So, in essence, isn’t it a failure of the removal process as a whole?

Comment on lines +111 to +112
errMsg := fmt.Sprintf("failed to remove %d images", errCount)
return errors.New(errMsg)

Choose a reason for hiding this comment

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

medium

For consistency and to be more idiomatic, consider using fmt.Errorf to create the formatted error message directly. A slightly more generic message could also improve clarity.

Suggested change
errMsg := fmt.Sprintf("failed to remove %d images", errCount)
return errors.New(errMsg)
return fmt.Errorf("encountered errors with %d images", errCount)

Copy link
Author

Choose a reason for hiding this comment

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

I saw a lot of places using errors.New(), especially in the files that this PR touches. So with that reasoning I went forward with errors.New.

But I am not against using it :)

omegaji added a commit to omegaji/toolbox that referenced this pull request Mar 14, 2026
both rm and rmi commands now count any failed removals and return an error
whenever failed removals are non-zero

containers#1770
@softwarefactory-project-zuul
Copy link

both rm and rmi commands now count any failed removals and return an error
whenever failed removals are non-zero

containers#1770
Signed-off-by: om <omhpurohit@gmail.com>
@softwarefactory-project-zuul
Copy link

@omegaji
Copy link
Author

omegaji commented Mar 23, 2026

@debarshiray would you be able to take a look at this? Sorry for pinging!

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.

The rm and rmi commands exits with status 0 even after an error

1 participant