Skip to content

Commit 2f94f80

Browse files
committed
fix: Require weakref
refactor: Change package_name to require_name
1 parent 9940acf commit 2f94f80

File tree

3 files changed

+39
-31
lines changed

3 files changed

+39
-31
lines changed

lib/appmap/config.rb

+35-30
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,17 @@
1212
module AppMap
1313
class Config
1414
# Specifies a code +path+ to be mapped.
15+
#
1516
# Options:
1617
#
1718
# * +gem+ may indicate a gem name that "owns" the path
18-
# * +package_name+ can be used to make sure that the code is required so that it can be loaded. This is generally used with
19+
# * +require_name+ can be used to make sure that the code is required so that it can be loaded. This is generally used with
1920
# builtins, or when the path to be required is not automatically required when bundler requires the gem.
2021
# * +exclude+ can be used used to exclude sub-paths. Generally not used with +gem+.
2122
# * +labels+ is used to apply labels to matching code. This is really only useful when the package will be applied to
2223
# specific functions, via TargetMethods.
2324
# * +shallow+ indicates shallow mapping, in which only the entrypoint to a gem is recorded.
24-
Package = Struct.new(:path, :gem, :package_name, :exclude, :labels, :shallow) do
25+
Package = Struct.new(:path, :gem, :require_name, :exclude, :labels, :shallow) do
2526
# This is for internal use only.
2627
private_methods :gem
2728

@@ -43,20 +44,20 @@ def shallow?
4344
class << self
4445
# Builds a package for a path, such as `app/models` in a Rails app. Generally corresponds to a `path:` entry
4546
# in appmap.yml. Also used for mapping specific methods via TargetMethods.
46-
def build_from_path(path, shallow: false, package_name: nil, exclude: [], labels: [])
47-
Package.new(path, nil, package_name, exclude, labels, shallow)
47+
def build_from_path(path, shallow: false, require_name: nil, exclude: [], labels: [])
48+
Package.new(path, nil, require_name, exclude, labels, shallow)
4849
end
4950

5051
# Builds a package for gem. Generally corresponds to a `gem:` entry in appmap.yml. Also used when mapping
5152
# a builtin.
52-
def build_from_gem(gem, shallow: true, package_name: nil, exclude: [], labels: [], optional: false, force: false)
53+
def build_from_gem(gem, shallow: true, require_name: nil, exclude: [], labels: [], optional: false, force: false)
5354
if !force && %w[method_source activesupport].member?(gem)
5455
warn "WARNING: #{gem} cannot be AppMapped because it is a dependency of the appmap gem"
5556
return
5657
end
5758
path = gem_path(gem, optional)
5859
if path
59-
Package.new(path, gem, package_name, exclude, labels, shallow)
60+
Package.new(path, gem, require_name, exclude, labels, shallow)
6061
else
6162
AppMap::Util.startup_message "#{gem} is not available in the bundle"
6263
end
@@ -81,7 +82,7 @@ def name
8182
def to_h
8283
{
8384
path: path,
84-
package_name: package_name,
85+
require_name: require_name,
8586
gem: gem,
8687
handler_class: handler_class.name,
8788
exclude: Util.blank?(exclude) ? nil : exclude,
@@ -117,11 +118,11 @@ def to_h
117118
# entry in appmap.yml. When the Config is initialized, each Function is converted into
118119
# a Package and TargetMethods. It's called a Function rather than a Method, because Function
119120
# is the AppMap terminology.
120-
Function = Struct.new(:package, :cls, :labels, :function_names, :builtin, :package_name) do # :nodoc:
121+
Function = Struct.new(:package, :cls, :labels, :function_names, :builtin, :require_name) do # :nodoc:
121122
def to_h
122123
{
123124
package: package,
124-
package_name: package_name,
125+
require_name: require_name,
125126
class: cls,
126127
labels: labels,
127128
functions: function_names.map(&:to_sym),
@@ -138,9 +139,9 @@ def to_h
138139
private_constant :MethodHook
139140

140141
class << self
141-
def package_hooks(gem_name, methods, handler_class: nil, package_name: nil)
142+
def package_hooks(gem_name, methods, handler_class: nil, require_name: nil)
142143
Array(methods).map do |method|
143-
package = Package.build_from_gem(gem_name, package_name: package_name, labels: method.labels, shallow: false, optional: true)
144+
package = Package.build_from_gem(gem_name, require_name: require_name, labels: method.labels, shallow: false, optional: true)
144145
next unless package
145146

146147
package.handler_class = handler_class if handler_class
@@ -164,14 +165,14 @@ def method_hook(cls, method_names, labels)
164165
method_hook('ActionView::PartialRenderer', :render, %w[mvc.view])
165166
],
166167
handler_class: AppMap::Handler::Rails::Template::RenderHandler,
167-
package_name: 'action_view'
168+
require_name: 'action_view'
168169
),
169170
package_hooks('actionview',
170171
[
171172
method_hook('ActionView::Resolver', %i[find_all find_all_anywhere], %w[mvc.template.resolver])
172173
],
173174
handler_class: AppMap::Handler::Rails::Template::ResolverHandler,
174-
package_name: 'action_view'
175+
require_name: 'action_view'
175176
),
176177
package_hooks('actionpack',
177178
[
@@ -181,7 +182,7 @@ def method_hook(cls, method_names, labels)
181182
method_hook('ActionDispatch::Cookies::CookieJar', %i[[]= clear update delete recycle], %w[http.session.write]),
182183
method_hook('ActionDispatch::Cookies::EncryptedCookieJar', %i[[]= clear update delete recycle], %w[http.cookie crypto.encrypt])
183184
],
184-
package_name: 'action_dispatch'
185+
require_name: 'action_dispatch'
185186
),
186187
package_hooks('cancancan',
187188
[
@@ -193,11 +194,11 @@ def method_hook(cls, method_names, labels)
193194
[
194195
method_hook('ActionController::Instrumentation', %i[process_action send_file send_data redirect_to], %w[mvc.controller])
195196
],
196-
package_name: 'action_controller'
197+
require_name: 'action_controller'
197198
)
198199
].flatten.freeze
199200

200-
OPENSSL_PACKAGES = ->(labels) { Package.build_from_path('openssl', package_name: 'openssl', labels: labels) }
201+
OPENSSL_PACKAGES = ->(labels) { Package.build_from_path('openssl', require_name: 'openssl', labels: labels) }
201202

202203
# Hook functions which are builtin to Ruby. Because they are builtins, they may be loaded before appmap.
203204
# Therefore, we can't rely on TracePoint to report the loading of this code.
@@ -210,25 +211,25 @@ def method_hook(cls, method_names, labels)
210211
TargetMethods.new(%i[decrypt], OPENSSL_PACKAGES.(%w[crypto.decrypt]))
211212
],
212213
'ActiveSupport::Callbacks::CallbackSequence' => [
213-
TargetMethods.new(:invoke_before, Package.build_from_gem('activesupport', force: true, package_name: 'active_support', labels: %w[mvc.before_action])),
214-
TargetMethods.new(:invoke_after, Package.build_from_gem('activesupport', force: true, package_name: 'active_support', labels: %w[mvc.after_action])),
214+
TargetMethods.new(:invoke_before, Package.build_from_gem('activesupport', force: true, require_name: 'active_support', labels: %w[mvc.before_action])),
215+
TargetMethods.new(:invoke_after, Package.build_from_gem('activesupport', force: true, require_name: 'active_support', labels: %w[mvc.after_action])),
215216
],
216-
'ActiveSupport::SecurityUtils' => TargetMethods.new(:secure_compare, Package.build_from_gem('activesupport', force: true, package_name: 'active_support/security_utils', labels: %w[crypto.secure_compare])),
217+
'ActiveSupport::SecurityUtils' => TargetMethods.new(:secure_compare, Package.build_from_gem('activesupport', force: true, require_name: 'active_support/security_utils', labels: %w[crypto.secure_compare])),
217218
'OpenSSL::X509::Certificate' => TargetMethods.new(:sign, OPENSSL_PACKAGES.(%w[crypto.x509])),
218-
'Net::HTTP' => TargetMethods.new(:request, Package.build_from_path('net/http', package_name: 'net/http', labels: %w[protocol.http]).tap do |package|
219+
'Net::HTTP' => TargetMethods.new(:request, Package.build_from_path('net/http', require_name: 'net/http', labels: %w[protocol.http]).tap do |package|
219220
package.handler_class = AppMap::Handler::NetHTTP
220221
end),
221-
'Net::SMTP' => TargetMethods.new(:send, Package.build_from_path('net/smtp', package_name: 'net/smtp', labels: %w[protocol.email.smtp])),
222-
'Net::POP3' => TargetMethods.new(:mails, Package.build_from_path('net/pop3', package_name: 'net/pop', labels: %w[protocol.email.pop])),
222+
'Net::SMTP' => TargetMethods.new(:send, Package.build_from_path('net/smtp', require_name: 'net/smtp', labels: %w[protocol.email.smtp])),
223+
'Net::POP3' => TargetMethods.new(:mails, Package.build_from_path('net/pop3', require_name: 'net/pop', labels: %w[protocol.email.pop])),
223224
# This is happening: Method send_command not found on Net::IMAP
224-
# 'Net::IMAP' => TargetMethods.new(:send_command, Package.build_from_path('net/imap', package_name: 'net/imap', labels: %w[protocol.email.imap])),
225+
# 'Net::IMAP' => TargetMethods.new(:send_command, Package.build_from_path('net/imap', require_name: 'net/imap', labels: %w[protocol.email.imap])),
225226
# 'Marshal' => TargetMethods.new(%i[dump load], Package.build_from_path('marshal', labels: %w[format.marshal])),
226227
'Psych' => [
227-
TargetMethods.new(%i[load load_stream parse parse_stream], Package.build_from_path('yaml', package_name: 'psych', labels: %w[format.yaml.parse])),
228-
TargetMethods.new(%i[dump dump_stream], Package.build_from_path('yaml', package_name: 'psych', labels: %w[format.yaml.generate])),
228+
TargetMethods.new(%i[load load_stream parse parse_stream], Package.build_from_path('yaml', require_name: 'psych', labels: %w[format.yaml.parse])),
229+
TargetMethods.new(%i[dump dump_stream], Package.build_from_path('yaml', require_name: 'psych', labels: %w[format.yaml.generate])),
229230
],
230-
'JSON::Ext::Parser' => TargetMethods.new(:parse, Package.build_from_path('json', package_name: 'json', labels: %w[format.json.parse])),
231-
'JSON::Ext::Generator::State' => TargetMethods.new(:generate, Package.build_from_path('json', package_name: 'json', labels: %w[format.json.generate])),
231+
'JSON::Ext::Parser' => TargetMethods.new(:parse, Package.build_from_path('json', require_name: 'json', labels: %w[format.json.parse])),
232+
'JSON::Ext::Generator::State' => TargetMethods.new(:generate, Package.build_from_path('json', require_name: 'json', labels: %w[format.json.generate])),
232233
}.freeze
233234

234235
attr_reader :name, :appmap_dir, :packages, :exclude, :swagger_config, :depends_config, :hooked_methods, :builtin_hooks
@@ -256,8 +257,8 @@ def initialize(name,
256257
functions.each do |func|
257258
package_options = {}
258259
package_options[:labels] = func.labels if func.labels
259-
package_options[:package_name] = func.package_name
260-
package_options[:package_name] ||= func.package if func.builtin
260+
package_options[:require_name] = func.require_name
261+
package_options[:require_name] ||= func.package if func.builtin
261262
hook = TargetMethods.new(func.function_names, Package.build_from_path(func.package, **package_options))
262263
if func.builtin
263264
@builtin_hooks[func.cls] ||= []
@@ -366,7 +367,11 @@ def load(config_data)
366367
shallow = package['shallow']
367368
# shallow is true by default for gems
368369
shallow = true if shallow.nil?
369-
Package.build_from_gem(gem, package_name: package['package'], exclude: package['exclude'] || [], shallow: shallow)
370+
371+
require_name = \
372+
package['package'] || #deprecated
373+
package['require_name']
374+
Package.build_from_gem(gem, require_name: require_name, exclude: package['exclude'] || [], shallow: shallow)
370375
else
371376
Package.build_from_path(path, exclude: package['exclude'] || [], shallow: package['shallow'])
372377
end

lib/appmap/event.rb

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# frozen_string_literal: true
22

3+
require 'weakref'
4+
35
module AppMap
46
module Event
57
@@id_counter = 0

lib/appmap/hook.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ def hook_builtins
8989

9090
config.builtin_hooks.each do |class_name, hooks|
9191
Array(hooks).each do |hook|
92-
require hook.package.package_name if hook.package.package_name && hook.package.package_name != 'ruby'
92+
require hook.package.require_name if hook.package.require_name && hook.package.require_name != 'ruby'
93+
9394
Array(hook.method_names).each do |method_name|
9495
method_name = method_name.to_sym
9596
base_cls = class_from_string.(class_name)

0 commit comments

Comments
 (0)