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
Sort requirements in Gem::Requirement to succeed comparison with different order #3889
Sort requirements in Gem::Requirement to succeed comparison with different order #3889
Conversation
@toy I couldn't repro the problem on my linux box with recent versions of bundler & rubygems. Maybe this is fixed in recent versions? We started sorting requirements here, but then reverted it here. Reason for the revert was that it wasn't necessary for any of the problems we wanted to fix, and that it was pointed out that requirement ordering inside gemspecs might be there for a reason. |
@deivid-rodriguez docker run --rm -i ruby:2.7 bash <<BASH
gem install image_optim_pack --platform ruby
cat <<GEMFILE > Gemfile
source 'https://rubygems.org'
gem 'image_optim'
GEMFILE
bundle
cat <<GEMFILE > Gemfile
source 'https://rubygems.org'
gem 'image_optim'
gem 'image_optim_pack'
GEMFILE
bundle
BASH For me output is (note the
I understand #2627, the order of requirements should not randomly change in the output, but the issue is still there and as far as I know the order of requirements is not important for comparison. I'll try to change my PR to fix the issue while persisting the order of requirements in the output. |
@toy Thanks for the docker repro, issue confirmed on my side 👍. Your suggestion also looks perfectly fine, as long as the original order is preserved for other things, it sounds perfectly reasonable to ignore it for requirement comparison 👍. |
@deivid-rodriguez Restored changes in |
I'm not sure either. Does memoizing do the trick? Something else that would be nice is to have a test in |
It should, I had an impression that class is mutable, but reading through it I didn't find mutating methods.
I'll try to create a test, |
…d multiple times during same comparison
…fferent requirements order
069b87d
to
53c9377
Compare
The test succeeds if I install rubygems from the branch (bumping version so condition is met) and fails with rubygems |
Nice @toy, thanks! I said there was no need to backport the fix itself to diff --git a/bundler/lib/bundler/rubygems_ext.rb b/bundler/lib/bundler/rubygems_ext.rb
index 1d3bdc5565..76b779b5bb 100644
--- a/bundler/lib/bundler/rubygems_ext.rb
+++ b/bundler/lib/bundler/rubygems_ext.rb
@@ -129,6 +129,27 @@ def to_lock
end
end
+ unless Requirement.instance_methods(false).include?(:_sorted_requirements)
+ module OrderIndependentComparison
+ def ==(other)
+ rubygems_comparison = super
+ return rubygems_comparison unless rubygems_comparison == false
+
+ _sorted_requirements == other._sorted_requirements
+ end
+
+ protected
+
+ def _sorted_requirements
+ @_sorted_requirements ||= requirements.sort_by(&:to_s)
+ end
+ end
+
+ class Requirement
+ prepend OrderIndependentComparison
+ end
+ end
+
class Platform
JAVA = Gem::Platform.new("java") unless defined?(JAVA)
MSWIN = Gem::Platform.new("mswin32") unless defined?(MSWIN) |
@deivid-rodriguez I was not sure about calling |
4a26833
to
6981e06
Compare
6981e06
to
c33914f
Compare
Force-pushed to keep history clean: found a bug and memoized |
Smart! Thanks for fixing my buggy code 😅! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
…ms < 3.2.0 is dropped
Thanks @toy!! 💜 |
Sort requirements in Gem::Requirement to succeed comparison with different order (cherry picked from commit a452809)
Sort requirements in Gem::Requirement to succeed comparison with different order (cherry picked from commit a452809)
Sort requirements in Gem::Requirement to succeed comparison with different order (cherry picked from commit a452809)
Sort requirements in Gem::Requirement to succeed comparison with different order (cherry picked from commit a452809)
Sort requirements in Gem::Requirement to succeed comparison with different order (cherry picked from commit a452809)
Sort requirements in Gem::Requirement to succeed comparison with different order (cherry picked from commit a452809)
Description:
This PR ensures that comparison of requirements works properly when order or requirements is different.
Before this PR following two dependencies are considered different:
What was the end-user or developer problem that led to this PR?
When debugging an issue toy/image_optim_pack#20 I've found that in certain conditions platform-specific gem is not selected and an invalid warning is displayed due to comparison of equal dependencies failing. I was able to reproduce it by running following commands:
What is your fix for the problem, implemented in this PR?
Debugging the issue I found that inside
Bundler::LazySpecification#__materialize__
the comparison of dependencies failed due to different order of requirements which led to the warning suggesting that ruby and platform specific gems have different dependencies and selected ruby version of the gem. Suggested change fixes the behaviour.Tasks:
I will abide by the code of conduct.