Skip to content

Fix "no anonymous block parameter" in ruby 3.1#51476

Merged
byroot merged 1 commit intorails:mainfrom
marvinthepa:no_anonymous_block_parameter
Apr 16, 2024
Merged

Fix "no anonymous block parameter" in ruby 3.1#51476
byroot merged 1 commit intorails:mainfrom
marvinthepa:no_anonymous_block_parameter

Conversation

@marvinthepa
Copy link
Copy Markdown
Contributor

Motivation / Background

On
ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [x86_64-linux-gnu] (e.g. Ubuntu 23.10), simple rails commands fail with
no anonymous block parameter:

vendor/bundle/ruby/3.1.0/bundler/gems/rails-0e0da316ca80/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb:38:in `require': vendor/bundle/ruby/3.1.0/bundler/gems/rails-0e0da316ca80/activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb:190: no anonymous block parameter (SyntaxError)
        from vendor/bundle/ruby/3.1.0/bundler/gems/rails-0e0da316ca80/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb:38:in `<class:AbstractAdapter>'
        from vendor/bundle/ruby/3.1.0/bundler/gems/rails-0e0da316ca80/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb:31:in `<module:ConnectionAdapters>'
        from vendor/bundle/ruby/3.1.0/bundler/gems/rails-0e0da316ca80/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb:15:in `<module:ActiveRecord>'
        from vendor/bundle/ruby/3.1.0/bundler/gems/rails-0e0da316ca80/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb:14:in `<top (required)>'
        from vendor/bundle/ruby/3.1.0/bundler/gems/rails-0e0da316ca80/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb:3:in `require'
        from vendor/bundle/ruby/3.1.0/bundler/gems/rails-0e0da316ca80/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb:3:in `<top (required)>'
        from vendor/bundle/ruby/3.1.0/bundler/gems/rails-0e0da316ca80/activerecord/lib/active_record/connection_adapters.rb:100:in `require'
        from vendor/bundle/ruby/3.1.0/bundler/gems/rails-0e0da316ca80/activerecord/lib/active_record/connection_adapters.rb:100:in `resolve'
        from vendor/bundle/ruby/3.1.0/bundler/gems/rails-0e0da316ca80/activerecord/lib/active_record/database_configurations/database_config.rb:18:in `adapter_class'
        from vendor/bundle/ruby/3.1.0/bundler/gems/rails-0e0da316ca80/activerecord/lib/active_record/database_configurations/database_config.rb:26:in `validate!'
        from vendor/bundle/ruby/3.1.0/bundler/gems/rails-0e0da316ca80/activerecord/lib/active_record/connection_adapters/abstract/connection_handler.rb:268:in `resolve_pool_config'
        from vendor/bundle/ruby/3.1.0/bundler/gems/rails-0e0da316ca80/activerecord/lib/active_record/connection_adapters/abstract/connection_handler.rb:116:in `establish_connection'
        from vendor/bundle/ruby/3.1.0/bundler/gems/rails-0e0da316ca80/activerecord/lib/active_record/connection_handling.rb:53:in `establish_connection'
        from repro.rb:20:in `<main>'

One file repro:

# frozen_string_literal: true

require "bundler/inline"
require 'bundler'
Bundler.configure

gemfile(true) do
  source "https://rubygems.org"

  git_source(:github) { |repo| "https://github.com/#{repo}.git" }

  gem "rails", github: "rails/rails", branch: "main"

  gem "sqlite3"
end

require "active_record"

# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")

This Pull Request has been created to fix this issue.

Detail

I do not know why this triggers "no anonymous block parameter", as the parameter is clearly there.
It was introduced when a named parameter was added, without the named parameter the error does not occur.
However, converting to a named block parameter fixes this issue.

@JoeDupuis
Copy link
Copy Markdown
Contributor

I wasn't able to reproduce with the one file repro.

I tried to fix the repro, but I am getting inconsistent results (first run give a different error).
I've only been able to reproduce consistently using a Gemfile.

I was able to reproduce on both Ubuntu and Nixos.

Here's a repo for the repro on ubuntu: https://github.com/JoeDupuis/rails_repro_51476

Issue happens on 3.1, but doesn't affect 3.2

Started on this commit: 5d528ba

@smudge
Copy link
Copy Markdown

smudge commented Apr 5, 2024

I saw this on 3.1.0, but upgrading to a newer 3.1 patch resolved the issue for me. (3.1.3 worked for me.)

Copy link
Copy Markdown
Member

@skipkayhil skipkayhil left a comment

Choose a reason for hiding this comment

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

I think this is a good change seems its seem like anonymous block param support is spotty across Ruby versions (ex. rubocop/rubocop#12571).

I searched the Rails repo for any more occurrences and found just one more:

$ rg '&\)'
actionpack/test/dispatch/request_test.rb
1234:    %w(; &).each do |sep|

activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb
177:      def cache(&)
178:        pool.enable_query_cache(&)
189:      def uncached(dirties: true, &)
190:        pool.disable_query_cache(dirties: dirties, &)

activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb
678:        #   ALTER TABLE "products" ADD CONSTRAINT price_check EXCLUDE USING gist (price WITH =, availability_range WITH &&)

activesupport/lib/active_support/evented_file_update_checker.rb
178:        paths.map { |path| path.ascend.to_a }.reduce(&:&)&.first

Can you also change the cache(&) on 177?

@matthewd
Copy link
Copy Markdown
Member

matthewd commented Apr 6, 2024

Should we just rubocop against anonymous blocks until we drop Ruby 3.1 support? The newer syntax is a small nicety, but it doesn't seem worth tangling with edge cases if we can just wait a while instead.

Also change another anonymous block to a named block as requested by
skipkayhil, although it does not trigger the same error as the method
does not contain any keyword arguments.
@marvinthepa marvinthepa force-pushed the no_anonymous_block_parameter branch from 3170805 to f2addb5 Compare April 6, 2024 19:34
@yahonda
Copy link
Copy Markdown
Member

yahonda commented Apr 11, 2024

It is likely duplicate with https://bugs.ruby-lang.org/issues/18673
that has been fixed in 3.2 ruby/ruby@3bb70a6 and backported to Ruby 3.1.3 via ruby/ruby@750d4dc .

@byroot
Copy link
Copy Markdown
Member

byroot commented Apr 16, 2024

The alternative is to bump the ruby version requirement to 3.1.3. But it's perhaps a bit weird.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants