Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Builds fail fast on any ruby warnings #741

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

nitishr
Copy link
Contributor

@nitishr nitishr commented Feb 18, 2025

Closes #729

Introduces and uses a reusable command in CircleCI config for failing fast on warnings. Addresses resulting warnings that originally caused builds to fail with the updated configuration.

Changes:

  1. CircleCI Configuration:
  • Introduce a reusable run-fail-fast command to execute commands and fail on any warnings in the output.
  • Conditionally set RUBYOPT for all Ruby versions except ruby:2.2 (as that version doesn't support the frozen-string-literal flag).
  1. Update Gemfile: Add the rdoc gem to the development group (as rdoc will no longer be part of the default gems starting from Ruby 3.5.0)
  2. Fix Resulting Warnings:
  • Update lib/mocha/deprecation.rb to use @logger ||= Logger.new to silence uninitialized ivar warning on older ruby versions.
  • Add a JRuby-specific warning filter in lib/mocha/object_methods.rb to suppress unnecessary warnings.
  • Modify test/acceptance/loose_keyword_argument_matching_test.rb to remove unused variable.

Benefits:

  • Improved Maintainability: The CI configuration is more maintainable and easier to update.
  • Faster Feedback Loop: The run-fail-fast command ensures that the CI pipeline fails immediately if any warnings are encountered during test execution, providing faster feedback.

Testing:

The CI pipeline has been tested to ensure that it correctly fails on warnings for the specified Ruby versions.

ruby 2.x emitted warning: instance variable @logger not initialized
JRuby emits the warning: ObjectMethods#method accesses caller method's
state and should not be aliased.

see ruby/ostruct#40 for a discussion about why
JRuby emits this warning.

We suppress the warning by extending Warning to ignore the message.
@floehopper
Copy link
Member

Closes #729

Introduces and uses a reusable command in CircleCI config for failing fast on warnings. Addresses resulting warnings that originally caused builds to fail with the updated configuration.

Thank you. I'm broadly in favour of this. I've added some comments below and in some review comments.

I've fixed the formatting issues you fixed in .circleci/config.yml, so can you rebase against main to remove them from the diff?

While I understand why the commits are in the order they are, I think I'd prefer the commits with the warning fixes to come before the changes to the CI build which catches them, i.e. so all the tests pass in each commit.

Changes:

1. **CircleCI Configuration:**


* Introduce a reusable run-fail-fast command to execute commands and fail on any warnings in the output.

This seems like quite a fragile way to detect warnings. Is there definitely no better way to do this. I wonder if it's worth looking around at what other projects do? I haven't thought this through, but could we globally patch Warning#warn to raise an exception? Although I'm not sure when Warning#warn was added or whether it captures all warnings...

* Conditionally set RUBYOPT for all Ruby versions except ruby:2.2 (as that version doesn't support the frozen-string-literal flag).


2. **Update Gemfile:** Add the rdoc gem to the development group (as rdoc will no longer be part of the default gems starting from Ruby 3.5.0)

Does the absence of this gem actually trigger a warning at the moment?

3. **Fix Resulting Warnings:**


* Update lib/mocha/deprecation.rb to use @logger ||= Logger.new to silence uninitialized ivar warning on older ruby versions.

* Add a JRuby-specific warning filter in lib/mocha/object_methods.rb to suppress unnecessary warnings.

* Modify test/acceptance/loose_keyword_argument_matching_test.rb to remove unused variable.

Benefits:

* **Improved Maintainability:** The CI configuration is more maintainable and easier to update.

* **Faster Feedback Loop:** The run-fail-fast command ensures that the CI pipeline fails immediately if any warnings are encountered during test execution, providing faster feedback.

Testing:

The CI pipeline has been tested to ensure that it correctly fails on warnings for the specified Ruby versions.

Thanks for being so thorough with the testing!

@floehopper floehopper self-requested a review February 22, 2025 17:35
Copy link
Member

@floehopper floehopper left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See review comments and comment on PR description.

@@ -36,6 +34,10 @@ jobs:
- run: gem --version
- run: bundle --version
- run: bundle install --gemfile=<< parameters.gemfile >>
- run: |
if [ "<< parameters.docker-image >>" != "ruby:2.2" ]; then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it works I think it might be slightly more expressive to use > "ruby:2.2" instead of != "ruby:2.2".

@@ -7,6 +7,7 @@ group :development do
gem 'introspection', '~> 0.0.1'
gem 'minitest'
gem 'rake'
gem 'rdoc'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless I'm mistaken, this change is orthogonal to the other changes in this commit. If so, can you split it out into a separate explanatory commit?

Comment on lines +15 to +22
module JRubyAliasMethodWarningFilter
def warn(message, *args)
super unless /ObjectMethods#method accesses caller method's state and should not be aliased/.match?(message)
end
end

# using public_send as calling extend directly causes YARD to warn about Undocumentable mixin
Warning.public_send(:extend, JRubyAliasMethodWarningFilter) # rubocop:disable Lint/SendWithMixinArgument, Style/SendWithLiteralMethodName
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks a bit ugly. Could we avoid the public_send workaround (and associated comment) and the disabling of two cops by using an approach more like this?

As an aside, I wonder if it might separately be worth trying to extract this pattern somewhere. However, if so, I'd prefer to do that in a separate PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Consider failing fast on any Ruby warnings
2 participants