Skip to content

Commit

Permalink
Reduce file watching overhead from Rails
Browse files Browse the repository at this point in the history
  • Loading branch information
vinistock committed Jan 14, 2025
1 parent 8d37218 commit 209ba37
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions lib/ruby_lsp/ruby_lsp_rails/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
require "json"
require "open3"

# NOTE: We should avoid printing to stderr since it causes problems. We never read the standard error pipe from the
# client, so it will become full and eventually hang or crash. Instead, return a response with an `error` key.

module RubyLsp
module Rails
module Common
Expand Down Expand Up @@ -125,6 +122,7 @@ def initialize(stdout: $stdout, override_default_output_device: true)

def start
load_routes
clear_file_system_resolver_hooks
send_result({ message: "ok", root: ::Rails.root.to_s })

while @running
Expand Down Expand Up @@ -302,6 +300,18 @@ def load_routes
routes_reloader.execute_unless_loaded if routes_reloader&.respond_to?(:execute_unless_loaded)
end
end

# File system resolver hooks spawn file watcher threads which introduce unnecessary overhead since the LSP already
# watches files. Since the Rails application is already booted by the time we reach this script, we can't no-op
# the file watcher implementation. Instead, we clear the hooks to prevent the registered file watchers from being
# instantiated
def clear_file_system_resolver_hooks
return unless defined?(::ActionView::PathRegistry)

with_notification_error_handling("clear_file_system_resolver_hooks") do
::ActionView::PathRegistry.file_system_resolver_hooks.clear
end
end
end
end
end
Expand Down

0 comments on commit 209ba37

Please sign in to comment.