diff --git a/Library/Homebrew/cmd/link.rb b/Library/Homebrew/cmd/link.rb index 3f84329730..dac871ade3 100644 --- a/Library/Homebrew/cmd/link.rb +++ b/Library/Homebrew/cmd/link.rb @@ -27,49 +27,62 @@ module Homebrew ARGV.kegs.each do |keg| keg_only = Formulary.keg_only?(keg.rack) - if HOMEBREW_PREFIX.to_s == "/usr/local" && keg_only - if keg.name.start_with?("openssl", "libressl") - opoo <<~EOS - Refusing to link: #{keg.name} - Linking keg-only #{keg.name} means you may end up linking against the insecure, - deprecated system OpenSSL while using the headers from Homebrew's #{keg.name}. - Instead, pass the full include/library paths to your compiler e.g.: - -I#{HOMEBREW_PREFIX}/opt/#{keg.name}/include -L#{HOMEBREW_PREFIX}/opt/#{keg.name}/lib - EOS - next - elsif (MacOS.version >= :mojave || - MacOS::Xcode.version >= "10.0" || - MacOS::CLT.version >= "10.0") && - keg.to_formula.keg_only_reason.reason == :provided_by_macos - opoo <<~EOS - Refusing to link macOS-provided software: #{keg.name} - Instead, pass the full include/library paths to your compiler e.g.: - -I#{HOMEBREW_PREFIX}/opt/#{keg.name}/include -L#{HOMEBREW_PREFIX}/opt/#{keg.name}/lib - EOS - next - end - elsif keg.linked? - opoo "Already linked: #{keg}" - puts "To relink: brew unlink #{keg.name} && brew link #{keg.name}" - next - elsif keg_only && !ARGV.force? - opoo "#{keg.name} is keg-only and must be linked with --force" - puts "Note that doing so can interfere with building software." - puts_keg_only_path_message(keg) - next - elsif mode.dry_run && mode.overwrite - puts "Would remove:" - keg.link(mode) + if keg.linked? + opoo "Already linked: #{keg}" + name_and_flag = if keg_only + "--force #{keg.name}" + else + keg.name + end + puts "To relink: brew unlink #{keg.name} && brew link #{name_and_flag}" next - elsif mode.dry_run - puts "Would link:" + end + + if mode.dry_run + if mode.overwrite + puts "Would remove:" + else + puts "Would link:" + end keg.link(mode) puts_keg_only_path_message(keg) if keg_only - next end + if keg_only + if HOMEBREW_PREFIX.to_s == "/usr/local" + if keg.to_formula.keg_only_reason.reason == :provided_by_macos && + (MacOS.version >= :mojave || + MacOS::Xcode.version >= "10.0" || + MacOS::CLT.version >= "10.0") + opoo <<~EOS + Refusing to link macOS-provided software: #{keg.name} + Instead, pass the full include/library paths to your compiler e.g.: + -I#{HOMEBREW_PREFIX}/opt/#{keg.name}/include -L#{HOMEBREW_PREFIX}/opt/#{keg.name}/lib + EOS + next + end + + if keg.name.start_with?("openssl", "libressl") + opoo <<~EOS + Refusing to link: #{keg.name} + Linking keg-only #{keg.name} means you may end up linking against the insecure, + deprecated system OpenSSL while using the headers from Homebrew's #{keg.name}. + Instead, pass the full include/library paths to your compiler e.g.: + -I#{HOMEBREW_PREFIX}/opt/#{keg.name}/include -L#{HOMEBREW_PREFIX}/opt/#{keg.name}/lib + EOS + next + end + end + + unless ARGV.force? + opoo "#{keg.name} is keg-only and must be linked with --force" + puts_keg_only_path_message(keg) + next + end + end + keg.lock do print "Linking #{keg}... " puts if ARGV.verbose? diff --git a/Library/Homebrew/test/cmd/link_spec.rb b/Library/Homebrew/test/cmd/link_spec.rb index 565de5b537..27ccd0bd5b 100644 --- a/Library/Homebrew/test/cmd/link_spec.rb +++ b/Library/Homebrew/test/cmd/link_spec.rb @@ -37,9 +37,8 @@ describe "brew link", :integration_test do expect { brew "link", "testball1", "SHELL" => "/bin/zsh" } .to output(/testball1 is keg-only/).to_stderr - .and output(a_string_matching(/Note that doing so can interfere with building software\./) - .and(matching("If you need to have this software first in your PATH instead consider running:") - .and(including("echo 'export PATH=\"#{HOMEBREW_PREFIX}/opt/testball1/bin:$PATH\"' >> ~/.zshrc")))).to_stdout + .and output(a_string_matching(/If you need to have this software first in your PATH instead consider running:/) + .and(including("echo 'export PATH=\"#{HOMEBREW_PREFIX}/opt/testball1/bin:$PATH\"' >> ~/.zshrc"))).to_stdout .and be_a_success end end