a-nti_manner_kick_course 0.1.3 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 68267188f3995d0cf7a60c081677f7b86ebc4dba7e29a5b075b32d1edd98b411
4
- data.tar.gz: 037e96608abbb2fbef919095be48ea7dc48d171e7446292b52dc4f53be69fc60
3
+ metadata.gz: 9ff98ea010b2a18d649f9d61a592063abe42f27311ede7c647f30b8a50cb49a8
4
+ data.tar.gz: 978697243153cb4439afd3988c65799b90b00523a32ba46a199bbe5dcb8fd5a6
5
5
  SHA512:
6
- metadata.gz: 9cb5109a6b9524bc12ca8209a309bab3a46c938eb4ccbd66f1642f3c8d23b3b44fd65d9013bbbbf5eb7e5b94c23fc3460cc9b6eb786ead28c59353c57e1f37fd
7
- data.tar.gz: 5b4836b815b3cc14c7d009f1b61132cb58df8619f7e213b587f25e0eb5a67c8944d0891e75ce9d63dd73ebc3c02b7ace5cb37f9a99844e1db93b995b5e355833
6
+ metadata.gz: ee5c1bb87b979434b0cbb5cce467adc13dc7cfab3a194aef87fee9f6901f61b627a634ecf194f924f76f60bbccd75660fd8db9aabcd5decc377f076b2535806f
7
+ data.tar.gz: 3eda7e4a5837d9d14bd3981080f36d2622db09b051c4c5cc50d8e603b1fdc95faced616a6d8edf43bbcc8ef364470a40accb118a8b5d4c49f782e440939cbd64
data/README.md CHANGED
@@ -1,9 +1,14 @@
1
1
  # A::NtiMannerKickCourse
2
2
 
3
+ ## Introduction
3
4
  Rails speeds up the startup process by lazily loading classes for components like ActiveRecord::Base and ActionController::Base. Additionally, the behavior of lazy loading is essential for applying configuration settings to these components. Without lazy loading, some configurations might not be applied as expected.
4
5
 
6
+ ### Why Lazy Loading Matters
7
+ Without lazy loading, certain settings might not be applied in time.
8
+
5
9
  For example, consider the following scenario:
6
10
 
11
+ ### Example: Upgrading from Rails 7.0 to 7.1
7
12
  When upgrading from Rails 7.0 to Rails 7.1, running rails app:upgrade generates a file named `config/initializers/new_framework_defaults_7_1.rb`. This file helps incrementally enable new default settings for Rails 7.1. Initially, all settings in this file are commented out. Uncommenting the following setting will enable it:
8
13
 
9
14
  ```ruby
@@ -12,8 +17,9 @@ When upgrading from Rails 7.0 to Rails 7.1, running rails app:upgrade generates
12
17
  # as equal to an equivalent `Hash` by default.
13
18
  #++
14
19
  Rails.application.config.action_controller.allow_deprecated_parameters_hash_equality = false
15
- ````
20
+ ```
16
21
 
22
+ ### Configuration Details and Pitfalls
17
23
  As the comment suggests, this setting ensures that ActionController::Parameters instances are no longer treated as equivalent to Hash. However, if ActionController::Base is eager-loaded, this configuration will not behave as expected. Why does this happen?
18
24
 
19
25
  Configuration settings like `Rails.application.config.action_controller.allow_deprecated_parameters_hash_equality` are not automatically effective. These settings are applied to components like ActionController only during the Rails initialization process.
@@ -32,7 +38,7 @@ This assignment is wrapped in an `ActiveSupport.on_load(:action_controller, run_
32
38
  ActiveSupport.on_load(:action_controller, run_once: true) do
33
39
  # Configuration assignment logic
34
40
  end
35
- ````
41
+ ```
36
42
 
37
43
  Now, consider a gem "add_some_function_to_controller" with the following code:
38
44
 
@@ -52,8 +58,7 @@ Since all gems listed in the Gemfile are required in config/application.rb, the
52
58
 
53
59
  As a result, the configuration logic for ActionController is executed before the initializer file `config/initializers/new_framework_defaults_7_1.rb`. This means that the setting `Rails.application.config.action_controller.allow_deprecated_parameters_hash_equality = false` is not applied in time.
54
60
 
55
- Fixing the Issue
56
-
61
+ ### Fixing the Issue
57
62
  To solve this, modify the gem code as follows:
58
63
 
59
64
  ```ruby
@@ -76,6 +81,7 @@ This resolves the issue, as the setting in config/initializers/new_framework_def
76
81
 
77
82
  While this specific case is likely a common issue, other problems may also arise from changes to the initialization order. To ensure reliable behavior, it is critical to keep all component loading deferred during Rails initialization. However, manually checking for potential issues with lazy loading is impractical.
78
83
 
84
+ ### Using a-nti_manner_kick_course
79
85
  Using a-nti_manner_kick_course can help detect libraries or application code (like add_some_function_to_controller) that interfere with lazy loading, allowing you to maintain proper initialization behavior.
80
86
 
81
87
  ## Installation
@@ -94,7 +100,7 @@ And then execute:
94
100
  $ bundle
95
101
  ```
96
102
 
97
- ## Usage
103
+ ## Usage for rails application developers
98
104
 
99
105
  Start your Rails application with the ANTI_MANNER environment variable as follows.
100
106
 
@@ -110,7 +116,7 @@ $ ANTI_MANNER=1 rails runner 1
110
116
 
111
117
  If any code fails to lazy loading, the process will exit with status code 1. This command suggests which lines are eager loading Rails code. It is a good idea to regularly check in CI if lazy loading is working correctly.
112
118
 
113
- You can fix the issue by wrapping the relevant code in an `ActiveSupport.on_load` block.
119
+ You can fix the issue by wrapping the relevant code in an `ActiveSupport.on_load` block.
114
120
 
115
121
  For more details about ActiveSupport.on_load, check [the official Rails documentation](https://api.rubyonrails.org/classes/ActiveSupport/LazyLoadHooks.html).
116
122
 
@@ -119,5 +125,17 @@ If the ANTI_MANNER environment variable is not set, this gem does nothing.
119
125
  > [!CAUTION]
120
126
  > If you are using Spring, this gem will not work correctly. In that case, add DISABLE_SPRING=1 to your command before running it.
121
127
 
128
+ ## Usage for gem developers
129
+
130
+ `A::NtiMannerKickcourse.monitor { require 'your_gem' }` allows you to run the specified code in a new process and exit with status code 1 if lazy loading is not properly deferred.
131
+
132
+ Execute `require 'your_gem'` within the `A::NtiMannerKickcourse.monitor` block as follows:
133
+
134
+ ```ruby
135
+ A::NtiMannerKickcourse.monitor { require 'your_gem' }
136
+ ```
137
+
138
+ When this code runs, if `your_gem` interferes with lazy loading, an error message will be displayed and the process will exit with status code 1. This helps you quickly identify and fix lazy loading issues.
139
+
122
140
  ## License
123
141
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -5,10 +5,7 @@ module A
5
5
  class Railtie < ::Rails::Railtie
6
6
  initializer "anti_manner", before: :eager_load! do
7
7
  if A::NtiMannerKickCourse.enabled?
8
- A::NtiMannerKickCourse.finish_monitoring
9
-
10
- puts "Congratulations! No code was found that fails to defer execution!"
11
- exit
8
+ A::NtiMannerKickCourse.wrapup!
12
9
  end
13
10
  end
14
11
  end
@@ -1,5 +1,5 @@
1
1
  module A
2
2
  module NtiMannerKickCourse
3
- VERSION = "0.1.3"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
@@ -22,41 +22,25 @@ module A
22
22
  ENV["ANTI_MANNER_DEBUG"]
23
23
  end
24
24
 
25
- def monitor
26
- require "active_support/lazy_load_hooks"
27
-
25
+ def monitor_rails_startup
28
26
  start_monitoring
27
+ add_hooks
28
+ end
29
29
 
30
- MONITORED_HOOKS.each do |framework|
31
- ActiveSupport.on_load(framework) do
32
- if A::NtiMannerKickCourse.monitoring? && !A::NtiMannerKickCourse.already_checked?
33
- A::NtiMannerKickCourse.already_checked
34
- message = if A::NtiMannerKickCourse.debug?
35
- <<~"MESSAGE"
36
- During Rails startup, the block inside ActiveSupport.on_load(:#{framework}) was executed.
37
- There is code that is not being deferred as expected.
38
-
39
- Currently, debug mode is enabled, so the full stack trace is being displayed.
40
- To show only the suspicious code line, remove the ANTI_MANNER_DEBUG environment variable and rerun.
41
-
42
- #{caller}
43
- MESSAGE
44
- else
45
- suspect = caller.find { |c| !A::NtiMannerKickCourse.filtering.match?(c) }
46
- <<~"MESSAGE"
47
- During Rails startup, the block inside ActiveSupport.on_load(:#{framework}) was executed.
48
- There is code that is not being deferred as expected. The suspicious part is here.
30
+ def monitor_gem
31
+ start_monitoring
32
+ add_hooks
33
+ yield
34
+ wrapup!
35
+ end
49
36
 
50
- #{suspect}
37
+ alias monitor monitor_gem
51
38
 
52
- If you want to check the entire stack trace, set the ANTI_MANNER_DEBUG environment variable.
53
- MESSAGE
54
- end
39
+ def wrapup!
40
+ A::NtiMannerKickCourse.finish_monitoring
55
41
 
56
- abort(message)
57
- end
58
- end
59
- end
42
+ puts "✅Congratulations! No code was found that fails to defer execution!"
43
+ exit
60
44
  end
61
45
 
62
46
  def monitoring?
@@ -80,13 +64,56 @@ module A
80
64
  @already_checked = true
81
65
  end
82
66
 
67
+ def error_message(framework)
68
+ suspect = caller.find { |c| !A::NtiMannerKickCourse.filtering.match?(c) }
69
+ <<~"MESSAGE"
70
+ ❌During Rails startup, the block inside ActiveSupport.on_load(:#{framework}) was executed.
71
+ There is code that is not being deferred as expected. The suspicious part is here.
72
+
73
+ #{suspect}
74
+
75
+ If you want to check the entire stack trace, set the ANTI_MANNER_DEBUG environment variable.
76
+ MESSAGE
77
+ end
78
+
79
+ def error_message_with_debug(framework)
80
+ <<~"MESSAGE"
81
+ ❌During Rails startup, the block inside ActiveSupport.on_load(:#{framework}) was executed.
82
+ There is code that is not being deferred as expected.
83
+
84
+ Currently, debug mode is enabled, so the full stack trace is being displayed.
85
+ To show only the suspicious code line, remove the ANTI_MANNER_DEBUG environment variable and rerun.
86
+
87
+ #{caller}
88
+ MESSAGE
89
+ end
90
+
83
91
  private
84
92
 
85
93
  def start_monitoring
86
94
  @monitoring = true
87
95
  end
96
+
97
+ def add_hooks
98
+ require "active_support/lazy_load_hooks"
99
+
100
+ MONITORED_HOOKS.each do |framework|
101
+ ActiveSupport.on_load(framework) do
102
+ if A::NtiMannerKickCourse.monitoring? && !A::NtiMannerKickCourse.already_checked?
103
+ A::NtiMannerKickCourse.already_checked
104
+ message = if A::NtiMannerKickCourse.debug?
105
+ A::NtiMannerKickCourse.error_message_with_debug(framework)
106
+ else
107
+ A::NtiMannerKickCourse.error_message(framework)
108
+ end
109
+
110
+ abort(message)
111
+ end
112
+ end
113
+ end
114
+ end
88
115
  end
89
116
 
90
- monitor if enabled?
117
+ monitor_rails_startup if enabled?
91
118
  end
92
119
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: a-nti_manner_kick_course
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shinichi Maeshima
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2025-02-07 00:00:00.000000000 Z
10
+ date: 2025-03-16 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: rails
@@ -59,7 +59,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
59
59
  - !ruby/object:Gem::Version
60
60
  version: '0'
61
61
  requirements: []
62
- rubygems_version: 3.6.2
62
+ rubygems_version: 3.6.5
63
63
  specification_version: 4
64
64
  summary: This library detects code or gems that violate lazy loading conventions during
65
65
  Rails initialization.