Closed tdeo closed 3 days ago
I don't see any test, so it's hard to confirm it's indeed the cause.
I tried to write some tests but unfortunately Kernel.remove_const(:Date)
is impossible because Date
is a built-in. I meant in the PR description that if you try to run the test suite without the autoload
statement, you'll get a failure.
You would also get failures if that statement is within Psych::ScalarScanner
, in which case it's trying to autoload Psych::ScalarScanner::Date
instead of ::Date
You can probably use a subprocess.
You can probably use a subprocess.
I was able indeed to add a test case with this technique, thanks for the suggestion
There's a test helper for that: assert_separatly
.
But no worries, I'll take care of it. I need to run some errand but I'll fix this in a couple hours.
Also I think we should just stop lazy requiring libraries like that, it's stupid.
I updated your PR.
cc @tenderlove what do you think?
Thanks for wrapping it up
Right, sorry Aaron, I should have explained.
In YAMLTree#visit_String
, there's this condition to figure if a string should be quoted:
elsif not String === @ss.tokenize(o) or /\A0[0-7]*[89]/.match?(o)
style = Nodes::Scalar::SINGLE_QUOTED
So we end up in:
class_loader.date.strptime(string, '%F', Date::GREGORIAN)
Which further end up trying ClassLoader#resolve
of "Date", and this uses path2class
, so it's resolved as essentially ::Date
, so the scoped autoload
I introduced isn't triggered in this case.
Hence why the initial version of this PR was to move the autoload at the top level, but at that point I think we should just eager load.
@byroot ah, gotcha. I somewhat worry this might have downstream effects (maybe on Bundler or RubyGems). But I like the change so I'll merge it.
Fixes https://github.com/ruby/psych/issues/694.
When picking up only the test changes (and not
require 'date'
explicitly anymore), such failures start to appear:The
autoload :Date
statement gets moved to a more appropriate file (the one actually loading the constant), and moved to the top-level so it requires::Date
and not try to requirePsych::ClassLoader::Date
Performance-wise, reusing the benchmark from https://github.com/ruby/psych/commit/06db36fa3a849f229e2cbc7b495d142ff58a1f89#diff-6a459e056cadf37665f54005bd2dde09d9ba8e66c9807eb0dc67145f9b841771R7, it seems to further improve performance quite unsignificantly:
With those changes:
On current master: