ruby-grape / ruby-grape/grape

Namespace is evaluated twice

Open
#2,081 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug?
Dominant language
Ruby
Stars
10k
Forks
1.2k
Avg merge
14h 38m
Merged PRs (30d)
92

Description

I put the following code into my namespace definition:

puts caller
puts '~~~~'

and it is executed twice, I see two backtraces. Is this normal? The file itself is loaded only once.

/usr/local/bundle/gems/grape-1.3.3/lib/grape/api/instance.rb:120:in `instance_eval'
/usr/local/bundle/gems/grape-1.3.3/lib/grape/api/instance.rb:120:in `block in evaluate_as_instance_with_configuration'
/usr/local/bundle/gems/grape-1.3.3/lib/grape/util/lazy_block.rb:11:in `evaluate_from'
/usr/local/bundle/gems/grape-1.3.3/lib/grape/api/instance.rb:127:in `evaluate_as_instance_with_configuration'
/usr/local/bundle/gems/grape-1.3.3/lib/grape/api/instance.rb:107:in `block in nest'
/usr/local/bundle/gems/grape-1.3.3/lib/grape/api/instance.rb:107:in `each'
/usr/local/bundle/gems/grape-1.3.3/lib/grape/api/instance.rb:107:in `nest'
/usr/local/bundle/gems/grape-1.3.3/lib/grape/dsl/routing.rb:172:in `block in namespace'
/usr/local/bundle/gems/grape-1.3.3/lib/grape/dsl/settings.rb:160:in `within_namespace'
/usr/local/bundle/gems/grape-1.3.3/lib/grape/dsl/routing.rb:169:in `namespace'
/opt/reports_data/app/controllers/data.rb:50:in `block in <class:Data>'
/usr/local/bundle/gems/grape-1.3.3/lib/grape/api/instance.rb:120:in `instance_eval'
/usr/local/bundle/gems/grape-1.3.3/lib/grape/api/instance.rb:120:in `block in evaluate_as_instance_with_configuration'
/usr/local/bundle/gems/grape-1.3.3/lib/grape/util/lazy_block.rb:11:in `evaluate_from'
/usr/local/bundle/gems/grape-1.3.3/lib/grape/api/instance.rb:127:in `evaluate_as_instance_with_configuration'
/usr/local/bundle/gems/grape-1.3.3/lib/grape/api/instance.rb:107:in `block in nest'
/usr/local/bundle/gems/grape-1.3.3/lib/grape/api/instance.rb:107:in `each'
/usr/local/bundle/gems/grape-1.3.3/lib/grape/api/instance.rb:107:in `nest'
/usr/local/bundle/gems/grape-1.3.3/lib/grape/dsl/routing.rb:172:in `block in namespace'
/usr/local/bundle/gems/grape-1.3.3/lib/grape/dsl/settings.rb:160:in `within_namespace'
/usr/local/bundle/gems/grape-1.3.3/lib/grape/dsl/routing.rb:169:in `namespace'
/usr/local/bundle/gems/grape-1.3.3/lib/grape/api.rb:155:in `replay_step_on'
/usr/local/bundle/gems/grape-1.3.3/lib/grape/api.rb:147:in `block in add_setup'
/usr/local/bundle/gems/grape-1.3.3/lib/grape/api.rb:146:in `each'
/usr/local/bundle/gems/grape-1.3.3/lib/grape/api.rb:146:in `add_setup'
/usr/local/bundle/gems/grape-1.3.3/lib/grape/api.rb:42:in `block (2 levels) in override_all_methods!'
/opt/reports_data/app/controllers/data.rb:37:in `<class:Data>'
~~~~
/usr/local/bundle/gems/grape-1.3.3/lib/grape/api/instance.rb:120:in `instance_eval'
/usr/local/bundle/gems/grape-1.3.3/lib/grape/api/instance.rb:120:in `block in evaluate_as_instance_with_configuration'
/usr/local/bundle/gems/grape-1.3.3/lib/grape/util/lazy_block.rb:11:in `evaluate_from'
/usr/local/bundle/gems/grape-1.3.3/lib/grape/api/instance.rb:127:in `evaluate_as_instance_with_configuration'
/usr/local/bundle/gems/grape-1.3.3/lib/grape/api/instance.rb:107:in `block in nest'
/usr/local/bundle/gems/grape-1.3.3/lib/grape/api/instance.rb:107:in `each'
/usr/local/bundle/gems/grape-1.3.3/lib/grape/api/instance.rb:107:in `nest'
/usr/local/bundle/gems/grape-1.3.3/lib/grape/dsl/routing.rb:172:in `block in namespace'
/usr/local/bundle/gems/grape-1.3.3/lib/grape/dsl/settings.rb:160:in `within_namespace'
/usr/local/bundle/gems/grape-1.3.3/lib/grape/dsl/routing.rb:169:in `namespace'
/opt/reports_data/app/controllers/data.rb:50:in `block in <class:Data>'
/usr/local/bundle/gems/grape-1.3.3/lib/grape/api/instance.rb:120:in `instance_eval'
/usr/local/bundle/gems/grape-1.3.3/lib/grape/api/instance.rb:120:in `block in evaluate_as_instance_with_configuration'
/usr/local/bundle/gems/grape-1.3.3/lib/grape/util/lazy_block.rb:11:in `evaluate_from'
/usr/local/bundle/gems/grape-1.3.3/lib/grape/api/instance.rb:127:in `evaluate_as_instance_with_configuration'
/usr/local/bundle/gems/grape-1.3.3/lib/grape/api/instance.rb:107:in `block in nest'
/usr/local/bundle/gems/grape-1.3.3/lib/grape/api/instance.rb:107:in `each'
/usr/local/bundle/gems/grape-1.3.3/lib/grape/api/instance.rb:107:in `nest'
/usr/local/bundle/gems/grape-1.3.3/lib/grape/dsl/routing.rb:172:in `block in namespace'
/usr/local/bundle/gems/grape-1.3.3/lib/grape/dsl/settings.rb:160:in `within_namespace'
/usr/local/bundle/gems/grape-1.3.3/lib/grape/dsl/routing.rb:169:in `namespace'
/usr/local/bundle/gems/grape-1.3.3/lib/grape/api.rb:155:in `replay_step_on'
/usr/local/bundle/gems/grape-1.3.3/lib/grape/api.rb:105:in `block in replay_setup_on'
/usr/local/bundle/gems/grape-1.3.3/lib/grape/api.rb:104:in `each'
/usr/local/bundle/gems/grape-1.3.3/lib/grape/api.rb:104:in `replay_setup_on'
/usr/local/bundle/gems/grape-1.3.3/lib/grape/api.rb:97:in `mount_instance'
/usr/local/bundle/gems/grape-1.3.3/lib/grape/dsl/routing.rb:86:in `block in mount'
/usr/local/bundle/gems/grape-1.3.3/lib/grape/dsl/routing.rb:84:in `each_pair'
/usr/local/bundle/gems/grape-1.3.3/lib/grape/dsl/routing.rb:84:in `mount'
/usr/local/bundle/gems/grape-1.3.3/lib/grape/api.rb:155:in `replay_step_on'
/usr/local/bundle/gems/grape-1.3.3/lib/grape/api.rb:147:in `block in add_setup'
/usr/local/bundle/gems/grape-1.3.3/lib/grape/api.rb:146:in `each'
/usr/local/bundle/gems/grape-1.3.3/lib/grape/api.rb:146:in `add_setup'
/usr/local/bundle/gems/grape-1.3.3/lib/grape/api.rb:42:in `block (2 levels) in override_all_methods!'
/opt/reports_data/app/controllers/api.rb:29:in `<class:API>'
~~~~

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Trace the namespace evaluation paths in lib/grape/api/instance.rb, lib/grape/util/lazy_block.rb, lib/grape/dsl/routing.rb, and lib/grape/api.rb, using the reported stack traces as entry points. Determine whether the namespace block is intentionally replayed and identify or add a focused test that captures the expected evaluation count; the issue is done when the behavior is explained and verified.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.