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

Rails env for empty string env vars #27399

Merged
merged 1 commit into from
Jan 4, 2017
Merged

Rails env for empty string env vars #27399

merged 1 commit into from
Jan 4, 2017

Conversation

sinogermany
Copy link

@sinogermany sinogermany commented Dec 19, 2016

The story:

I've got a weird issue where Rails.application.config_for(:app_config) returns nil.
So I looked into the console and found out that Rails.env is an empty string.
That was because I'm using docker-compose and it's setting RAILS_ENV to an empty string because I specified in docker-compose.yml but not set in my local env vars.
That was an easy fix on my side but I always thought rails handles that edge case.
That's why I'm submitting this PR to give rails the ability to fall back to development mode when this happens.

The changes:

I've added 2 tests with RAILS_ENV and RACK_ENV being set to an empty string and Rails.env should be development. Also added the message to CHANGELOG.md.

P.S.

This is my first commit to rails repo and I tried to follow the guidelines for contributing. I skipped some steps that seems irrelevant but if I'm missing any step please let me know.

Daniel

@rails-bot
Copy link

Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @matthewd (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

This repository is being automatically checked for code quality issues using Code Climate. You can see results for this analysis in the PR status below. Newly introduced issues should be fixed before a Pull Request is considered ready to review.

Please see the contribution instructions for more information.

@sinogermany
Copy link
Author

sinogermany commented Dec 19, 2016

@matthewd Travis and Code Climate both display the lovely green color now. Let me know if there's anything missing for this PR :)

*Daniel Deng*

* Make `Rails.env` fall back to `development` when `RAILS_ENV` and `RACK_ENV` is an empty string.

Copy link
Member

Choose a reason for hiding this comment

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

Your name should go on the bottom instead of the top of the change notice.

@@ -78,6 +78,18 @@ def restore_default_config
end
end

test "Rails.env falls back to development if RAILS_ENV == '' and RACK_ENV == nil" do
Copy link
Member

Choose a reason for hiding this comment

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

With tests I think it's usually better to write out what you're expecting versus writing code. So in that case this would be "Rails.env falls back to development if RAILS_ENV is blank and RACK_ENV is nil"

@sinogermany
Copy link
Author

@eileencodes thanks for your review comments, they make sense to me. Please see the extra commit e3bcbdc . Cheers!

@sinogermany
Copy link
Author

sinogermany commented Dec 20, 2016

hm... there's a travis error for mysql2, not sure what happened? I don't think it's relevant to my changes though, coz I can't see the commit e3bcbdc breaking anything. Could anyone help and have a look at that error please? Thanks heaps!

/home/travis/build/rails/rails/vendor/bundle/ruby/2.3.0/gems/mysql2-0.4.5/lib/mysql2/client.rb:120:in `_query': Mysql2::Error: Cannot load from mysql.proc. The table is probably corrupted: DROP PROCEDURE IF EXISTS ten; (ActiveRecord::StatementInvalid)

@maclover7
Copy link
Contributor

@sinogermany restarted the failing build, is green now

@sinogermany
Copy link
Author

@sinogermany restarted the failing build, is green now

Thanks @maclover7 !
Anyone who has any other questions feel free to let me know.

Cheers,

Daniel

@@ -67,7 +67,7 @@ def root
# Rails.env.development? # => true
# Rails.env.production? # => false
def env
@_env ||= ActiveSupport::StringInquirer.new(ENV["RAILS_ENV"] || ENV["RACK_ENV"] || "development")
@_env ||= ActiveSupport::StringInquirer.new(ENV["RAILS_ENV"].presence || ENV["RACK_ENV"].presence || "development")
Copy link
Member

Choose a reason for hiding this comment

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

presence is not present in this file. We need to require it. The test suite is passing because some of part of the test suite is requiring it and it is being used.

Copy link
Author

Choose a reason for hiding this comment

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

Thanks @rafaelfranca . Nice catch! I've required the file now, please see a1c0cca . Cheers!

Copy link
Member

@rafaelfranca rafaelfranca left a comment

Choose a reason for hiding this comment

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

Could you squash your commit?

@sinogermany
Copy link
Author

Could you squash your commit?

@rafaelfranca done. Thanks for the reminding :)

@rafaelfranca
Copy link
Member

rafaelfranca commented Jan 4, 2017

Now you have two commits again 😄. Don't worry with conflicts after you squash, I can solve them.

@sinogermany
Copy link
Author

Now you have two commits again 😄. Don't worry with conflicts after you squash, I can solve them.

LOL I removed the conflict merging commit, hopefully I haven't done something that bothers you agian :-D

rafaelfranca added a commit that referenced this pull request Jan 4, 2017
…-env-vars

Rails env for empty string env vars
@rafaelfranca
Copy link
Member

Merged in 50cb1b6 and backported in 807b0cb

@rafaelfranca rafaelfranca merged commit 498370c into rails:master Jan 4, 2017
rafaelfranca added a commit that referenced this pull request Jan 4, 2017
…-env-vars

Rails env for empty string env vars
kamipo added a commit to kamipo/rails that referenced this pull request May 24, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants