From 457b4ffca9d05e1be3ca09fd439f895529571514 Mon Sep 17 00:00:00 2001 From: Martin Kuntz Jacobsen Date: Sat, 28 Nov 2026 20:03:30 +0100 Subject: [PATCH 1/2] fix: set up Bundler under coverage only when the target has a bundle The coverage helper required bundler/setup unconditionally and rescued only LoadError. A target without a bundle has no BUNDLE_GEMFILE, so Bundler searched for a Gemfile and raised GemfileNotFound or GemNotFound, which failed the test. Set up Bundler only when BUNDLE_GEMFILE is set, and warn if that fails. --- .github/workflows/ci.yml | 5 +++++ examples/gem/spec/BUILD | 8 ++++++++ examples/gem/spec/plain_test.rb | 4 ++++ ruby/private/coverage/rules_ruby_coverage.rb | 12 +++++++++++- 4 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 examples/gem/spec/plain_test.rb diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index cd2d4cc3..deebca58 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -91,6 +91,11 @@ jobs: cat "$COVERAGE_FILE" exit 1 fi + # Targets without a bundle must not fail under coverage. + - run: bazel coverage //spec:plain + shell: bash + env: + MSYS_NO_PATHCONV: 1 - name: Upload coverage report uses: actions/upload-artifact@v7 with: diff --git a/examples/gem/spec/BUILD b/examples/gem/spec/BUILD index 1295af07..6e64e041 100644 --- a/examples/gem/spec/BUILD +++ b/examples/gem/spec/BUILD @@ -21,6 +21,14 @@ rb_test( deps = [":spec_helper"], ) +rb_test( + name = "plain", + size = "small", + timeout = "moderate", # JRuby startup can be slow + srcs = ["plain_test.rb"], + main = "plain_test.rb", +) + rb_library( name = "env_spec", srcs = ["env_spec.rb"], diff --git a/examples/gem/spec/plain_test.rb b/examples/gem/spec/plain_test.rb new file mode 100644 index 00000000..86d86114 --- /dev/null +++ b/examples/gem/spec/plain_test.rb @@ -0,0 +1,4 @@ +# frozen_string_literal: true + +# Has no Bundler dependency, so `bazel coverage` runs it without BUNDLE_GEMFILE. +puts 'ok' diff --git a/ruby/private/coverage/rules_ruby_coverage.rb b/ruby/private/coverage/rules_ruby_coverage.rb index 666b01ba..5b60f3b4 100644 --- a/ruby/private/coverage/rules_ruby_coverage.rb +++ b/ruby/private/coverage/rules_ruby_coverage.rb @@ -1,8 +1,18 @@ # frozen_string_literal: true if ENV['COVERAGE'] == '1' + # Targets without a bundle have no BUNDLE_GEMFILE. Bundler would then look + # for a Gemfile above the working directory and raise an error that + # `rescue LoadError` does not catch. + if ENV['BUNDLE_GEMFILE'] + begin + require 'bundler/setup' + rescue StandardError, LoadError => e + warn "Coverage enabled but Bundler setup failed: #{e.message}" + end + end + begin - require 'bundler/setup' require 'simplecov' require 'simplecov-lcov' From f4873c80909dad1a5fa286d859fd017794035b2a Mon Sep 17 00:00:00 2001 From: Martin Kuntz Jacobsen Date: Mon, 30 Nov 2026 10:56:01 +0100 Subject: [PATCH 2/2] ci: run both coverage targets in one step --- .github/workflows/ci.yml | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index deebca58..07cb481c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -84,18 +84,14 @@ jobs: # Git Bash rewrites `//spec:add` into `/spec:add` on Windows. MSYS_NO_PATHCONV: 1 run: | - bazel coverage //spec:add + # //spec:plain has no bundle and must still pass under coverage. + bazel coverage //spec:add //spec:plain COVERAGE_FILE=bazel-testlogs/spec/add/coverage.dat if ! grep -q "SF:.*lib/gem/add.rb" "$COVERAGE_FILE"; then echo "Error: Coverage report does not contain expected file coverage" cat "$COVERAGE_FILE" exit 1 fi - # Targets without a bundle must not fail under coverage. - - run: bazel coverage //spec:plain - shell: bash - env: - MSYS_NO_PATHCONV: 1 - name: Upload coverage report uses: actions/upload-artifact@v7 with: