Code Monkey home page Code Monkey logo

bazelruby / rules_ruby Goto Github PK

View Code? Open in Web Editor NEW
99.0 4.0 37.0 4.13 MB

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

License: Apache License 2.0

Shell 30.02% Ruby 18.25% C 0.18% Dockerfile 3.74% Starlark 45.93% Smarty 0.24% Makefile 1.64%
bazel rules-ruby build-systems monorepo bazel-examples ruby rails deployment

rules_ruby's People

Contributors

adzenith avatar cfeckardt avatar clarecat avatar dbaratta avatar dependabot[bot] avatar jasonlunn avatar justusft avatar kigster avatar lalten avatar matsubara0507 avatar mmizutani avatar morriar avatar p0deje avatar russell avatar sayrer avatar tsawada avatar yugui avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

rules_ruby's Issues

State of this project

Hello,

I would like to inquire about the state of this project. Last release was in the fall of last year, and even current master does not seem to support ruby 3.1.2 released in April of this year. Please don't take this in a pushy way, I'm just curious before I import this into our workspace.

Thanks

Ability to run ruby_bundle without gemfile_lock attribute

Currently when running a ruby_bundle without the gemfile_lock attribute results in the following error

ERROR: An error occurred during the fetch of repository 'vagrant_bundle':
   Traceback (most recent call last):
	File "/private/var/tmp/_bazel_maarten/8c4bfcd606c4d7b95cf418885251b08c/external/bazelruby_rules_ruby/ruby/private/bundle/def.bzl", line 198, column 28, in _ruby_bundle_impl
		result = bundle_install(runtime_ctx, result)
	File "/private/var/tmp/_bazel_maarten/8c4bfcd606c4d7b95cf418885251b08c/external/bazelruby_rules_ruby/ruby/private/bundle/def.bzl", line 138, column 13, in bundle_install
		fail("bundle install failed: %s%s" % (result.stdout, result.stderr))
Error in fail: bundle install failed: The deployment setting requires a Gemfile.lock. Please make sure you have
checked your Gemfile.lock into version control before deploying

The documentation (https://github.com/bazelruby/rules_ruby#54-ruby_bundle) suggests that it's possible to run ruby_bundle without passing the gemfile_lock attribute. This is backed-up by the conditional in the code which checks for the presence of the gemfile_lock attribute https://github.com/bazelruby/rules_ruby/blob/master/ruby/private/bundle/def.bzl#L125-L126.

The problem is that the set_bundler_config hardcodes the deployment and frozen settings to true (https://github.com/bazelruby/rules_ruby/blob/master/ruby/private/bundle/def.bzl#L56-L58) which enforces that any bundle install command requires a Gemfile.lock file.

Disabling these settings as follows seems to help in the case of no Gemfile.lock:

    bundler_config = {
        # "deployment": "false",
        "standalone": "true",
        "force": "false",
        "redownload": "false",
        # "frozen": "true",
        "path": BUNDLE_PATH,
        "jobs": "20",
        "shebang": runtime_ctx.interpreter,
    }

`ruby_library` not using specified SDK version

We are testing out the flow where the desired version of ruby is not installed on the host.

I expect for rules_ruby to be able to install and use v2.7.6. (Note this first requires bumping the ruby-build version, which I created a PR here).

With the version specified in WORKSPACE:

load("@bazelruby_rules_ruby//ruby:deps.bzl", "rules_ruby_select_sdk")
rules_ruby_select_sdk(version = "2.7.6")

I can confirm that ruby interpreter that's installed to the @org_ruby_lang_ruby_toolchain external repository is the right version:

➜  org_ruby_lang_ruby_toolchain build/bin/ruby -v
ruby 2.7.6p219 (2022-04-12 revision c9c2245c0a) [arm64-darwin21]

However, running any ruby_library target i get this error on my macos:

/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in 'require': incompatible library version...

I think this requires updating the way the binary wrapper is implemented, which defaults to the first ruby found in PATH today rather than the version that's specified in the SDK:

#!/usr/bin/env ruby

...

def find_ruby_binary
  File.join(
    RbConfig::CONFIG['bindir'],
    RbConfig::CONFIG['ruby_install_name'],
  )
end

In fact, it looks like there was an intention to substitute {interpreter} here but the wrapper script template itself does not have a {interpreter} placeholder anywhere, which looks like a bug.

Use `genrule` output directory as `srcs` for `ruby_library`

When using a genrule to generate ruby sources, it's helpful to output a directory and then use that for the sources of a ruby_library

For example:

genrule(
  name = "gen-src-dir",
  outs = ["some-directory"],
  cmd = "mkdir -p $@ && touch $@/foo.rb $@/bar.rb"
)

ruby_library(
  name = "my-lib",
  srcs = [":gen-src-dir"],
)

Note that because these are generated files, it's hard to know what prefix to place in the include attribute, so ideally, that would allow itself to be relative to the output directory the genrule created.

ruby_test and ruby_rspec rules use system ruby

When running on my local Mac (Mojave 10.14), trying to run any ruby_test or ruby_rspec
rules crashes with the following error:

/private/var/tmp/_bazel_kevinmiller/f7932d341642f6d347f4cead95172a70/execroot/__main__/bazel-out/darwin-fastbuild/bin/ruby_utils/test_rspec.runfiles/__main__/ruby_utils/test_rspec:47:in `get_repository_imports': undefined method `children' for Dir:Class (NoMethodError)
	from /private/var/tmp/_bazel_kevinmiller/f7932d341642f6d347f4cead95172a70/execroot/__main__/bazel-out/darwin-fastbuild/bin/ruby_utils/test_rspec.runfiles/__main__/ruby_utils/test_rspec:99:in `main'
	from /private/var/tmp/_bazel_kevinmiller/f7932d341642f6d347f4cead95172a70/execroot/__main__/bazel-out/darwin-fastbuild/bin/ruby_utils/test_rspec.runfiles/__main__/ruby_utils/test_rspec:125:in `<main>'

My guess is that the setup is using my system ruby (2.3.5) which doesn't support Dir.children

WORKSPACE:

...
load(
    "@bazel_tools//tools/build_defs/repo:git.bzl",
    "git_repository",
)

git_repository(
    name = "bazelruby_ruby_rules",
    commit = "fafab7ebb658857e150b80c39155b75b7f67fde2", # Head as of Feb 4, 2020
    remote = "https://github.com/bazelruby/rules_ruby.git",
)

load(
    "@bazelruby_ruby_rules//ruby:deps.bzl",
    "ruby_register_toolchains",
    "ruby_rules_dependencies",
)

ruby_rules_dependencies()

ruby_register_toolchains(version = "2.6.5")

load("@bazelruby_ruby_rules//ruby:defs.bzl", "ruby_bundle")

ruby_bundle(
    name = "bundle",
    gemfile = "//:BazelGemfile",
    gemfile_lock = "//:BazelGemfile.lock",
    bundler_version = "2.0.2",
    visibility = ["//visibility:public"],
)

BUILD:

load(
    "@bazelruby_ruby_rules//ruby:defs.bzl",
    "ruby_binary",
    "ruby_library",
    "ruby_rspec",
    "ruby_test",
)

filegroup(
    name = "sources",
    srcs = glob([
        "lib/**/*.rb",
    ]),
)

filegroup(
    name = "specs",
    srcs = glob([
        "spec/**/*.rb",
    ]),
)

ruby_library(
    name = "lib",
    srcs = [":sources"],
    includes = ["lib"],
    deps = [
        "@bundle//:sorbet-runtime",
    ],
)

ruby_test(
  name = "test_rspec",
  main = "example_spec.rb",
  srcs = [
    ":specs",
  ],
  deps = [
    ":lib",
  ]
)

gem from the .zip archive?

Thanks for the effort In building this project. I added the ruby_gem rule and it generates a .zip file in the cache directory. Is there a way to create a regular .gem file? Not sure how to use the .zip file even though it is gem compatible? Thanks

Support pulling prebuilt ruby

This currently supports building ruby from source and using ruby from host. Building ruby from source can be slow and has to be redone every time we upgrade our clang compiler. ruby coming from the host machine is outdated and is often incompatible with newer gems; it also creates a room for non-hermetic builds. It would be nice if this can have support for pulling prebuilt ruby binaries based on the host platform.

private method `define_method' called for Dir:Class (NoMethodError)

Hi, I'm trying to use CMock's cmock.rb with rules_ruby.

I have the WORKSPACE set up as per the rules_ruby readme instructions.

cmock.rb is visible as target autogen

ruby_binary(
    name = "autogen",
    main = "lib/cmock.rb",
    srcs = glob(["**/*"]),
)

I'm calling this autogen target in a custom Bazel rule that basically does this:

...
    autogen_exe = ctx.attr.cmock_autogen.files.to_list()[0]
    ctx.actions.run(
        inputs = ctx.files.headers_to_be_mocked + ctx.files.cmock_config,
        outputs = generated_files,
        arguments = ["-o", ctx.file.cmock_config.path] + [file.path for file in ctx.files.headers_to_be_mocked],
        executable = autogen_exe,
    )
...

But there seem to be errors in rules_ruby's wrapper script

bazel-out/k8-opt/bin/external/CMock/autogen:121: warning: string literal in condition
bazel-out/k8-opt/bin/external/CMock/autogen:122: warning: string literal in condition
bazel-out/k8-opt/bin/external/CMock/autogen:26:in `<main>': private method `define_method' called for Dir:Class (NoMethodError)
Did you mean?  define_singleton_method
Target //(redacted) failed to build

I tried with ruby 2.3 (host ruby) and 2.7.0.

I also tried to use the commit before 5a81b11 (and set up the WORKSPACE as explained in that version's readme) but it has other problems:

bazel-out/k8-opt/bin/external/CMock/autogen:113: warning: string literal in condition
bazel-out/k8-opt/bin/external/CMock/autogen:114: warning: string literal in condition
bazel-out/k8-opt/bin/external/CMock/autogen:39:in `find_runfiles': Cannot find .runfiles directory for bazel-out/k8-opt/bin/external/CMock/autogen (RuntimeError)
        from bazel-out/k8-opt/bin/external/CMock/autogen:103:in `main'
        from bazel-out/k8-opt/bin/external/CMock/autogen:146:in `<main>'
Target //(redacted) failed to build

Am I doing something wrong? Any ideas how to fix this?

Generated BUILD.bazel files should use `allow_empty`

Some Gems result in an empty glob inside the generated BUILD files, which causes Bazel to fail. Generated globs which may be empty should probably pass allow_empty = True to the glob() function. This is notably a problem for nokogiri.

ruby_binary - how to package up runfiles dir

I'm trying to use the rules to package our rails app into a container. I'm using pkg_tar to collect the output from ruby_binary, allowing the folder structure to be maintained:

ruby_binary(
    name = "server",
    srcs = glob(
        include = [
            "app/**/*",
            "config/**/*",
            "bin/*",
            "config.ru",
        ],
    ),
    main = ":bin/rails",
    deps = [
        "@bundle//:rails",
    ],
)

pkg_tar(
    name = "server_tar",
    strip_prefix = ".",
    package_dir = "/rails",
    srcs = [
        ":server",
    ],
    mode = "0755",
)

However, the tar only contains a single file - the binary runner. The runfiles are not exported and thus not tarred up.

What's the right way to collect these files?

First class support for Ruby RSpec gem with Bazel

One of the core tenets of successful ruby projects is a solid test suite. RSpec has become a de-facto standard, and makes a lot of assumptions about file layout, $LOAD_PATH, etc.

This ticket is to do the following:

  • Analyze, learn and understand the current approach
  • Identify shortcomings (if any)
  • Consider creating a dedicated test rule ruby_rspec
  • Create a basic BUILD template file Ruby Projects that use RSpecs for tests, and in particular ensure that specs for the deeply nested project files can resolve require

Rule ruby_rspec

This rule should be able to handle a deeply nested spec tree like the one shown below from the Ruby Gem Sym:

spec
├── spec/fixtures
│   ├── spec/fixtures/hamlet.enc
│   ├── spec/fixtures/hamlet.txt
│   ├── spec/fixtures/secrets.key
│   ├── spec/fixtures/secrets.yml
│   └── spec/fixtures/secrets.yml.enc
├── spec/integration
│   └── spec/integration/sym_cli_spec.rb
├── spec/spec_helper.rb
├── spec/support
│   ├── spec/support/aruba.rb
│   ├── spec/support/contexts.rb
│   ├── spec/support/fake_terminal.rb
│   └── spec/support/shared_examples.rb
└── spec/sym
    ├── spec/sym/app
    │   ├── spec/sym/app/args_spec.rb
    │   ├── spec/sym/app/cli_spec.rb
    │   ├── spec/sym/app/commands
    │   │   ├── spec/sym/app/commands/encrypt_decrypt_spec.rb
    │   │   ├── spec/sym/app/commands/generate_key_spec.rb
    │   │   ├── spec/sym/app/commands/open_editor_spec.rb
    │   │   └── spec/sym/app/commands/print_key_spec.rb
    │   ├── spec/sym/app/commands_spec.rb
    │   ├── spec/sym/app/input
    │   │   └── spec/sym/app/input/handler_spec.rb
    │   ├── spec/sym/app/keychain_spec.rb
    │   ├── spec/sym/app/password
    │   │   ├── spec/sym/app/password/cache_spec.rb
    │   │   └── spec/sym/app/password/providers_spec.rb
    │   └── spec/sym/app/private_key
    │       ├── spec/sym/app/private_key/detector_spec.rb
    │       └── spec/sym/app/private_key/handler_spec.rb
    ├── spec/sym/application_spec.rb
    ├── spec/sym/data_spec.rb
    ├── spec/sym/extensions
    │   └── spec/sym/extensions/instance_methods_spec.rb
    ├── spec/sym/magic_file_spec.rb
    └── spec/sym/sym_spec.rb

Clarification - using generated ruby_binary in container image

Hey - I'm a bazel noob, so apologies for the question, but I'm trying to produce a container image with my rails project using these rules and I'm stuck.

I have a ruby_binary package in my BUILD (largely cribbed from the examples/simple_rails_api dir) which builds successfully. But I'm then coming up dry getting that into a runnable container. I've looked at py_binary for inspiration, but there seems to be a py_image (https://github.com/bazelbuild/rules_docker#py_image) rules_docker rule that does the work for that. How can I go about doing it with container_layer and container_image rules? Is the idea that the ruby_binary produces a binary that requires no other deps?

Want to understand better why native extensions are failing on CircleCI

I managed to get CircleCI build passing for the https://github.com/bazelruby/ruby-monorepo repository, but only after I removed rubocop and other gems that depended on gems needing native extensions. I am able to install rubocop and run it on CI via a BASH script, but not via Bazel.

Here is one example CI build that failed installing a gem with native extension:
https://app.circleci.com/jobs/github/bazelruby/ruby-monorepo/40

The actual error is this:

ake "DESTDIR="
compiling adj_matrix.c
gcc: error trying to exec 'cc1': execvp: No such file or directory
make: *** [Makefile:245: adj_matrix.o] Error 1

bin/setup Failure

ahillman@MacBook-Air rules_ruby % ./bin/setup
Password:

┌───────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│  Installing required development dependencies for working with rules_ruby and Bazel.                          │
└───────────────────────────────────────────────────────────────────────────────────────────────────────────────┘


┌───────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│    « WARNING »   — You are on a local system without any RBENV...                                             │
└───────────────────────────────────────────────────────────────────────────────────────────────────────────────┘


       Are you sure you want to wipe your local ~/.rbenv? [Y/n] Y

       Roger that.
       Let's just hope it won't go nuclear on us :) 💥
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────\e[0m

┌───────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│    « WARNING »   — Recreating RBENV home, downloading rbenv...                                                │
└───────────────────────────────────────────────────────────────────────────────────────────────────────────────┘

  ✔︎    ❯ rm -rf /Users/ahillman/.rbenv ▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪〔      9 ms 〕    0 
  ✔︎    ❯ git clone https://github.com/rbenv/rbenv.git /Users/ahillman/.rbenv ▪▪▪▪▪▪▪▪▪〔   1417 ms 〕    0 
  ✔︎    ❯ rbenv global 2.7.1 || true ▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪〔    883 ms 〕    0 
rbenv: version `2.7.1' is not installed (set by /Users/ahillman/Development/rules_ruby/.ruby-version)
  ✔︎    ❯ mkdir -p /Users/ahillman/.rbenv/plugins ▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪〔      5 ms 〕    0 
  ✔︎    ❯ git clone https://github.com/rbenv/ruby-build.git /Users/ahillman/.rbe ▪▪▪▪▪▪〔   1592 ms 〕    0 
  ✔︎    ❯ rbenv rehash ▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪〔    175 ms 〕    0 
       ❯ rbenv install -s 2.7.1 
  ✘  ▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪〔 209777 ms 〕    1 


    STDERR                                                                                                     

Downloading openssl-1.1.1k.tar.gz...
-> https://dqw8nmjcqpjn7.cloudfront.net/892a0875b9872acd04a9fde79b1f943075d5ea162415de3047c327df33fbaee5
Installing openssl-1.1.1k...
Installed openssl-1.1.1k to /Users/ahillman/.rbenv/versions/2.7.1

Downloading ruby-2.7.1.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.7/ruby-2.7.1.tar.bz2
Installing ruby-2.7.1...

BUILD FAILED (macOS 11.2.2 using ruby-build 20210405-6-g6573f0a)

Inspect or clean up the working tree at /var/folders/gx/y4w50svj4_7gycfy_412q5zc0000gn/T/ruby-build.20210417134813.96611.ykQotX
Results logged to /var/folders/gx/y4w50svj4_7gycfy_412q5zc0000gn/T/ruby-build.20210417134813.96611.log

Last 10 log lines:
compiling psych_parser.c
compiling psych_to_ruby.c
linking shared-object -test-/string.bundle
compiling psych_yaml_tree.c
compiling ../.././ext/psych/yaml/reader.c
compiling ../.././ext/psych/yaml/scanner.c
compiling ../.././ext/psych/yaml/writer.c
linking shared-object zlib.bundle
linking shared-object psych.bundle
make: *** [build-ext] Error 2
\e[0m
┌───────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│    « ERROR »   — Aborting, due to 'abort on error' being set to true.                                         │
└───────────────────────────────────────────────────────────────────────────────────────────────────────────────┘

       Failed command: rbenv install -s 2.7.1



    STDOUT                                                                                                     

ruby-build: using readline from homebrew
\e[0m

    STDERR                                                                                                     

Downloading openssl-1.1.1k.tar.gz...
-> https://dqw8nmjcqpjn7.cloudfront.net/892a0875b9872acd04a9fde79b1f943075d5ea162415de3047c327df33fbaee5
Installing openssl-1.1.1k...
Installed openssl-1.1.1k to /Users/ahillman/.rbenv/versions/2.7.1

Downloading ruby-2.7.1.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.7/ruby-2.7.1.tar.bz2
Installing ruby-2.7.1...

BUILD FAILED (macOS 11.2.2 using ruby-build 20210405-6-g6573f0a)

Inspect or clean up the working tree at /var/folders/gx/y4w50svj4_7gycfy_412q5zc0000gn/T/ruby-build.20210417134813.96611.ykQotX
Results logged to /var/folders/gx/y4w50svj4_7gycfy_412q5zc0000gn/T/ruby-build.20210417134813.96611.log

Last 10 log lines:
compiling psych_parser.c
compiling psych_to_ruby.c
linking shared-object -test-/string.bundle
compiling psych_yaml_tree.c
compiling ../.././ext/psych/yaml/reader.c
compiling ../.././ext/psych/yaml/scanner.c
compiling ../.././ext/psych/yaml/writer.c
linking shared-object zlib.bundle
linking shared-object psych.bundle
make: *** [build-ext] Error 2
\e[0m

When using a local ruby installation it includes the local `LOAD_PATH`

The problem is that if the ruby interpreter uses the ruby already installed on the host, and that host is rbenv, rbenv will inject into its own LOAD_PATH which includes its gems folders.

This means that ruby_binary is not hermetic as it may inadvertently depend on gems that are not explicitly declared in the rule.

Not sure how to fix this, maybe use a better script to use the local ruby version, and try work around rbenvs magic (or use rbenvs magic to find the real interpreter path without LOAD_PATH injection)

`warning: string literal in condition` from generated wrapper

The generated wrapper from a ruby_binary includes the lines:

ENV["GEM_PATH"] = File.join(runfiles, "{bundle_name}/lib/ruby/2.7.0") if "{bundle_name}/lib/ruby/2.7.0"
ENV["GEM_HOME"] = File.join(runfiles, "{bundle_name}/lib/ruby/2.7.0") if "{bundle_name}/lib/ruby/2.7.0"

which causes Ruby to emit a warning about the string literal of the path.

Unable to find Rakefile when running rake ruby_binary

I'm trying to precompile my assets by creating a ruby_binary that runs rake assets:precompile.

I intend to then tar up the compiled assets and have the genrule output these (rather than test.txt!)

My BUILD goes like this:

ruby_library(
    name = "assets_precompile_lib",
    srcs = glob(
        include = [
            "app/**/*",
            "config/**/*",
            "lib/**/*",
            "vendor/assets/**/*",
        ],
    ) + [
        "Rakefile"
    ],
    deps = [
        "@bundle//:gems",
    ],
)

ruby_binary(
    name = "assets_precompile_bin",
    srcs = [":assets_precompile_lib"],
    args = ["assets:precompile"],
    includes = [
        "src/webapp/Rakefile",
    ],
    main = "@bundle//:bin/rake",
    deps = ["@bundle//:gems"],
)

genrule(
    name = "test",
    srcs = ["Rakefile"],
    outs = ["test.txt"],
    cmd = """
        $(location :assets_precompile_bin)
        echo "test" > $@
    """,
    local = 1,
    message = "test",
    tools = [
        ":assets_precompile_bin",
    ],
    visibility = ["//visibility:public"],
)

But when I run the genrule I get:

➜  bazel build //src/webapp:test
INFO: Analyzed target //src/webapp:test (1 packages loaded, 1740 targets configured).
INFO: Found 1 target...
ERROR: /Users/robert.gates/Repositories/cube/src/webapp/BUILD:698:8: test //src/webapp:test failed: (Exit 1): bash failed: error executing command /bin/bash -c ... (remaining 1 argument skipped)
rake aborted!
No Rakefile found (looking for: rakefile, Rakefile, rakefile.rb, Rakefile.rb)
/private/var/tmp/_bazel_robert.gates/dc10ae55cdaa28397da3989c20f911cb/external/bundle/lib/ruby/2.7.0/gems/rake-13.0.6/exe/rake:27:in `<top (required)>'
(See full trace by running task with --trace)
Target //src/webapp:test failed to build
Use --verbose_failures to see the command lines of failed build steps.

Could anyone help me understand what's wrong here? I've included the Rakefile in the lib, and also tried getting it onto the LOAD_PATH with includes = but no luck. Any advice really appreciated!

`ruby_bundle` should support Gemfiles with `gemspec` method

Currently bundle is created based on the Gemfile and Gemfile.lock only.

However, many custom gems use gemspec inside the Gemfile to delegate gem listing to the *.gemspec file.

bundle install works just the same, except it needs to have the gem's gemspec file present and at the same level as the Gemfile.

Ruby Gem isn't including generated files

Hi all,
I'm not especially familiar with bazel, so this may be more of a user error, but I was hoping I could get some help.
I am generating a ruby proto library (with grpc) using

load("@build_stack_rules_proto//ruby:ruby_proto_library.bzl", "ruby_proto_library")
ruby_proto_library(
    name = "rb_proto",
    has_services = True,
    deps = [
        ":v1",
    ],
    verbose = 3,
)

I then am trying to package the generated rb files into a ruby_gem. I can see both the proto generated rb files and the zip file from ruby_gem in my bazel-out, but my rb files aren't in my ruby_gem zip file when I unzip it. I only find the generated README and gemspec file.

Here's how I create my ruby_gem:

ruby_gem(
    name = "rb_gem",
    srcs = [":rb_proto"],
    gem_author_emails = [
        "my email here"
    ],
    gem_name = "my_proto_library",
    gem_version = "0.3.0"
)

What am I missing here? How do I ensure that my proto library generated ruby files show up in ruby_gem zip file?
Thanks!

google-protobuf and grpc gems fail

When trying to use google-protobuf and grpc gems, the paths created do not match the globs in the generated BUILD.bazel file.

For example, here's the library rule generated for grpc:

ruby_library(
  name = "grpc",
  srcs = glob(
    include = [
      ".bundle/config",
      "lib/ruby/2.7.0/gems/grpc-1.35.0/lib/**/*",
      "lib/ruby/2.7.0/specifications/grpc-1.35.0.gemspec",
      
    ],
    exclude = ["**/* *.*", "**/* */*"],
  ),
  deps = [":google-protobuf", ":googleapis-common-protos-types"],
  includes = ["lib/ruby/2.7.0/gems/grpc-1.35.0/lib"],
)

The glob in the srcs attr does not match any of the files, as both the gem name and contents are possibly incorrect.
In grpc's case, the path is lib/ruby/2.7.0/gems/grpc-1.35.0-x86_64-linux/... and does not contain a 'lib' directory.
In google-protobuf's case, the path is lib/ruby/2.7.0/gems/google-protobuf-3.14.0-x86_64-linux/... and does contain a 'lib' directory.

Therefore, when trying to require 'grpc' it cannot find any files:

/usr/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:92:in `require': cannot load such file -- grpc (LoadError)

Context: rules-proto-grpc/rules_proto_grpc#65

ATTENTION: YOUR OPINION MATTERS S! real world ruby/bazel testers are needed

We are trying to decide on which rules should be the primary or canonical rules.

  1. These current rules depend on a very old version of Bazel, fail on CI, but they have been designed with a typical Ruby/rails flow in mind. That's why we have rspec rules and rails example.

  2. The alternative rules by Alex Radionov are much more modern, pass CI across multiple Ruby implementations, provide hermetic build of Ruby, but might be impractical for a large rails application.

I am looking for users of any Ruby rules to play with both and provide some feedback. If Alex's rules become the default, is there anything in this repo that we might want to carry over?

Unable to run `bazel build //...:all`

On a fresh checkout, getting this error:

Starting local Bazel server and connecting to it...
INFO: Writing tracer profile to '/private/var/tmp/_bazel_kig/33b68b0201a3a257916c02f76c289f12/command.profile.gz'
ERROR: /Users/kig/Coinbase/bazel/bazelruby/rules_ruby/examples/spec/foo/BUILD.bazel:6:1: no such package 'lib': BUILD file not found in any of the following directories. Add a BUILD file to a directory to mark it as a package.
 - /Users/kig/Coinbase/bazel/bazelruby/rules_ruby/lib and referenced by '//examples/spec/foo:version_test'
ERROR: Analysis of target '//examples/spec/foo:version_test' failed; build aborted: no such package 'lib': BUILD file not found in any of the following directories. Add a BUILD file to a directory to mark it as a package.
 - /Users/kig/Coinbase/bazel/bazelruby/rules_ruby/lib
INFO: Elapsed time: 4.097s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (33 packages loaded, 78 targets configured)
    Fetching @local_config_cc; Restarting.
    Fetching @com_github_yugui_rules_ruby_ruby_tests_testdata_another_workspace; Restarting.

Importing a bundle gem adds all gems onto the load path (although they are not there)

the $LOAD_PATH is the path ruby looks for files to require. the binary_wrapper.tpl adds to the load path using the environment variable RUBYLIB.

With recent updates we have allowed a ruby_library or ruby_binary to specify specifically the gem it requires to be included. This works and only adds the files for that gem to the runfiles of the binary.

However, the LOAD_PATH for all gems are still added.

You can check this by creating a ruby file that just puts $LOAD_PATH and executing it adding only 1 gem, and you will see a path to all gems there. These paths are invalid because the gems files are not included, but since ruby looks through each folder for each require this will make loading files very slow.

Support AWS Ruby Lambda with Bazel Rules

According to this guide on packaging Ruby Lambda on AWS, dependencies must be installed invendor/bundle folder using bundle install --path vendor/bundle command.

The Ruby runtime provided is Ruby version 2.5.

In order to be able to support this, we must have a way to grab gem dependencies from ruby_bundle and attach gem folders to vendor/bundle and feed it to pkg_zip rule, together with any source files and the configuration YAML.

Right now we have no easy way to extract gem folders from the external bundle. We also don't know if the gems with native extensions built on the target host will work on AWS against Ruby binary possibly located in another place on the file system. This is important because native extensions often hard-code link paths to the ruby interpreter as can be seen from the following output:

Taken from OS-X:

 1 ❯ otool -L jaro_winkler_ext.bundle
jaro_winkler_ext.bundle:
	/Users/kig/.rbenv/versions/2.6.5/lib/libruby.2.6.dylib (compatibility version 2.6.0, current version 2.6.5)
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1281.0.0)

Add support for bzlmod

Add support for bzlmod:

  • Add Bazel module support
  • Add rules_ruby to Central Bazel Registry

"warning: already initialized constant" with rack gem

Minimal reproduction: repro.zip

If you run bazel run :main in this workspace, you will get output which includes the following:

INFO: Build options --cxxopt, --incompatible_strict_action_env, and --legacy_external_runfiles have changed, discarding analysis cache.
DEBUG: /private/var/tmp/_bazel_johnfirebaugh/1c33c885364b1c1481b2fccb2257d6ac/external/bazelruby_rules_ruby/ruby/private/toolchains/ruby_runtime.bzl:99:14: Found local Ruby SDK version '2.6.6' which matches requested version '2.6.6'
INFO: Analyzed target //:main (0 packages loaded, 283 targets configured).
INFO: Found 1 target...
Target //:main up-to-date:
  bazel-bin/main
INFO: Elapsed time: 0.244s, Critical Path: 0.03s
INFO: 3 processes: 3 internal.
INFO: Build completed successfully, 3 total actions
INFO: Build completed successfully, 3 total actions
/private/var/tmp/_bazel_johnfirebaugh/1c33c885364b1c1481b2fccb2257d6ac/external/bundle/lib/ruby/2.6.0/gems/rack-2.2.3/lib/rack.rb:17: warning: already initialized constant Rack::HTTP_HOST
/private/var/tmp/_bazel_johnfirebaugh/1c33c885364b1c1481b2fccb2257d6ac/execroot/__main__/bazel-out/darwin-fastbuild/bin/main.runfiles/bundle/lib/ruby/2.6.0/gems/rack-2.2.3/lib/rack.rb:17: warning: previous definition of HTTP_HOST was here
/private/var/tmp/_bazel_johnfirebaugh/1c33c885364b1c1481b2fccb2257d6ac/external/bundle/lib/ruby/2.6.0/gems/rack-2.2.3/lib/rack.rb:18: warning: already initialized constant Rack::HTTP_PORT
/private/var/tmp/_bazel_johnfirebaugh/1c33c885364b1c1481b2fccb2257d6ac/execroot/__main__/bazel-out/darwin-fastbuild/bin/main.runfiles/bundle/lib/ruby/2.6.0/gems/rack-2.2.3/lib/rack.rb:18: warning: previous definition of HTTP_PORT was here
[snip many more warnings of the same form]

I debugged a bit by placing a puts caller inside rack.rb. It gets required via two call stacks:

  • First directly via main.rb:1, resolved as <outputBase>/execroot/__main__/bazel-out/darwin-fastbuild/bin/main.runfiles/bundle/lib/ruby/2.6.0/gems/rack-2.2.3/lib/rack.rb
  • Second via a require_relative in rack/session/abstract/id.rb, resolved as <outputBase>/external/bundle/lib/ruby/2.6.0/gems/rack-2.2.3/lib/rack.rb:20

It appears that require_relative is resolving the require relative to the resolved symlink location in bazel's external directory, rather than the runfiles location where the direct require was resolved to.

"Can't find ruby interpreter in the PATH"

Hello, I'm looking for usage information on this project, looking to incorporate it into an existing mono-repo. For our purposes as a primarily golang project, I can't rely on ruby already being pre-installed on our devs' machines, so I'm doing this in a VM that doesn't have ruby installed. A very basic install with the WORKSPACE, BUILD.bazel, a Gemfile and a ruby source file fails to bazel run with /usr/bin/env: ‘ruby’: No such file or directory

It will also output a DEBUG message with DEBUG: /home/vince/.cache/bazel/_bazel_vince/acbc1f1ef0fcc039a89f9e3efce79ee0/external/bazelruby_rules_ruby/ruby/private/toolchains/ruby_runtime.bzl:91:14: Can't find ruby interpreter in the PATH

This is due to the build target boilerplate calling /usr/bin/env ruby in a shebang. I would have assumed that rules_ruby would put the ruby that it has just built into PATH when executing a ruby_binary but this apparently isn't the case. Is there some kind of WORKSPACE invocation I'm missing? How can I get it to use the ruby that it's installing?

`ruby_bundle` 'the path ... does not exist' for `gem 'dave', path: dave` gems in Gemfile

Our Gemfile contains a couple of local 'path' referenced gems like this:

gem 'cube_api', path: 'engines/cube_api'
...

And when doing a bazel build of the ruby_bundle we get the following error:

The path
`/private/var/tmp/_bazel_robgates/8efc94f49031def35bcd0c9e587be3f7/external/webapp_bundle/engines/cube_api`
does not exist.
ERROR: Analysis of target '//src/webapp:webapp_lib' failed; build aborted: Analysis failed

Is there a way to symlink/include those paths for the the ruby_bundle?

Setup script attempts to delete ~/.rbenv if rbenv is installed with Homebrew

I can't tell for sure, but the setup script seems to try to delete ~/.rbenv if there is no bin/rbenv executable inside there. I believe that Homebrew installs the rbenv binary in /usr/oca/bin/rbenv.

I tried to run the bin/setup command on my Mac, but then was worried that it seemed to want to delete ~/.rbenv.
Screen Shot 2020-09-29 at 4 38 07 PM

My ~/.rbenv just has shims and versions whereas my rbenv executable comes from /usr/local/bin/rbenv:
Screen Shot 2020-09-29 at 4 40 20 PM

Use sorbets Ruby 2.6 build instead of host

We should allow ruby_toolchains() to receive an argument ruby_version which should have the following options:

  • host (default) — current implementation of the host ruby
  • 2.6.2 — uses Sorbet's ruby Bazel rules to install the desired version. Unfortunately we are limited to only the versions that Sorbet builds today (2.4.3 and 2.6.3)

Restore functionality that supported symlinking an existing Ruby interpreter

Ruby bundling functionality that was temporarily removed in This PR.

So ruby_binary now depends on the interpreter installed on the runtime environment as py_binary does.

@kig — question: is the goal to allow users a choice between using 1) locally installed ruby on the system, 2) localized user install via rbenv/rvm, 3) download and build ruby interpreter from scratch?

Documentation question regarding native extensions

I was initially mislead by README content that says: "Building native extensions in gems with Bazel" is not yet supported.

Is non-gem support for native extensions documented somewhere? I eventually found

https://github.com/bazelruby/rules_ruby/blob/master/ruby/tests/BUILD.bazel#L186

so it seems like it is possible to use native extensions in ruby_binary and ruby_library, but I only figured this out because the @org_ruby_lang_ruby_toolchain//:headers rule seems designed to support this use case.

🐛 "Could not create symlink... (File exists)"

Description

I have a failing CI build here.

File "/var/lib/buildkite-agent/.cache/bazel/_bazel_buildkite-agent/2800332926ca5670e559e7706ead37a8/external/bazelruby_ruby_rules/ruby/private/toolchains/ruby_runtime.bzl", line 52, in _install_dirs
--
  | ctx.symlink(path, rel_path)
  | java.io.IOException: Could not create symlink from /usr/include to /var/lib/buildkite-agent/.cache/bazel/_bazel_buildkite-agent/2800332926ca5670e559e7706ead37a8/external/org_ruby_lang_ruby_toolchain/usr/include: /var/lib/buildkite-agent/.cache/bazel/_bazel_buildkite-agent/2800332926ca5670e559e7706ead37a8/external/org_ruby_lang_ruby_toolchain/usr/include (File exists)

This is working locally, but failing in CI.


rules_ruby version: a0d21e570f79424e6125df6c691ab27ed7454e1a
Bazel version: 2.0.0

Question about minitest and rails

Hello all. My team is currently trying to setup bazel rules ruby to run our rails minitest test suite. The ruby_test rule seems to expect to run tests with the ruby executable directly. Are there good examples or documentation on running rails app tests? We have tried to various methods but are running into issues getting minitest to load.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.