Skip to content

Commit 7e7b284

Browse files
compwronclaude
andcommitted
fix prosopite config: enable raise, scope envs, clean up hooks
- Set Prosopite.raise = true so N+1s fail specs instead of silently logging - Populate .prosopite_ignore with known-issue directories for gradual rollout - Scope initializer to development only; spec/support/prosopite.rb owns test config - Remove redundant prosopite settings from test.rb and production.rb - Replace fragile FactoryBot::Strategy::Create monkey-patch with SyntaxRunner hook - Filter comment lines in .prosopite_ignore parser Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent d0f5517 commit 7e7b284

5 files changed

Lines changed: 41 additions & 38 deletions

File tree

config/environments/production.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,4 @@
108108
# ]
109109
# Skip DNS rebinding protection for the default health check endpoint.
110110
# config.host_authorization = { exclude: ->(request) { request.path == "/up" } }
111-
112-
# Prosopite N+1 query detection (disabled by default in production)
113-
config.prosopite_enabled = ENV.fetch("PROSOPITE_ENABLED", "false") == "true"
114-
config.prosopite_min_n_queries = ENV.fetch("PROSOPITE_MIN_N_QUERIES", "10").to_i
115111
end

config/environments/test.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,6 @@
6363
# Raises error for missing translations.
6464
config.i18n.raise_on_missing_translations = true
6565

66-
# Prosopite N+1 query detection
67-
config.prosopite_enabled = true
68-
config.prosopite_min_n_queries = 2 # Stricter for tests
69-
7066
# Annotate rendered view with file names.
7167
# config.action_view.annotate_rendered_view_with_filenames = true
7268

config/initializers/prosopite.rb

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
# frozen_string_literal: true
22

3-
# Only enable Rack middleware if Prosopite is configured on
4-
if Rails.configuration.respond_to?(:prosopite_enabled) && Rails.configuration.prosopite_enabled
3+
# Rack middleware for development only — in test, scanning is handled by RSpec hooks
4+
if Rails.env.development? &&
5+
Rails.configuration.respond_to?(:prosopite_enabled) &&
6+
Rails.configuration.prosopite_enabled
57
require "prosopite/middleware/rack"
68
Rails.configuration.middleware.use(Prosopite::Middleware::Rack)
79
end
810

11+
# Development configuration — test config lives in spec/support/prosopite.rb
912
Rails.application.config.after_initialize do
10-
# Core settings
13+
next unless Rails.env.development?
14+
1115
Prosopite.enabled = Rails.configuration.respond_to?(:prosopite_enabled) &&
1216
Rails.configuration.prosopite_enabled
1317

14-
# Minimum repeated queries to trigger detection (default: 2)
1518
Prosopite.min_n_queries = Rails.configuration.respond_to?(:prosopite_min_n_queries) ?
1619
Rails.configuration.prosopite_min_n_queries : 2
1720

18-
# Logging options
19-
Prosopite.rails_logger = true # Log to Rails.logger
20-
Prosopite.prosopite_logger = Rails.env.development? # Log to log/prosopite.log
21+
Prosopite.rails_logger = true
22+
Prosopite.prosopite_logger = true
2123
end

spec/.prosopite_ignore

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1-
# Directories to exclude from Prosopite N+1 scanning
2-
# Remove paths as you fix N+1s in each area
1+
# Directories excluded from Prosopite N+1 raise (will still log).
2+
# Remove paths as you fix the underlying N+1s.
33
#
4-
# Example entries:
5-
# spec/features
6-
# spec/system
7-
# spec/requests/legacy
4+
# See PROSOPITE_TODO.md for the full list of known issues.
5+
spec/models
6+
spec/services
7+
spec/lib
8+
spec/system
9+
spec/requests
10+
spec/controllers
11+
spec/views
12+
spec/decorators
13+
spec/policies
14+
spec/datatables
15+
spec/helpers

spec/support/prosopite.rb

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
return unless defined?(Prosopite)
44

5-
# Test configuration
5+
# Test configuration — this file owns all Prosopite settings for the test env
66
Prosopite.enabled = true
7-
Prosopite.raise = false # Log only, don't fail specs
7+
Prosopite.raise = true
88
Prosopite.rails_logger = true
99
Prosopite.prosopite_logger = true
1010

@@ -16,35 +16,36 @@
1616
"shoulda/matchers/active_model/allow_value_matcher.rb"
1717
]
1818

19-
# Optional: Load ignore list from file for gradual rollout
19+
# Load ignore list from file for gradual rollout — directories listed in
20+
# .prosopite_ignore are scanned but won't raise, only log.
2021
PROSOPITE_IGNORE = if File.exist?("spec/.prosopite_ignore")
21-
File.read("spec/.prosopite_ignore").lines.map(&:chomp).reject(&:empty?)
22+
File.read("spec/.prosopite_ignore")
23+
.lines
24+
.map(&:chomp)
25+
.reject { |line| line.empty? || line.start_with?("#") }
2226
else
2327
[]
2428
end
2529

26-
# Monkey-patch FactoryBot to pause during factory creation
27-
# Prevents false positives from factory callbacks
28-
if defined?(FactoryBot)
29-
module FactoryBot
30-
module Strategy
31-
class Create
32-
alias_method :original_result, :result
30+
RSpec.configure do |config|
31+
# Pause Prosopite during factory creation to prevent false positives
32+
# from factory callbacks and associations
33+
config.before(:suite) do
34+
if defined?(FactoryBot)
35+
FactoryBot::SyntaxRunner.class_eval do
36+
alias_method :original_create, :create
3337

34-
def result(evaluation)
38+
def create(*args, **kwargs, &block)
3539
if defined?(Prosopite) && Prosopite.enabled?
36-
Prosopite.pause { original_result(evaluation) }
40+
Prosopite.pause { original_create(*args, **kwargs, &block) }
3741
else
38-
original_result(evaluation)
42+
original_create(*args, **kwargs, &block)
3943
end
4044
end
4145
end
4246
end
4347
end
44-
end
4548

46-
# RSpec integration
47-
RSpec.configure do |config|
4849
config.around do |example|
4950
if use_prosopite?(example)
5051
Prosopite.scan { example.run }

0 commit comments

Comments
 (0)