-
Notifications
You must be signed in to change notification settings - Fork 28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reload routes when changed #501
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -119,10 +119,13 @@ def create_definition_listener(response_builder, uri, node_context, dispatcher) | |
|
||
sig { params(changes: T::Array[{ uri: String, type: Integer }]).void } | ||
def workspace_did_change_watched_files(changes) | ||
if changes.any? do |change| | ||
change[:uri].end_with?("db/schema.rb") || change[:uri].end_with?("structure.sql") | ||
end | ||
schema = lambda { |change| change[:uri].end_with?("db/schema.rb") || change[:uri].end_with?("structure.sql") } | ||
routes = lambda { |change| change[:uri].end_with?("routes.rb") || change[:uri].match?("routes/**/*.rb") } | ||
|
||
if changes.any?(&schema) | ||
@rails_runner_client.trigger_reload | ||
elsif changes.any?(&routes) | ||
@rails_runner_client.reload_routes | ||
end | ||
end | ||
|
||
|
@@ -141,7 +144,7 @@ def register_additional_file_watchers(global_state:, outgoing_queue:) | |
register_options: Interface::DidChangeWatchedFilesRegistrationOptions.new( | ||
watchers: [ | ||
Interface::FileSystemWatcher.new( | ||
glob_pattern: "**/*structure.sql", | ||
glob_pattern: "**/*structure.sql,**/*routes.rb,routes/**/*.rb", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this should cover almost any way that people set up the Rails routes, but there is a risk of a false positive if someone is using the term There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤔 but I wonder if we can ask Rails where all the routes files are... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Possibly:
|
||
kind: Constant::WatchKind::CREATE | Constant::WatchKind::CHANGE | Constant::WatchKind::DELETE, | ||
), | ||
], | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -176,10 +176,34 @@ def resolve_route_info(requirements) | |
assert_equal("Hello\n", stderr) | ||
end | ||
|
||
test "reloads if routes files change" do | ||
old_routes = File.read("test/dummy/config/routes.rb") | ||
|
||
@server.execute("route_location", { name: "user_path" }) | ||
location = response[:result][:location] | ||
assert_match(%r{test/dummy/config/routes.rb:5$}, location) | ||
|
||
File.write("test/dummy/config/routes.rb", <<~RUBY) | ||
Rails.application.routes.draw do | ||
end | ||
RUBY | ||
|
||
@server.execute("reload_routes", {}) | ||
|
||
@server.execute("route_location", { name: "user_path" }) | ||
|
||
assert_nil(response[:result]) | ||
ensure | ||
File.write("test/dummy/config/routes.rb", old_routes) | ||
end | ||
|
||
private | ||
|
||
def response | ||
_headers, content = @stdout.string.split("\r\n\r\n") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Had to change to handle making multiple requests. |
||
JSON.parse(content, symbolize_names: true) | ||
headers, content = @stdout.string.split("\r\n\r\n") | ||
content_length = headers[/Content-Length: (\d+)/i, 1].to_i | ||
# binding.break | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @vinistock I'm trying to figure out what's happening here: If I add the breakpoint, and call |
||
# @stdout.string = "" | ||
JSON.parse(content.first(content_length), symbolize_names: true) | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Refactored to simplify the conditional.