Skip to content

Commit

Permalink
Merge pull request #2595 from DataDog/ivoanjo/fix-google-protobuf-det…
Browse files Browse the repository at this point in the history
…ection

Fix profiler wrongly detecting that google-protobuf is installed
  • Loading branch information
ivoanjo committed Feb 3, 2023
2 parents 2f31cd8 + 47340ba commit bbdb316
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/datadog/profiling.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def self.start_if_enabled
# NOTE: On environments where protobuf is already loaded, we skip the check. This allows us to support environments
# where no Gem.loaded_version is NOT available but customers are able to load protobuf; see for instance
# https://github.com/teamcapybara/capybara/commit/caf3bcd7664f4f2691d0ca9ef3be9a2a954fecfb
if !defined?(::Google::Protobuf) && Gem.loaded_specs['google-protobuf'].nil?
if !protobuf_already_loaded? && Gem.loaded_specs['google-protobuf'].nil?
"Missing google-protobuf dependency; please add `gem 'google-protobuf', '~> 3.0'` to your Gemfile or gems.rb file"
end
end
Expand All @@ -74,12 +74,16 @@ def self.start_if_enabled
# See above for why we skip the check when protobuf is already loaded; note that when protobuf was already loaded
# we skip the version check to avoid the call to Gem.loaded_specs. Unfortunately, protobuf does not seem to
# expose the gem version constant elsewhere, so in that setup we are not able to check the version.
if !defined?(::Google::Protobuf) && Gem.loaded_specs['google-protobuf'].version < GOOGLE_PROTOBUF_MINIMUM_VERSION
if !protobuf_already_loaded? && Gem.loaded_specs['google-protobuf'].version < GOOGLE_PROTOBUF_MINIMUM_VERSION
'Your google-protobuf is too old; ensure that you have google-protobuf >= 3.0 by ' \
"adding `gem 'google-protobuf', '~> 3.0'` to your Gemfile or gems.rb file"
end
end

private_class_method def self.protobuf_already_loaded?
defined?(::Google::Protobuf) && !defined?(::Protobuf)
end

private_class_method def self.protobuf_failed_to_load?
unless protobuf_loaded_successfully?
'There was an error loading the google-protobuf library; see previous warning message for details'
Expand Down
10 changes: 10 additions & 0 deletions spec/datadog/profiling_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,16 @@
end

it { is_expected.to be nil }

context "but it's the protobuf/cucumber-protobuf gem instead of google-protobuf" do
include_context 'loaded gems', :'google-protobuf' => nil

before do
stub_const('::Protobuf', Module.new)
end

it { is_expected.to include 'Missing google-protobuf' }
end
end
end
end
Expand Down

0 comments on commit bbdb316

Please sign in to comment.