From 166a63178e44319b95554b2366342982ae692645 Mon Sep 17 00:00:00 2001 From: Ufuk Kayserilioglu Date: Wed, 28 Sep 2022 22:30:42 +0300 Subject: [PATCH 1/6] Add Tracker registration and disabling --- lib/tapioca/runtime/trackers.rb | 28 ++++++++++++++- lib/tapioca/runtime/trackers/tracker.rb | 45 +++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 lib/tapioca/runtime/trackers/tracker.rb diff --git a/lib/tapioca/runtime/trackers.rb b/lib/tapioca/runtime/trackers.rb index 02ea85640..a6476e889 100644 --- a/lib/tapioca/runtime/trackers.rb +++ b/lib/tapioca/runtime/trackers.rb @@ -1,6 +1,32 @@ -# typed: strict +# typed: true # frozen_string_literal: true +require "tapioca/runtime/trackers/tracker" + +module Tapioca + module Runtime + module Trackers + extend T::Sig + + @trackers = T.let([], T::Array[Tracker]) + + class << self + extend T::Sig + + sig { void } + def disable_all! + @trackers.each(&:disable!) + end + + sig { params(tracker: Tracker).void } + def register_tracker(tracker) + @trackers << tracker + end + end + end + end +end + # The load order below is important: # ---------------------------------- # We want the mixin tracker to be the first thing that is diff --git a/lib/tapioca/runtime/trackers/tracker.rb b/lib/tapioca/runtime/trackers/tracker.rb new file mode 100644 index 000000000..f70fec472 --- /dev/null +++ b/lib/tapioca/runtime/trackers/tracker.rb @@ -0,0 +1,45 @@ +# typed: true +# frozen_string_literal: true + +module Tapioca + module Runtime + module Trackers + module Tracker + extend T::Sig + extend T::Helpers + + abstract! + + class << self + extend T::Sig + + sig { params(base: T.all(Tracker, Module)).void } + def extended(base) + Trackers.register_tracker(base) + base.instance_exec do + @enabled = true + end + end + end + + sig { void } + def disable! + @enabled = false + end + + def enabled? + @enabled + end + + def with_disabled_tracker(&block) + original_state = @enabled + @enabled = false + + block.call + ensure + @enabled = original_state + end + end + end + end +end From 30547e2b8098c143ca3dfde05df7e19f2ee9437d Mon Sep 17 00:00:00 2001 From: Ufuk Kayserilioglu Date: Wed, 28 Sep 2022 22:31:40 +0300 Subject: [PATCH 2/6] Make all trackers respect disabling --- lib/tapioca/runtime/trackers/autoload.rb | 3 +++ lib/tapioca/runtime/trackers/constant_definition.rb | 11 +++++++++-- lib/tapioca/runtime/trackers/mixin.rb | 10 +++------- lib/tapioca/runtime/trackers/required_ancestor.rb | 3 +++ 4 files changed, 18 insertions(+), 9 deletions(-) diff --git a/lib/tapioca/runtime/trackers/autoload.rb b/lib/tapioca/runtime/trackers/autoload.rb index 5019f0f16..2f58058be 100644 --- a/lib/tapioca/runtime/trackers/autoload.rb +++ b/lib/tapioca/runtime/trackers/autoload.rb @@ -5,6 +5,7 @@ module Tapioca module Runtime module Trackers module Autoload + extend Tracker extend T::Sig NOOP_METHOD = ->(*_args, **_kwargs, &_block) {} @@ -28,6 +29,8 @@ def eager_load_all! sig { params(constant_name: String).void } def register(constant_name) + return unless enabled? + @constant_names_registered_for_autoload << constant_name end diff --git a/lib/tapioca/runtime/trackers/constant_definition.rb b/lib/tapioca/runtime/trackers/constant_definition.rb index bcbff921c..fb676d804 100644 --- a/lib/tapioca/runtime/trackers/constant_definition.rb +++ b/lib/tapioca/runtime/trackers/constant_definition.rb @@ -9,6 +9,7 @@ module Trackers # correspondence between classes/modules and files, as this information isn't # available in the ruby runtime without extra accounting. module ConstantDefinition + extend Tracker extend Reflection extend T::Sig @@ -20,7 +21,7 @@ class ConstantLocation < T::Struct @class_files = {}.compare_by_identity # Immediately activated upon load. Observes class/module definition. - Tapioca.register_trace(:class) do |tp| + @class_tracepoint = TracePoint.trace(:class) do |tp| next if tp.self.singleton_class? key = tp.self @@ -40,7 +41,7 @@ class ConstantLocation < T::Struct (@class_files[key] ||= Set.new) << loc end - Tapioca.register_trace(:c_return) do |tp| + @creturn_tracepoint = TracePoint.trace(:c_return) do |tp| next unless tp.method_id == :new next unless Module === tp.return_value @@ -50,6 +51,12 @@ class ConstantLocation < T::Struct end class << self + def disable! + @class_tracepoint.disable + @creturn_tracepoint.disable + super + end + def build_constant_location(tp, locations) file = resolve_loc(caller_locations) lineno = file == File.realpath(tp.path) ? tp.lineno : 0 diff --git a/lib/tapioca/runtime/trackers/mixin.rb b/lib/tapioca/runtime/trackers/mixin.rb index af50b5443..8eee01e7c 100644 --- a/lib/tapioca/runtime/trackers/mixin.rb +++ b/lib/tapioca/runtime/trackers/mixin.rb @@ -5,11 +5,11 @@ module Tapioca module Runtime module Trackers module Mixin + extend Tracker extend T::Sig @constants_to_mixin_locations = {}.compare_by_identity @mixins_to_constants = {}.compare_by_identity - @enabled = true class Type < T::Enum enums do @@ -28,11 +28,7 @@ class << self .returns(T.type_parameter(:Result)) end def with_disabled_registration(&block) - @enabled = false - - block.call - ensure - @enabled = true + with_disabled_tracker(&block) end sig do @@ -43,7 +39,7 @@ def with_disabled_registration(&block) ).void end def register(constant, mixin, mixin_type) - return unless @enabled + return unless enabled? location = Reflection.resolve_loc(caller_locations) diff --git a/lib/tapioca/runtime/trackers/required_ancestor.rb b/lib/tapioca/runtime/trackers/required_ancestor.rb index 5d13904ef..b9213022f 100644 --- a/lib/tapioca/runtime/trackers/required_ancestor.rb +++ b/lib/tapioca/runtime/trackers/required_ancestor.rb @@ -5,6 +5,7 @@ module Tapioca module Runtime module Trackers module RequiredAncestor + extend Tracker @required_ancestors_map = {}.compare_by_identity class << self @@ -12,6 +13,8 @@ class << self sig { params(requiring: T::Helpers, block: T.proc.void).void } def register(requiring, block) + return unless enabled? + ancestors = @required_ancestors_map[requiring] ||= [] ancestors << block end From 4bc73c0abda351e0138961f63528ca8b6c58a898 Mon Sep 17 00:00:00 2001 From: Ufuk Kayserilioglu Date: Wed, 28 Sep 2022 22:33:22 +0300 Subject: [PATCH 3/6] Add a CommandWithoutTracker base class that disables all trackers --- lib/tapioca/commands.rb | 1 + .../commands/command_without_tracker.rb | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+) create mode 100644 lib/tapioca/commands/command_without_tracker.rb diff --git a/lib/tapioca/commands.rb b/lib/tapioca/commands.rb index 51e96f1e4..1185c544f 100644 --- a/lib/tapioca/commands.rb +++ b/lib/tapioca/commands.rb @@ -4,6 +4,7 @@ module Tapioca module Commands autoload :Command, "tapioca/commands/command" + autoload :CommandWithoutTracker, "tapioca/commands/command_without_tracker" autoload :Annotations, "tapioca/commands/annotations" autoload :CheckShims, "tapioca/commands/check_shims" autoload :Dsl, "tapioca/commands/dsl" diff --git a/lib/tapioca/commands/command_without_tracker.rb b/lib/tapioca/commands/command_without_tracker.rb new file mode 100644 index 000000000..67b3820a9 --- /dev/null +++ b/lib/tapioca/commands/command_without_tracker.rb @@ -0,0 +1,18 @@ +# typed: strict +# frozen_string_literal: true + +module Tapioca + module Commands + class CommandWithoutTracker < Command + extend T::Helpers + + abstract! + + sig { void } + def initialize + Tapioca::Runtime::Trackers.disable_all! + super + end + end + end +end From e0085f5070663ff46c3408df86a3dcba05445aae Mon Sep 17 00:00:00 2001 From: Ufuk Kayserilioglu Date: Wed, 28 Sep 2022 22:33:22 +0300 Subject: [PATCH 4/6] Make commands that don't need trackers subclass from CommandWithoutTracker --- lib/tapioca/commands/annotations.rb | 2 +- lib/tapioca/commands/check_shims.rb | 2 +- lib/tapioca/commands/configure.rb | 2 +- lib/tapioca/commands/dsl.rb | 2 +- lib/tapioca/commands/require.rb | 2 +- lib/tapioca/commands/todo.rb | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/tapioca/commands/annotations.rb b/lib/tapioca/commands/annotations.rb index dca12f251..c064dd78d 100644 --- a/lib/tapioca/commands/annotations.rb +++ b/lib/tapioca/commands/annotations.rb @@ -3,7 +3,7 @@ module Tapioca module Commands - class Annotations < Command + class Annotations < CommandWithoutTracker extend T::Sig sig do diff --git a/lib/tapioca/commands/check_shims.rb b/lib/tapioca/commands/check_shims.rb index 8b11fead2..cc37f6275 100644 --- a/lib/tapioca/commands/check_shims.rb +++ b/lib/tapioca/commands/check_shims.rb @@ -3,7 +3,7 @@ module Tapioca module Commands - class CheckShims < Command + class CheckShims < CommandWithoutTracker extend T::Sig include SorbetHelper include RBIFilesHelper diff --git a/lib/tapioca/commands/configure.rb b/lib/tapioca/commands/configure.rb index 4a08cf817..0d54a05d1 100644 --- a/lib/tapioca/commands/configure.rb +++ b/lib/tapioca/commands/configure.rb @@ -3,7 +3,7 @@ module Tapioca module Commands - class Configure < Command + class Configure < CommandWithoutTracker sig do params( sorbet_config: String, diff --git a/lib/tapioca/commands/dsl.rb b/lib/tapioca/commands/dsl.rb index f46d185ca..44c53be40 100644 --- a/lib/tapioca/commands/dsl.rb +++ b/lib/tapioca/commands/dsl.rb @@ -3,7 +3,7 @@ module Tapioca module Commands - class Dsl < Command + class Dsl < CommandWithoutTracker include SorbetHelper include RBIFilesHelper diff --git a/lib/tapioca/commands/require.rb b/lib/tapioca/commands/require.rb index f7b9e4937..4adacc42d 100644 --- a/lib/tapioca/commands/require.rb +++ b/lib/tapioca/commands/require.rb @@ -3,7 +3,7 @@ module Tapioca module Commands - class Require < Command + class Require < CommandWithoutTracker sig do params( requires_path: String, diff --git a/lib/tapioca/commands/todo.rb b/lib/tapioca/commands/todo.rb index bf5d58c00..43473d756 100644 --- a/lib/tapioca/commands/todo.rb +++ b/lib/tapioca/commands/todo.rb @@ -3,7 +3,7 @@ module Tapioca module Commands - class Todo < Command + class Todo < CommandWithoutTracker include SorbetHelper sig do From f488a9cf9fd3b19b33090e55b04d131469d30ff2 Mon Sep 17 00:00:00 2001 From: Ufuk Kayserilioglu Date: Wed, 28 Sep 2022 22:32:12 +0300 Subject: [PATCH 5/6] Remove TracePoint registration which is no longer needed --- lib/tapioca.rb | 10 ---------- lib/tapioca/cli.rb | 2 -- 2 files changed, 12 deletions(-) diff --git a/lib/tapioca.rb b/lib/tapioca.rb index f7ac24cbb..209b576a2 100644 --- a/lib/tapioca.rb +++ b/lib/tapioca.rb @@ -11,16 +11,6 @@ module Tapioca class << self extend T::Sig - sig { params(trace_name: Symbol, block: T.proc.params(arg0: TracePoint).void).void } - def register_trace(trace_name, &block) - @traces << TracePoint.trace(trace_name, &block) - end - - sig { void } - def disable_traces - @traces.each(&:disable) - end - sig do type_parameters(:Result) .params(blk: T.proc.returns(T.type_parameter(:Result))) diff --git a/lib/tapioca/cli.rb b/lib/tapioca/cli.rb index eb4635344..e6c89d86d 100644 --- a/lib/tapioca/cli.rb +++ b/lib/tapioca/cli.rb @@ -269,8 +269,6 @@ def gem(*gems) option :payload, type: :boolean, desc: "Check shims against Sorbet's payload", default: true option :workers, aliases: ["-w"], type: :numeric, desc: "Number of parallel workers (default: auto)" def check_shims - Tapioca.disable_traces - command = Commands::CheckShims.new( gem_rbi_dir: options[:gem_rbi_dir], dsl_rbi_dir: options[:dsl_rbi_dir], From 8e277cd75077d15946b68c4f75a1fd580a460ef2 Mon Sep 17 00:00:00 2001 From: Ufuk Kayserilioglu Date: Wed, 28 Sep 2022 23:02:34 +0300 Subject: [PATCH 6/6] Run AnnotionsSpec in isolation --- spec/tapioca/commands/annotations_spec.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/spec/tapioca/commands/annotations_spec.rb b/spec/tapioca/commands/annotations_spec.rb index efa83dbe7..7f9f55894 100644 --- a/spec/tapioca/commands/annotations_spec.rb +++ b/spec/tapioca/commands/annotations_spec.rb @@ -7,6 +7,7 @@ module Tapioca module Commands class AnnotationsSpec < SpecWithProject + include Tapioca::Helpers::Test::Isolation include WebMock::API DUMMY_REPO_URI_1 = "https://my-private-repo-1"