diff --git a/Library/Homebrew/cask/cmd/doctor.rb b/Library/Homebrew/cask/cmd/doctor.rb index ddd033a2e1f5c..0a6a015bba9f5 100644 --- a/Library/Homebrew/cask/cmd/doctor.rb +++ b/Library/Homebrew/cask/cmd/doctor.rb @@ -67,7 +67,7 @@ def check_staging_location if path.exist? && !path.writable? add_error "The staging path #{user_tilde(path.to_s)} is not writable by the current user." - add_error "To fix, run \'sudo chown -R ${USER}:staff #{user_tilde(path.to_s)}'" + add_error "To fix, run \'sudo chown -R $(whoami):staff #{user_tilde(path.to_s)}'" end puts user_tilde(path.to_s) diff --git a/Library/Homebrew/formula_installer.rb b/Library/Homebrew/formula_installer.rb index b549b762d6b07..e861f38c786f0 100644 --- a/Library/Homebrew/formula_installer.rb +++ b/Library/Homebrew/formula_installer.rb @@ -289,7 +289,14 @@ def install rescue Exception => e # rubocop:disable Lint/RescueException # any exceptions must leave us with nothing installed ignore_interrupts do - formula.prefix.rmtree if formula.prefix.directory? + begin + formula.prefix.rmtree if formula.prefix.directory? + rescue Errno::EACCES, Errno::ENOTEMPTY + odie <<~EOS + Could not remove #{formula.prefix.basename} keg! Do so manually: + sudo rm -rf #{formula.prefix} + EOS + end formula.rack.rmdir_if_possible end raise if ARGV.homebrew_developer? || diff --git a/Library/Homebrew/keg.rb b/Library/Homebrew/keg.rb index 5c509d4600b1c..bafea64514e51 100644 --- a/Library/Homebrew/keg.rb +++ b/Library/Homebrew/keg.rb @@ -310,8 +310,11 @@ def uninstall remove_opt_record if optlinked? remove_old_aliases remove_oldname_opt_record - rescue Errno::ENOTEMPTY - ofail "Could not remove #{path}! Check its permissions." + rescue Errno::EACCES, Errno::ENOTEMPTY + odie <<~EOS + Could not remove #{name} keg! Do so manually: + sudo rm -rf #{path} + EOS end def unlink(mode = OpenStruct.new) diff --git a/Library/Homebrew/reinstall.rb b/Library/Homebrew/reinstall.rb index 015fc8a4687ca..51862302a131e 100644 --- a/Library/Homebrew/reinstall.rb +++ b/Library/Homebrew/reinstall.rb @@ -45,12 +45,26 @@ def reinstall_formula(f, build_from_source: false) ignore_interrupts { restore_backup(keg, keg_was_linked) } raise else - backup_path(keg).rmtree if backup_path(keg).exist? + begin + backup_path(keg).rmtree if backup_path(keg).exist? + rescue Errno::EACCES, Errno::ENOTEMPTY + odie <<~EOS + Could not remove #{backup_path(keg).parent.basename} backup keg! Do so manually: + sudo rm -rf #{backup_path(keg)} + EOS + end end def backup(keg) keg.unlink - keg.rename backup_path(keg) + begin + keg.rename backup_path(keg) + rescue Errno::EACCES, Errno::ENOTEMPTY + odie <<~EOS + Could not rename #{keg.name} keg! Check/fix its permissions: + sudo chown -R $(whoami) #{keg} + EOS + end end def restore_backup(keg, keg_was_linked) diff --git a/docs/Gems,-Eggs-and-Perl-Modules.md b/docs/Gems,-Eggs-and-Perl-Modules.md index 8fafd9655ac68..9b6bd42308bf5 100644 --- a/docs/Gems,-Eggs-and-Perl-Modules.md +++ b/docs/Gems,-Eggs-and-Perl-Modules.md @@ -116,7 +116,7 @@ If you ever did a `sudo gem`, etc. before then a lot of files will have been created owned by root. Fix with: ```sh -sudo chown -R $USER /Library/Ruby /Library/Perl /Library/Python +sudo chown -R $(whoami) /Library/Ruby /Library/Perl /Library/Python ``` ## Perl CPAN modules without sudo