From eadd4314fdee47ca1d5cfe88a14f220ba4f59977 Mon Sep 17 00:00:00 2001 From: Vinicius Stock Date: Tue, 29 Oct 2024 12:37:39 -0400 Subject: [PATCH] Ensure absolute lockfile remotes for gems nested inside project (#2799) --- lib/ruby_lsp/setup_bundler.rb | 3 ++- test/setup_bundler_test.rb | 48 +++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/lib/ruby_lsp/setup_bundler.rb b/lib/ruby_lsp/setup_bundler.rb index 11e5a6e47..20237cd06 100644 --- a/lib/ruby_lsp/setup_bundler.rb +++ b/lib/ruby_lsp/setup_bundler.rb @@ -7,6 +7,7 @@ require "pathname" require "digest" require "time" +require "uri" # This file is a script that will configure a custom bundle for the Ruby LSP. The custom bundle allows developers to use # the Ruby LSP without including the gem in their application's Gemfile while at the same time giving us access to the @@ -300,7 +301,7 @@ def correct_relative_remote_paths # We should only apply the correction if the remote is a relative path. It might also be a URI, like # `https://rubygems.org` or an absolute path, in which case we shouldn't do anything - if path&.start_with?(".") + if path && !URI(path).scheme "remote: #{File.expand_path(path, T.must(@gemfile).dirname)}" else match diff --git a/test/setup_bundler_test.rb b/test/setup_bundler_test.rb index 4c51d493a..53e5235ab 100644 --- a/test/setup_bundler_test.rb +++ b/test/setup_bundler_test.rb @@ -395,6 +395,54 @@ def test_ensures_lockfile_remotes_are_relative_to_default_gemfile end end + def test_ensures_lockfile_remotes_are_absolute_in_projects_with_nested_gems + Dir.mktmpdir do |dir| + Dir.chdir(dir) do + File.write(File.join(dir, "Gemfile"), <<~GEMFILE) + # frozen_string_literal: true + source "https://rubygems.org" + gem "nested", path: "gems/nested" + GEMFILE + + FileUtils.mkdir_p(File.join(dir, "gems", "nested", "lib")) + + File.write(File.join(dir, "gems", "nested", "nested.gemspec"), <<~GEMSPEC) + Gem::Specification.new do |s| + s.platform = Gem::Platform::RUBY + s.name = "nested" + s.version = "1.0.0" + s.summary = "Nested gemspec" + s.description = "Nested gemspec" + s.license = "MIT" + s.author = "User" + s.email = "user@example.com" + s.homepage = "https://rubyonrails.org" + s.files = Dir[] + s.require_path = "lib" + end + GEMSPEC + + File.write(File.join(dir, "gems", "nested", "Gemfile"), <<~GEMFILE) + source "https://rubygems.org" + gemspec + GEMFILE + + real_path = File.realpath(dir) + + Bundler.with_unbundled_env do + capture_subprocess_io do + system("bundle install") + run_script(real_path) + end + end + + assert_path_exists(".ruby-lsp") + assert_path_exists(".ruby-lsp/Gemfile.lock") + assert_match("remote: #{File.join(real_path, "gems", "nested")}", File.read(".ruby-lsp/Gemfile.lock")) + end + end + end + def test_ruby_lsp_rails_is_automatically_included_in_rails_apps Dir.mktmpdir do |dir| FileUtils.mkdir("#{dir}/config")