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
cmd/uninstall: Add env variable that runs autoremove after uninstalls #13532
cmd/uninstall: Add env variable that runs autoremove after uninstalls #13532
Conversation
Review period will end on 2022-07-11 at 18:35:33 UTC. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
997b4a0
to
80d9d37
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I love this, great work so far!
Review period ended. |
0e947e7
to
c686739
Compare
Okay, I took a stab at moving the autoremove logic out into other files so that we don't have to shell out to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great so far! Would love some unit tests for the Formula
methods.
Library/Homebrew/env_config.rb
Outdated
@@ -343,6 +343,10 @@ module EnvConfig | |||
default_text: "macOS: `/private/tmp`, Linux: `/tmp`.", | |||
default: HOMEBREW_DEFAULT_TEMP, | |||
}, | |||
HOMEBREW_UNINSTALL_AUTOREMOVE: { | |||
description: "If set, `brew autoremove` is run after every successful call to `brew uninstall`.", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I misread this before. I wonder if a HOMEBREW_INSTALL_AUTOREMOVE
makes sense to have as well that is run on each install/upgrade/reinstall or whether a HOMEBREW_AUTOREMOVE
should do all of these. Thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have no opinion on when exactly we should do this, but I have a strong opinion against having multiple environment variables.
If we do it after multiple commands then it should just be HOMEBREW_AUTOREMOVE
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Works for me @carlocab. I'll personally enable HOMEBREW_AUTOREMOVE
as soon as it's around and will primarily rely on it being used at install
time.
Thinking about this more, though: we automatically run brew cleanup
at install time so I wonder whether running it at uninstall
or cleanup
time would be the most sensible implementation of HOMEBREW_AUTOREMOVE
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Running it as part of/after brew cleanup
makes sense to me 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be clear you're suggesting running autoremove
after install
, uninstall
, upgrade
and reinstall
, is that right?
I just took a look at how cleanup
is handled internally and it makes me think that a separate autoremove.rb
file might be the way to go with this.
Also, HOMEBREW_AUTOREMOVE
makes sense to me too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be clear you're suggesting running autoremove after install, uninstall, upgrade and reinstall, is that right?
Yes, but I (now) think the thing that should run it after install/upgrade/reinstall is that brew cleanup
should run the autoremove logic if HOMEBREW_AUTOREMOVE
is set (and also perhaps if --autoremove
is passed if people think that makes sense 🤔)? I could see --autoremove
being useful for brew bundle cleanup
.
When HOMEBREW_UNINSTALL_AUTOREMOVE is set, `brew autoremove` is run after every successful call to `brew uninstall`.
This allows us to autoremove formulae in the autoremove and uninstall commands without having to shell out to brew.
This allows us to call that logic internally in other brew commands instead of having to shell out.
c686739
to
fa43418
Compare
Okay, Beyond that all the logic for Also, it seems like there are currently no tests that exercise the |
Ah, and one more thing. It now runs after |
Library/Homebrew/autoremove.rb
Outdated
module_function | ||
|
||
def remove_unused_formulae(dry_run: false) | ||
removable_formulae = unused_formulae_with_no_dependents |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be good to have some basic unit tests to cover this module. This can wait until after we're agreed on design but would be good to have before PR lands.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, I agree.
docs/Manpage.md
Outdated
@@ -1945,6 +1945,9 @@ example, run `export HOMEBREW_NO_INSECURE_REDIRECT=1` rather than just | |||
|
|||
*Default:* `300`. | |||
|
|||
- `HOMEBREW_AUTOREMOVE` | |||
<br>If set, calls to `brew install`, `brew upgrade`, `brew reinstall` and `brew uninstall` will automatically remove unused formula dependents. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
<br>If set, calls to `brew install`, `brew upgrade`, `brew reinstall` and `brew uninstall` will automatically remove unused formula dependents. | |
<br>If set, calls to `brew cleanup` and `brew uninstall` will automatically remove unused formula dependents. Unless HOMEBREW_NO_INSTALL_CLEANUP is set, `brew cleanup` will automatically remove unused formula dependents every 30 days. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought we were going to test this out for a while first before making it the default. This would make running brew autoremove
every 30 days the default. Is that what we want?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, what I was intending with this messaging is "if you set HOMEBREW_AUTOREMOVE and not HOMEBREW_NO_INSTALL_CLEANUP: brew cleanup
will start doing brew autoremove
periodically".
Cleanup.rb: - Added #autoremove method - #autoremove is called in clean when HOMEBREW_AUTOREMOVE is set Formula.rb: - Added #unused_formulae_with_no_dependents and helpers Removed old autoremove.rb module.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is great! A few tiny optional style nits and some unit tests and I think this is good to go!
Also, made sure to clear formula cache before and after autoremoving packages.
Updated docs for: HOMEBREW_AUTOREMOVE HOMEBREW_NO_CLEANUP_FORMULAE
Okay, I added a few tests to make sure dependent formulae get filtered out before removing them. Would an integration test make sense here or do you think that's enough? The docs were updated as well for two environment variables.
The other main thing that was changed is that two calls to |
I've also been wondering if we should update the tab associated with a formula if it was originally installed as a dependency but then the user tries to install it explicitly later on ( |
I think we already do? Also, note that "installed as dependency" does not imply "not installed as request"; both can be true. |
You're absolutely right. I had just missed it. |
54bd5d2
to
7bd06f6
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work here! Happy for this to ship as-is or with tiny suggested changed.
expect(testball2.any_version_installed?).to be true | ||
|
||
# When there are no unused dependencies | ||
expect { brew "autoremove" } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally I'd say only have one brew
invocation in this test, even if it reduces the coverage a bit, as it's super slow.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On my machine there's only like a 5 second difference between running one and two tests and I'm still using this old iMac. Not sure how important that 5 seconds is. But yeah, I can take one out if you want.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think important enough to omit tbh, sorry!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it. Changed it to only one brew
invocation in the integration test.
7bd06f6
to
d04051a
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work! Merge when ready.
brew style
with your changes locally?brew typecheck
with your changes locally?brew tests
with your changes locally?When HOMEBREW_UNINSTALL_AUTOREMOVE is set,
brew autoremove
is run after every successful call tobrew uninstall
. If there are no formulae or casks to uninstall, it exits early (cmd/uninstall.rb:53).Resolves #13362.