Module: Datadog::CI::SourceCode::ConstantResolver

Defined in:
lib/datadog/ci/source_code/constant_resolver.rb

Overview

ConstantResolver resolves Ruby constant names to their source file locations.

This module uses Object.const_source_location to find where a constant is defined. Constants defined in C extensions or built-in Ruby classes have no source location.

This module mirrors the C implementation in datadog_common.c (dd_ci_resolve_const_to_file).

Class Method Summary collapse

Class Method Details

.resolve_path(constant_name) ⇒ String?

Resolve a constant name to its source file path.

Parameters:

  • The fully qualified constant name (e.g., “Foo::Bar::Baz”)

Returns:

  • The absolute file path where the constant is defined, or nil if not found



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/datadog/ci/source_code/constant_resolver.rb', line 17

def self.resolve_path(constant_name)
  return nil unless constant_name.is_a?(String)
  return nil if constant_name.empty?

  source_location = safely_get_const_source_location(constant_name)
  return nil unless source_location.is_a?(Array) && !source_location.empty?

  filename = source_location[0]
  return nil unless filename.is_a?(String)

  filename
end

.safely_get_const_source_location(constant_name) ⇒ Array?

Safely get source location for a constant, returning nil on any exception. This handles cases like anonymous classes, C-defined constants, etc.

Parameters:

  • The constant name to look up

Returns:

  • The [filename, lineno] array or nil



35
36
37
38
39
# File 'lib/datadog/ci/source_code/constant_resolver.rb', line 35

def self.safely_get_const_source_location(constant_name)
  Object.const_source_location(constant_name)
rescue
  nil
end