-
Notifications
You must be signed in to change notification settings - Fork 37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Warnings even if the right json version is available #27
Comments
Proposed a patch in #28 |
The JSON::VERSION check up front is intentional. In Ruby 1.9.3, it does not matter if you have json version 1.8.1+ installed. Calling a blind require 'json'
require 'jmespath' Then they will likely get a warning. Can you try adding the following to your Gemfile to see if this fixes the issue? gem 'json', '>= 1.8.1' |
Hi Trevor, No I can't I'm using this in a script (via calls to the aws_sdk) to set a puppet fact. As I explained above I already have the json gen installed in the right version. Did you try to reproduce it? Thanks. |
With Ruby 1.9.3-p551 and the following gems installed:
I ran the following code: require 'json'
JSON::VERSION
#=> 1.5.5 Now, in the same process, I ran the code from the pull request: def fallback_gem
begin
# Fallback on the json_pure gem dependency.
gem('json_pure', '>= 1.8.1')
require 'json'
rescue Gem::LoadError
if Object.const_defined?(:JSON) && JSON::VERSION < '1.8.1'
warn("WARNING: jmespath gem requires json gem >= 1.8.1; json #{JSON::VERSION} already loaded")
else
warn("ERROR: unable to load any json library")
end
end
end
begin
# Attempt to load the native version if available, not availble
# by default for Ruby 1.9.3.
gem('json', '>= 1.8.1')
require 'json'
rescue Gem::LoadError
fallback_gem
end
JSON::VERSION
#=> 1.5.5 The code fails to produce the desired warning because the code in the first (bottom) begin rescue block will not raise. It simply returns false because Also, care is required to ensure that you do not call |
Hi @trevorrowe, Using a freshly installed 1.9.3 rvm environment:
I did 4 tests (and a code update proposition after that): Content of the script used it's exactly the same version as you, just printing the last line:
Test 1: No Json gem with the right version (only the 1.5.5 that comes with ruby) and no json_pure eitherThe script cannot find the JSON constant, so the gem 1.5.5 is not loaded.
Test 2: with json_pure 1.8.1 installedThe script loads the json_pure gem:
Test 3: with json = 1.8.2
Test 4: with a require json done for an older gem version 1stNow, if in my script I load the gem 1.5.5 first, yes, I get a bad behavior. Is that the test case you are talking about? Adding those 2 lines at the top of the script:
Gives me indeed the result you were talking about:
Code updateBut that's easily fixable. I just need to add the following code to unload the gem and corresponding constant if the module has been loaded in an older version (and add "in the right version >= 1.8.1 required" to the error message):
Which would give us something like:
Would you be ok with this behavior and code? Note that your gemspec requires json_pure so if the jmespath gem is installed correctly, you should at least have json_pure available in the right version. Thanks |
Yes, the scenario I'm concerned about is when JSON is loaded prior to jmespath being loaded. I'm not sure how I feel about removing a loaded version of JSON. I would prefer to avoid such tactics. These things tend to alway go bad in the end. What issue do you have with the current behavior? Is it the warning that you concerned with? The warning can be improved with guidance on how to resolve this. |
My issue is that now, when I'm running puppet, it loads custom facts that use the aws-sdk gem (which calls jmespath). Somewhere in the facts or in a 3rd party library, the I don't see any other way than unloading the lib and reloading it with the right version. As I was saying earlier, the json_pure >= 1.8.1 is a prerequisite of this gem anyway, so it should be at least available. |
Closed by #35. We've removed the dependency on |
Hi guys,
I'm hitting this warning:
Even if the right version of json/json_pure is available:
Current ruby version:
Which comes from https://github.com/jmespath/jmespath.rb/blob/master/lib/jmespath.rb
Doing some testing I see that if I don't specify any gem, it shows a json version 1.8.0:
The thing is that if I load json_pure, I get the right JSON::VERSION:
And if I call for the json gem directly I also have the right version:
Shouldn't we try to load the json gem, fallback to json_pure and warn if both gem loads fail?
I can push a PR for that if you want, should be a quite easy fix, but I'd like your opinion 1st. :)
Thanks
Joseph
The text was updated successfully, but these errors were encountered: