Skip to content
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

Fix safe_constantize to not raise a LoadError. #34892

Merged
merged 1 commit into from
Jan 10, 2019

Conversation

kbrock
Copy link
Contributor

@kbrock kbrock commented Jan 7, 2019

Summary

There was an issues when using safe_constantize on a string that has the wrong case.

File em.rb defines EM.
"Em".safe_constantize causes a little confusion with the autoloader.
The autoloader finds file "em.rb", expecting it to define Em, but Em is not defined.
The autoloader raises a LoadError, which is good, but safe_constantize is defined to return nil when a class is not found.

Before

"Em".safe_constantize
LoadError: Unable to autoload constant Em, expected rails/activesupport/test/autoloading_fixtures/em.rb to define it

After

"Em".safe_constantize
# => nil

@@ -328,6 +328,8 @@ def safe_constantize(camel_cased_word)
e.name.to_s == camel_cased_word.to_s)
rescue ArgumentError => e
raise unless /not missing constant #{const_regexp(camel_cased_word)}!$/.match?(e.message)
rescue LoadError => e
rause unless /Unable to autoload constant/.match?(e.message)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rause?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might also want to add #{const_regexp(camel_cased_word)} to the end of this regex to ensure we're catching the right load error. Might be a good idea to test this actually raises now too seeing how CI is passing with a typo 🤔

Copy link
Member

@gmcgibbon gmcgibbon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this makes sense. 👍 Please add a changelog entry to ActiveSupport.

@kbrock
Copy link
Contributor Author

kbrock commented Jan 8, 2019

Thank you @gmcgibbon and @djberg96

  • added a test to catch the s/rause/raise/ fail (so it is properly tested)
  • added #{const_regexp(camel_cased_word)} to regular expression to narrow down exception from the autoloader

@djberg96
Copy link

djberg96 commented Jan 8, 2019

👍

Copy link
Member

@gmcgibbon gmcgibbon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still needs a changelog entry here. Other than that, LGTM! 👍

### Summary

There was an issues when using `safe_constantize` on a string that has
the wrong case.

File `em.rb` defines `EM`.
`"Em".safe_constantize` causes a little confusion with the autoloader.
The autoloader finds file "em.rb",
expecting it to define `Em`, but `Em` is not defined.
The autoloader raises a `LoadError`, which is good,
But `safe_constantize` is defined to return `nil` when a class is not found.

### Before

```
"Em".safe_constantize
LoadError: Unable to autoload constant Em, \
expected rails/activesupport/test/autoloading_fixtures/em.rb to define it
```

### After

```
"Em".safe_constantize
# => nil
```
@kbrock
Copy link
Contributor Author

kbrock commented Jan 9, 2019

fixed Changelog conflict...

@gmcgibbon thank you so much for your help (and your patience for having to repeat that)
@djberg96 thanks for the ping and keeping me honest

@kbrock
Copy link
Contributor Author

kbrock commented Jan 9, 2019

odd collate error for mysql (that I haven't seen before)
kicking

@kbrock kbrock closed this Jan 9, 2019
@kbrock kbrock reopened this Jan 9, 2019
@rafaelfranca rafaelfranca merged commit ebda02d into rails:master Jan 10, 2019
rafaelfranca added a commit that referenced this pull request Jan 10, 2019
Fix safe_constantize to not raise a LoadError.
@kbrock kbrock deleted the safer_safe_constantize branch January 11, 2019 14:48
@kbrock
Copy link
Contributor Author

kbrock commented Jan 11, 2019

Thanks for all the help on this PR, and rails in general. It is hard work maintaining OSS.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants