bazelruby / rules_ruby

Formerly canonical rules for ruby, that are about 2-3 years behind current Bazel. If they work for you great, but if not — please try the new rules ruby by Alex Radionov: https://github.com/bazel-contrib/rules_ruby
Apache License 2.0
99 stars 37 forks source link

Support Git source in Gemfile #124

Closed matsubara0507 closed 1 year ago

matsubara0507 commented 2 years ago

When use Git source in Gemfile:

gem 'rspec-its', github: "rspec/rspec-its", ref: "3d36b4a7b004ffa204a0e392f27c8b6b0b674ecf" # v1.3.0

Occur error

$ cd examples/simple_script
$ bazel test //...
...
INFO: Repository bundle instantiated at:
  /work/examples/simple_script/WORKSPACE:22:12: in <toplevel>
Repository rule ruby_bundle_install defined at:
  /root/.cache/bazel/_bazel_root/7f81b7228d5513bd8ae9460589a2de46/external/bazelruby_rules_ruby/ruby/private/bundle/def.bzl:200:38: in <toplevel>
ERROR: An error occurred during the fetch of repository 'bundle':
   Traceback (most recent call last):
    File "/root/.cache/bazel/_bazel_root/7f81b7228d5513bd8ae9460589a2de46/external/bazelruby_rules_ruby/ruby/private/bundle/def.bzl", line 198, column 31, in _ruby_bundle_impl
        generate_bundle_build_file(runtime_ctx, result)
    File "/root/.cache/bazel/_bazel_root/7f81b7228d5513bd8ae9460589a2de46/external/bazelruby_rules_ruby/ruby/private/bundle/def.bzl", line 163, column 13, in generate_bundle_build_file
        fail("build file generation failed: %s%s" % (result.stdout, result.stderr))
Error in fail: build file generation failed: registering gem diff-lcs with binaries: ["bin/htmldiff", "bin/ldiff"]
registering gem parser with binaries: ["bin/ruby-parse", "bin/ruby-rewrite"]
registering gem rspec-core with binaries: ["bin/rspec"]
/root/.cache/bazel/_bazel_root/7f81b7228d5513bd8ae9460589a2de46/external/org_ruby_lang_ruby_toolchain/build/lib/ruby/3.0.0/rubygems/stub_specification.rb:113:in `initialize': no implicit conversion of nil into String (TypeError)
    from /root/.cache/bazel/_bazel_root/7f81b7228d5513bd8ae9460589a2de46/external/org_ruby_lang_ruby_toolchain/build/lib/ruby/3.0.0/rubygems/stub_specification.rb:113:in `open'
    from /root/.cache/bazel/_bazel_root/7f81b7228d5513bd8ae9460589a2de46/external/org_ruby_lang_ruby_toolchain/build/lib/ruby/3.0.0/rubygems/stub_specification.rb:113:in `data'
    from /root/.cache/bazel/_bazel_root/7f81b7228d5513bd8ae9460589a2de46/external/org_ruby_lang_ruby_toolchain/build/lib/ruby/3.0.0/rubygems/stub_specification.rb:169:in `extensions'
    from /root/.cache/bazel/_bazel_root/7f81b7228d5513bd8ae9460589a2de46/external/org_ruby_lang_ruby_toolchain/build/lib/ruby/3.0.0/rubygems/basic_specification.rb:330:in `have_extensions?'
    from /root/.cache/bazel/_bazel_root/7f81b7228d5513bd8ae9460589a2de46/external/org_ruby_lang_ruby_toolchain/build/lib/ruby/3.0.0/rubygems/basic_specification.rb:247:in `require_paths'
    from create_bundle_build_file.rb:260:in `register_gem'
    from create_bundle_build_file.rb:215:in `block in generate!'
    from create_bundle_build_file.rb:215:in `each'
    from create_bundle_build_file.rb:215:in `generate!'
    from create_bundle_build_file.rb:332:in `<main>'
ERROR: /work/examples/simple_script/WORKSPACE:22:12: fetching ruby_bundle_install rule //external:bundle: Traceback (most recent call last):
    File "/root/.cache/bazel/_bazel_root/7f81b7228d5513bd8ae9460589a2de46/external/bazelruby_rules_ruby/ruby/private/bundle/def.bzl", line 198, column 31, in _ruby_bundle_impl
        generate_bundle_build_file(runtime_ctx, result)
    File "/root/.cache/bazel/_bazel_root/7f81b7228d5513bd8ae9460589a2de46/external/bazelruby_rules_ruby/ruby/private/bundle/def.bzl", line 163, column 13, in generate_bundle_build_file
        fail("build file generation failed: %s%s" % (result.stdout, result.stderr))
Error in fail: build file generation failed: registering gem diff-lcs with binaries: ["bin/htmldiff", "bin/ldiff"]
...

Reason is gem's path is wrong to create BUILD file for git source gem:

GEM_PATH = ->(ruby_version, gem_name, gem_version) do
  Dir.glob("lib/#{RbConfig::CONFIG['RUBY_INSTALL_NAME']}/#{ruby_version}/gems/#{gem_name}-#{gem_version}*").first
end

SPEC_PATH = ->(ruby_version, gem_name, gem_version) do
  Dir.glob("lib/#{RbConfig::CONFIG['RUBY_INSTALL_NAME']}/#{ruby_version}/specifications/#{gem_name}-#{gem_version}*.gemspec").first
end

...

    gem_path = GEM_PATH[ruby_version, spec.name, spec.version]
    spec_path = SPEC_PATH[ruby_version, spec.name, spec.version]

So, build gem_path and spec_path using StubSpecification object of spec variable.

kigster commented 2 years ago

Thank you for this contribution!

I have inadvertently broke the bashmatic dependency which is why CI is broken right now. Let me fix that first, and once this PR goes green I'll merge it.