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

shebangs: fix broken shebangs like #!python #19563

Merged
merged 2 commits into from
Mar 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Library/Homebrew/language/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ module Shebang
module_function

# A regex to match potential shebang permutations.
NODE_SHEBANG_REGEX = %r{^#! ?/usr/bin/(?:env )?node( |$)}
NODE_SHEBANG_REGEX = %r{^#! ?(?:/usr/bin/(?:env )?)?node( |$)}

# The length of the longest shebang matching `SHEBANG_REGEX`.
NODE_SHEBANG_MAX_LENGTH = T.let("#! /usr/bin/env node ".length, Integer)
Expand Down
2 changes: 1 addition & 1 deletion Library/Homebrew/language/perl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module Shebang
module_function

# A regex to match potential shebang permutations.
PERL_SHEBANG_REGEX = %r{^#! ?/usr/bin/(?:env )?perl( |$)}
PERL_SHEBANG_REGEX = %r{^#! ?(?:/usr/bin/(?:env )?)?perl( |$)}

# The length of the longest shebang matching `SHEBANG_REGEX`.
PERL_SHEBANG_MAX_LENGTH = T.let("#! /usr/bin/env perl ".length, Integer)
Expand Down
2 changes: 1 addition & 1 deletion Library/Homebrew/language/python.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ module Shebang
module_function

# A regex to match potential shebang permutations.
PYTHON_SHEBANG_REGEX = %r{^#! ?/usr/bin/(?:env )?python(?:[23](?:\.\d{1,2})?)?( |$)}
PYTHON_SHEBANG_REGEX = %r{^#! ?(?:/usr/bin/(?:env )?)?python(?:[23](?:\.\d{1,2})?)?( |$)}

# The length of the longest shebang matching `SHEBANG_REGEX`.
PYTHON_SHEBANG_MAX_LENGTH = T.let("#! /usr/bin/env pythonx.yyy ".length, Integer)
Expand Down
22 changes: 21 additions & 1 deletion Library/Homebrew/test/language/node/shebang_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

RSpec.describe Language::Node::Shebang do
let(:file) { Tempfile.new("node-shebang") }
let(:broken_file) { Tempfile.new("node-shebang") }
let(:f) do
f = {}

Expand Down Expand Up @@ -40,9 +41,16 @@
c
EOS
file.flush
broken_file.write <<~EOS
#!node
a
b
c
EOS
broken_file.flush
end

after { file.unlink }
after { [file, broken_file].each(&:unlink) }

describe "#detected_node_shebang" do
it "can be used to replace Node shebangs" do
Expand All @@ -57,6 +65,18 @@
EOS
end

it "can fix broken shebang like `#!node`" do
allow(Formulary).to receive(:factory).with(f[:node18].name).and_return(f[:node18])
Utils::Shebang.rewrite_shebang described_class.detected_node_shebang(f[:versioned_node_dep]), broken_file.path

expect(File.read(broken_file)).to eq <<~EOS
#!#{HOMEBREW_PREFIX/"opt/node@18/bin/node"}
a
b
c
EOS
end

it "errors if formula doesn't depend on node" do
expect { Utils::Shebang.rewrite_shebang described_class.detected_node_shebang(f[:no_deps]), file.path }
.to raise_error(ShebangDetectionError, "Cannot detect Node shebang: formula does not depend on Node.")
Expand Down
28 changes: 27 additions & 1 deletion Library/Homebrew/test/language/perl/shebang_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

RSpec.describe Language::Perl::Shebang do
let(:file) { Tempfile.new("perl-shebang") }
let(:broken_file) { Tempfile.new("perl-shebang") }
let(:f) do
f = {}

Expand Down Expand Up @@ -39,9 +40,16 @@
c
EOS
file.flush
broken_file.write <<~EOS
#!perl
a
b
c
EOS
broken_file.flush
end

after { file.unlink }
after { [file, broken_file].each(&:unlink) }

describe "#detected_perl_shebang" do
it "can be used to replace Perl shebangs when depends_on \"perl\" is used" do
Expand Down Expand Up @@ -74,6 +82,24 @@
EOS
end

it "can fix broken shebang like `#!perl`" do
allow(Formulary).to receive(:factory).with(f[:perl].name).and_return(f[:perl])
Utils::Shebang.rewrite_shebang described_class.detected_perl_shebang(f[:uses_from_macos]), broken_file.path

expected_shebang = if OS.mac?
"/usr/bin/perl#{MacOS.preferred_perl_version}"
else
HOMEBREW_PREFIX/"opt/perl/bin/perl"
end

expect(File.read(broken_file)).to eq <<~EOS
#!#{expected_shebang}
a
b
c
EOS
end

it "errors if formula doesn't depend on perl" do
expect { Utils::Shebang.rewrite_shebang described_class.detected_perl_shebang(f[:no_deps]), file.path }
.to raise_error(ShebangDetectionError, "Cannot detect Perl shebang: formula does not depend on Perl.")
Expand Down
24 changes: 23 additions & 1 deletion Library/Homebrew/test/language/python/shebang_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

RSpec.describe Language::Python::Shebang do
let(:file) { Tempfile.new("python-shebang") }
let(:broken_file) { Tempfile.new("python-shebang") }
let(:f) do
f = {}

Expand Down Expand Up @@ -40,9 +41,16 @@
c
EOS
file.flush
broken_file.write <<~EOS
#!python
a
b
c
EOS
broken_file.flush
end

after { file.unlink }
after { [file, broken_file].each(&:unlink) }

describe "#detected_python_shebang" do
it "can be used to replace Python shebangs" do
Expand Down Expand Up @@ -72,6 +80,20 @@
EOS
end

it "can fix broken shebang line `#!python`" do
Utils::Shebang.rewrite_shebang(
described_class.detected_python_shebang(f[:versioned_python_dep],
use_python_from_path: true), broken_file.path
)

expect(File.read(broken_file)).to eq <<~EOS
#!/usr/bin/env python3
a
b
c
EOS
end

it "errors if formula doesn't depend on python" do
expect do
Utils::Shebang.rewrite_shebang(
Expand Down
Loading