diff --git a/lib/ruby_lsp/tapioca/addon.rb b/lib/ruby_lsp/tapioca/addon.rb index 364b3fbcf..8a548a0f0 100644 --- a/lib/ruby_lsp/tapioca/addon.rb +++ b/lib/ruby_lsp/tapioca/addon.rb @@ -80,8 +80,8 @@ def workspace_did_change_watched_files(changes) current_checksum = Zlib.crc32(content).to_s if change[:type] == Constant::FileChangeType::CHANGED && @file_checksums[path] == current_checksum - $stderr.puts "File has not changed. Skipping #{path}" - next + # $stderr.puts "File has not changed. Skipping #{path}" + # next end @file_checksums[path] = current_checksum diff --git a/lib/ruby_lsp/tapioca/server_addon.rb b/lib/ruby_lsp/tapioca/server_addon.rb index 607095ab1..48fd20cc8 100644 --- a/lib/ruby_lsp/tapioca/server_addon.rb +++ b/lib/ruby_lsp/tapioca/server_addon.rb @@ -2,6 +2,9 @@ # frozen_string_literal: true require "tapioca/internal" +require "objspace" + +$runs = 1 module RubyLsp module Tapioca @@ -15,13 +18,25 @@ def execute(request, params) when "dsl" dsl(params) end + reset_runtime_tracker_state + puts "Mixins to constants size: #{ObjectSpace.memsize_of(::Tapioca::Runtime::Trackers::Mixin.instance_variable_get(:@mixins_to_constants))}" end private def dsl(params) + GC.compact + ObjectSpace.trace_object_allocations_start load("tapioca/cli.rb") # Reload the CLI to reset thor defaults between requests ::Tapioca::Cli.start(["dsl", "--lsp_addon", "--workers=1"] + params[:constants]) + # Object.const_get("Shop") + ObjectSpace.trace_object_allocations_stop + ObjectSpace.dump_all(output: File.open("tmp/tapioca-#{$runs}", "w+")) + $runs += 1 + end + + def reset_runtime_tracker_state + ::Tapioca::Runtime::Trackers.reset_state end end end diff --git a/lib/tapioca/commands/abstract_dsl.rb b/lib/tapioca/commands/abstract_dsl.rb index 77c03e2c2..a74b5a8a5 100644 --- a/lib/tapioca/commands/abstract_dsl.rb +++ b/lib/tapioca/commands/abstract_dsl.rb @@ -101,6 +101,9 @@ def generate_dsl_rbi_files(outpath, quiet:) files_to_purge = existing_rbi_files - generated_files files_to_purge + ensure + # pipeline.active_compilers.each(&:reset_state) if @lsp_addon + @pipeline = nil if @lsp_addon end sig { returns(T::Array[String]) } diff --git a/lib/tapioca/helpers/rbi_helper.rb b/lib/tapioca/helpers/rbi_helper.rb index a2249280a..ff85aff5a 100644 --- a/lib/tapioca/helpers/rbi_helper.rb +++ b/lib/tapioca/helpers/rbi_helper.rb @@ -108,13 +108,14 @@ def as_non_nilable_type(type) sig { params(name: String).returns(T::Boolean) } def valid_method_name?(name) # try to parse a method definition with this name - iseq = RubyVM::InstructionSequence.compile("def #{name}; end", nil, nil, 0, false) + # iseq = RubyVM::InstructionSequence.compile("def #{name}; end", nil, nil, 0, false) # pull out the first operation in the instruction sequence and its first argument - op, arg, _data = iseq.to_a.dig(-1, 0) + # op, arg, _data = iseq.to_a.dig(-1, 0) # make sure that the operation is a method definition and the method that was # defined has the expected name, for example, for `def !foo; end` we don't get # a syntax error but instead get a method defined as `"foo"` - op == :definemethod && arg == name.to_sym + # op == :definemethod && arg == name.to_sym + true rescue SyntaxError false end diff --git a/lib/tapioca/runtime/trackers.rb b/lib/tapioca/runtime/trackers.rb index d57f087e3..3608e8713 100644 --- a/lib/tapioca/runtime/trackers.rb +++ b/lib/tapioca/runtime/trackers.rb @@ -39,6 +39,11 @@ def disable_all! def register_tracker(tracker) @trackers << tracker end + + sig { void } + def reset_state + @trackers.each(&:reset_state) + end end end end diff --git a/lib/tapioca/runtime/trackers/autoload.rb b/lib/tapioca/runtime/trackers/autoload.rb index 2f58058be..1168824ba 100644 --- a/lib/tapioca/runtime/trackers/autoload.rb +++ b/lib/tapioca/runtime/trackers/autoload.rb @@ -53,6 +53,11 @@ def with_disabled_exits(&block) Kernel.define_method(:abort, original_abort) end end + + sig { override.void } + def reset_state + @constant_names_registered_for_autoload = [] + end end end end diff --git a/lib/tapioca/runtime/trackers/constant_definition.rb b/lib/tapioca/runtime/trackers/constant_definition.rb index 3ebd83402..15661cf2b 100644 --- a/lib/tapioca/runtime/trackers/constant_definition.rb +++ b/lib/tapioca/runtime/trackers/constant_definition.rb @@ -52,6 +52,8 @@ class ConstantLocation < T::Struct end class << self + extend T::Sig + def disable! @class_tracepoint.disable @creturn_tracepoint.disable @@ -75,6 +77,11 @@ def files_for(klass) def locations_for(klass) @class_files.fetch(klass, Set.new) end + + sig { override.void } + def reset_state + @class_files = {} + end end end end diff --git a/lib/tapioca/runtime/trackers/mixin.rb b/lib/tapioca/runtime/trackers/mixin.rb index f0c37a84e..986ae2219 100644 --- a/lib/tapioca/runtime/trackers/mixin.rb +++ b/lib/tapioca/runtime/trackers/mixin.rb @@ -62,6 +62,12 @@ def mixin_location(mixin, mixin_type, constant) find_or_initialize_mixin_lookup(mixin).dig(mixin_type, constant) end + sig { override.void } + def reset_state + @constants_to_mixin_locations = {} + @mixins_to_constants = {} + end + private sig { params(constant: Module, mixin: Module, mixin_type: Type, location: String).void } diff --git a/lib/tapioca/runtime/trackers/required_ancestor.rb b/lib/tapioca/runtime/trackers/required_ancestor.rb index b9213022f..36fe0176e 100644 --- a/lib/tapioca/runtime/trackers/required_ancestor.rb +++ b/lib/tapioca/runtime/trackers/required_ancestor.rb @@ -44,6 +44,11 @@ def required_ancestors_by(mod) nil end end + + sig { override.void } + def reset_state + @required_ancestors_map = {} + end end end end diff --git a/lib/tapioca/runtime/trackers/tracker.rb b/lib/tapioca/runtime/trackers/tracker.rb index f70fec472..8b3e7d7ec 100644 --- a/lib/tapioca/runtime/trackers/tracker.rb +++ b/lib/tapioca/runtime/trackers/tracker.rb @@ -39,6 +39,9 @@ def with_disabled_tracker(&block) ensure @enabled = original_state end + + sig { abstract.void } + def reset_state; end end end end