Skip to content
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

[Tapioca Add-on] Generate DSL RBIs in add-on mode #2093

Merged
merged 3 commits into from
Nov 27, 2024
Merged

Conversation

KaanOzkan
Copy link
Contributor

@KaanOzkan KaanOzkan commented Nov 26, 2024

Motivation

Extracting from main...tapioca-addon-feature-branch.

Implementation

Tests

Will be added later.

KaanOzkan and others added 2 commits November 26, 2024 11:42
Co-authored-by: Alex Rocha <[email protected]>
Co-authored-by: Andy Waite <[email protected]>
By leveraging file checksums using `Zlib.crc32`, we can avoid triggering
the DSL generation process for files that have not changed.
@KaanOzkan KaanOzkan changed the title Generate DSL RBIs in add-on mode [Tapioca Add-on] Generate DSL RBIs in add-on mode Nov 26, 2024
@KaanOzkan KaanOzkan added the enhancement New feature or request label Nov 26, 2024
@KaanOzkan KaanOzkan marked this pull request as ready for review November 26, 2024 18:32
@KaanOzkan KaanOzkan requested a review from a team as a code owner November 26, 2024 18:32
@@ -13,13 +13,17 @@ def name
def execute(request, params)
case request
when "dsl"
dsl(params)
fork do
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We run the DSL generation in a short living process because it uses a lot of memory.

@@ -41,6 +46,7 @@ def activate(global_state, outgoing_queue)
rescue IncompatibleApiError
# The requested version for the Rails add-on no longer matches. We need to upgrade and fix the breaking
# changes
puts "IncompatibleApiError: Cannot activate Tapioca LSP add-on"
Copy link
Contributor Author

@KaanOzkan KaanOzkan Nov 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume if we get this error @rails_runner_client will never be set and we'll return early in workspace_did_change_watched_files

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't puts in LSP related code. Although the Ruby LSP will override the default output device to prevent this from breaking communication, we should always standardize on window log messages.

Suggested change
puts "IncompatibleApiError: Cannot activate Tapioca LSP add-on"
outgoing_queue << Notification.window_log_message("IncompatibleApiError: Cannot activate Tapioca LSP add-on", type: Constant::MessageType::WARNING)

@processable_constants = nil
@all_classes = nil
@all_modules = nil
end
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are memoized but in LSP mode we want them to be calculated every time.

@@ -63,7 +69,41 @@ def workspace_did_change_watched_files(changes)
return unless T.must(@global_state).enabled_feature?(:tapiocaAddon)
return unless @rails_runner_client # Client is not ready

nil
constants = changes.flat_map do |change|
path = URI(change[:uri]).to_standardized_path
Copy link
Contributor

@andyw8 andyw8 Nov 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(nit) Perhaps extract the body of this to its own method for readability?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I refactored the checksum logic into its own method. It's a bit longer and I don't love the boolean returns but I think main loop reads much better now. Lmk what you think.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a little uncomfortable that file_updated? has side-effects. I'll take a look to see if there is another way we could structure it.

Copy link
Member

@vinistock vinistock left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just two minor things

@@ -41,6 +46,7 @@ def activate(global_state, outgoing_queue)
rescue IncompatibleApiError
# The requested version for the Rails add-on no longer matches. We need to upgrade and fix the breaking
# changes
puts "IncompatibleApiError: Cannot activate Tapioca LSP add-on"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't puts in LSP related code. Although the Ruby LSP will override the default output device to prevent this from breaking communication, we should always standardize on window log messages.

Suggested change
puts "IncompatibleApiError: Cannot activate Tapioca LSP add-on"
outgoing_queue << Notification.window_log_message("IncompatibleApiError: Cannot activate Tapioca LSP add-on", type: Constant::MessageType::WARNING)

lib/ruby_lsp/tapioca/addon.rb Outdated Show resolved Hide resolved
when Constant::FileChangeType::DELETED
@file_checksums.delete(path)
else
T.must(@outgoing_queue) << Notification.window_log_message(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we anticipating other change types?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, Sorbet was complaining and I thought it'd be nice to have an else here for the future.

@rails_runner_client.register_server_addon(File.expand_path("server_addon.rb", __dir__))
rescue IncompatibleApiError
# The requested version for the Rails add-on no longer matches. We need to upgrade and fix the breaking
# changes
outgoing_queue << Notification.window_log_message(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this also be an instance variable?

Suggested change
outgoing_queue << Notification.window_log_message(
@outgoing_queue << Notification.window_log_message(

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe for consistency, but both are pointing to the same object.

@@ -77,6 +77,10 @@ def initialize(

sig { params(outpath: Pathname, quiet: T::Boolean).returns(T::Set[Pathname]) }
def generate_dsl_rbi_files(outpath, quiet:)
if @lsp_addon
pipeline.active_compilers.each(&:reset_state)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe extract this to pipeline#reset?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think main issue is another method checking for lsp_addon. I don't want this code to be reusable for other clients. It's a unique thing (kind of hacky and not ideal 😬 ) for addon support.

@KaanOzkan KaanOzkan merged commit 3eb102a into main Nov 27, 2024
28 checks passed
@KaanOzkan KaanOzkan deleted the ko/tapioca-addon-3 branch November 27, 2024 19:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants