Skip to content

Commit d9c0158

Browse files
author
Andy Waite
authored
Assume UTF-8 when detecting Rails (#2469)
1 parent bec458c commit d9c0158

File tree

4 files changed

+75
-1
lines changed

4 files changed

+75
-1
lines changed

lib/ruby_lsp/setup_bundler.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ def correct_relative_remote_paths
286286
sig { returns(T::Boolean) }
287287
def rails_app?
288288
config = Pathname.new("config/application.rb").expand_path
289-
application_contents = config.read if config.exist?
289+
application_contents = config.read(external_encoding: Encoding::UTF_8) if config.exist?
290290
return false unless application_contents
291291

292292
/class .* < (::)?Rails::Application/.match?(application_contents)

sorbet/rbi/shims/pathname.rbi

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# typed: false
2+
3+
class Pathname
4+
sig do
5+
params(
6+
external_encoding: T.any(String, Encoding),
7+
internal_encoding: T.any(String, Encoding),
8+
encoding: T.any(String, Encoding),
9+
textmode: BasicObject,
10+
binmode: BasicObject,
11+
autoclose: BasicObject,
12+
mode: String,
13+
).returns(String)
14+
end
15+
def read(external_encoding: T.unsafe(nil), internal_encoding: T.unsafe(nil), encoding: T.unsafe(nil), textmode: T.unsafe(nil), binmode: T.unsafe(nil), autoclose: T.unsafe(nil), mode: T.unsafe(nil)); end
16+
end

test/fixtures/rails_application.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
module MyApp
22
class Application < Rails::Application
3+
# あ
34
end
45
end

test/setup_bundler_test.rb

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,39 @@ def test_ruby_lsp_rails_is_automatically_included_in_rails_apps
506506
end
507507
end
508508

509+
def test_ruby_lsp_rails_detection_handles_lang_from_environment
510+
with_default_external_encoding("us-ascii") do
511+
Dir.mktmpdir do |dir|
512+
FileUtils.mkdir("#{dir}/config")
513+
FileUtils.cp("test/fixtures/rails_application.rb", "#{dir}/config/application.rb")
514+
Dir.chdir(dir) do
515+
File.write(File.join(dir, "Gemfile"), <<~GEMFILE)
516+
source "https://rubygems.org"
517+
gem "rails"
518+
GEMFILE
519+
520+
capture_subprocess_io do
521+
Bundler.with_unbundled_env do
522+
# Run bundle install to generate the lockfile
523+
system("bundle install")
524+
end
525+
end
526+
527+
Object.any_instance.expects(:system).with(
528+
bundle_env(dir, ".ruby-lsp/Gemfile"),
529+
"(bundle check || bundle install) 1>&2",
530+
).returns(true)
531+
Bundler.with_unbundled_env do
532+
run_script(dir)
533+
end
534+
535+
assert_path_exists(".ruby-lsp/Gemfile")
536+
assert_match('gem "ruby-lsp-rails"', File.read(".ruby-lsp/Gemfile"))
537+
end
538+
end
539+
end
540+
end
541+
509542
def test_recovers_from_stale_lockfiles
510543
Dir.mktmpdir do |dir|
511544
custom_dir = File.join(dir, ".ruby-lsp")
@@ -575,6 +608,30 @@ def test_recovers_from_stale_lockfiles
575608

576609
private
577610

611+
def with_default_external_encoding(encoding, &block)
612+
ignore_warnings do
613+
original_encoding = Encoding.default_external
614+
begin
615+
Encoding.default_external = encoding
616+
block.call
617+
ensure
618+
Encoding.default_external = original_encoding
619+
end
620+
end
621+
end
622+
623+
def ignore_warnings(&block)
624+
# Since overwriting the encoding emits a warning
625+
previous = $VERBOSE
626+
$VERBOSE = nil
627+
628+
begin
629+
block.call
630+
ensure
631+
$VERBOSE = previous
632+
end
633+
end
634+
578635
# This method runs the script and then immediately unloads it. This allows us to make assertions against the effects
579636
# of running the script multiple times
580637
def run_script(path = Dir.pwd, expected_path: nil, **options)

0 commit comments

Comments
 (0)