-
-
Notifications
You must be signed in to change notification settings - Fork 122
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
Nondeterministic order of MIME::Types.type_for #148
Comments
Thanks for the report. I don’t have access to a Windows machine to do this testing on. The implementation of MIME::Types#type_for is currently this: def type_for(filename)
Array(filename).flat_map { |fn|
@extension_index[fn.chomp.downcase[/\.?([^.]*?)$/, 1]]
}.compact.inject(Set.new, :+).sort { |a, b|
a.priority_compare(b)
}
end Can you try this implementation instead? def type_for(filename)
Array(filename).flat_map { |fn|
@extension_index[fn.chomp.downcase[/\.?([^.]*?)$/, 1]]
}.compact.uniq.sort { |a, b|
a.priority_compare(b)
}
end I’m not necessarily expecting any difference |
I had to tweak your suggested variant a bit: def type_for(filename)
Array(filename).flat_map { |fn|
@extension_index[fn.chomp.downcase[/\.?([^.]*?)$/, 1]].to_a
}.compact.uniq.sort { |a, b|
a.priority_compare(b)
}
end As you can see here, the picture is the same - Rubies 2.4+ on Windows get different order. |
I believe the problem is in
There are ways to make it stable: https://stackoverflow.com/a/15442966 |
I found an even more interesting information about |
Good find. I would happily accept a PR that adds stability to the sort, but I have no time to do this myself. |
Okay, I'll work on a PR. Just wanted to be sure that it is accepted as a valid bug. |
It’s supposed to be stable. |
Just found this ticket due to the differing order (for "file.mp4" in my case) of returned types. I see there's a PR but a comment on it saying something along the lines of "this might not be performant so I won't merge it". Any chance of adding a flag / option to types_for / of to get this stable sort behavior in a way that's also backwards compatible? |
I’ll need to look through the PR again. |
I ran a simple test case on multiple Ruby implementations:
On Travis CI, it passes with whole range of Rubies that you test against.
But it fails on my Windows 10 machine because actual return value is reversed (first video/webm, second audio/webm):
My Ruby is:
I also ran this test on GitHub Actions and it fails on Windows too. But note that Ruby--2.2 and 2.3 did pass on Windows.
UPD: I messed up everything a bit so had to update PR since it was initially created.
The text was updated successfully, but these errors were encountered: