diff --git a/CHANGELOG.md b/CHANGELOG.md index 5cca903a..c78ca526 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## Next release + +* Preserve comments right after the shebang line which might include magic comments such as `frozen_string_literal: true' + ## 2.0.2 * Fix reloading when a watched directory contains a dangling symlink (#522) diff --git a/lib/spring/client/binstub.rb b/lib/spring/client/binstub.rb index 59c29dd4..181ced7a 100644 --- a/lib/spring/client/binstub.rb +++ b/lib/spring/client/binstub.rb @@ -3,7 +3,7 @@ module Spring module Client class Binstub < Command - SHEBANG = /\#\!.*\n/ + SHEBANG = /\#\!.*\n(\#.*\n)*/ # If loading the bin/spring file works, it'll run spring which will # eventually call Kernel.exit. This means that in the client process diff --git a/lib/spring/test/acceptance_test.rb b/lib/spring/test/acceptance_test.rb index 53e58223..999f429b 100644 --- a/lib/spring/test/acceptance_test.rb +++ b/lib/spring/test/acceptance_test.rb @@ -260,6 +260,30 @@ def exec_name end end + test "binstub preserve magic comments" do + File.write(app.path("bin/rake"), <<-RUBY.strip_heredoc) + #!/usr/bin/env ruby + # frozen_string_literal: true + # + # more comments + require 'bundler/setup' + load Gem.bin_path('rake', 'rake') + RUBY + + assert_success "bin/spring binstub rake" + + expected = <<-RUBY.gsub(/^ /, "") + #!/usr/bin/env ruby + # frozen_string_literal: true + # + # more comments + #{Spring::Client::Binstub::LOADER.strip} + require 'bundler/setup' + load Gem.bin_path('rake', 'rake') + RUBY + assert_equal expected, app.path("bin/rake").read + end + test "binstub upgrade with old binstub" do File.write(app.path("bin/rake"), <<-RUBY.strip_heredoc) #!/usr/bin/env ruby