spring
spring copied to clipboard
Rails.env not set correctly in initializers when using rails console
From @thinkerbot on August 12, 2014 18:30
I observed that Rails.env is not set correctly within initializers when using rails console -e. For example:
$ rails -v
Rails 4.1.4
$ rails new example
$ cd example
$ cat > config/initializers/example.rb <<"DOC"
File.open("example.env", "w") {|io| io.puts Rails.env }
DOC
$ printf "puts Rails.env; exit" | rails console -e test
Loading test environment (Rails 4.1.4)
Switch to inspect mode.
puts Rails.env; exit
test
$ cat example.env
development
You can see that the initializer is picking up the 'development' as a default but the console eventually sets Rails.env to test before the prompt appears. You can work around this issue by using an ENV variable instead of the option.
$ rm example.env
$ printf "puts Rails.env; exit" | RAILS_ENV=test rails console
Loading test environment (Rails 4.1.4)
Switch to inspect mode.
puts Rails.env; exit
test
$ cat example.env
test
Copied from original issue: rails/rails#16479
From @m5rk on August 22, 2014 20:17
This appears to have been fixed on master (4.2.0 alpha):
$ rails -v
Rails 4.2.0.alpha
$ bundle exec rails console -e test
Loading test environment (Rails 4.2.0.alpha)
awesome_print gem not found. Try typing 'gem install awesome_print' to get super-fancy output.
irb(main):001:0> exit
$ cat example.env
test
From @hannestyden on September 2, 2014 2:25
I can reproduce the problem on current edge.
$ ruby -v
ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-darwin13.0]
$ bundle exec rails -v
Rails 4.2.0.beta1
$ cat Gemfile.lock | grep 'rails/rails.git' -A 1
remote: git://github.com/rails/rails.git
revision: c6f9cec1bee6b722088119ae14c71a32c1eed914
$ cat > config/initializers/example.rb <<'DOC'
$rails_env = Rails.env
DOC
# As `-e` flag, does not work as documented:
$ echo 'puts "Reported: #{Rails.env}"; puts "Actual: #{$rails_env}"; exit' | bundle exec rails console -e test
Loading test environment (Rails 4.2.0.beta1)
Switch to inspect mode.
001:0> puts "Reported: #{Rails.env}"; puts "Actual: #{$rails_env}"; exit
Reported: test
Actual: development
# As positional flag, works as documented:
$ echo 'puts "Reported: #{Rails.env}"; puts "Actual: #{$rails_env}"; exit' | bundle exec rails console test
Loading test environment (Rails 4.2.0.beta1)
Switch to inspect mode.
001:0> puts "Reported: #{Rails.env}"; puts "Actual: #{$rails_env}"; exit
Reported: test
Actual: test
From @dgobaud on September 12, 2014 6:11
I'm not sure this is the same problem but I was having issues with environment variables accessed via ENV were not working correctly in initializers/* files. I figured out the problem was spring. Setting DISABLE_SPRING=true fixes everything. The problem was spring was loading env vars overriding what I specified on the command line.
From @steakknife on March 8, 2015 7:55
Setting Rails.env earlier along with RAILS_ENV in railties/lib/rails/commands/commands_tasks.rb seems to do the trick, as opposed to waiting for set_environment! later on. (PR #19255)
From @effkay on March 18, 2015 18:57
I just ran into this issue when upgrading a rails app from 4.0.3 to 4.2.0.
Once I changed bin/rails to the version proposed by rake rails:update I could not reproduce the issue anymore.
#!/usr/bin/env ruby
- begin
+ APP_PATH = File.expand_path('../../config/application', __FILE__)
- load File.expand_path('../spring', __FILE__)
- rescue LoadError
- end
- APP_PATH = File.expand_path('../../config/application', __FILE__)
require_relative '../config/boot'
require 'rails/commands'
From @jone on March 18, 2015 19:53
I can reproduce the problem with rails 4.2.0 when spring is installed too, with the setup as described in the pull request text by @thinkerbot
$ ./bin/spring stop
$ printf "puts Rails.env; exit" | rails console -e test
Loading test environment (Rails 4.2.0)
Switch to inspect mode.
puts Rails.env; exit
test
$ cat example.env
test
$ ./bin/spring status
Spring is running:
84769 spring server | example | started 1 min ago o
84771 spring app | example | started 1 min ago | test mode
$ printf "puts Rails.env; exit" | rails console -e development
Loading development environment (Rails 4.2.0)
Switch to inspect mode.
puts Rails.env; exit
development
$ cat example.env
development
$ ./bin/spring status
Spring is running:
85094 spring server | example | started 30 secs ago
85095 spring app | example | started 30 secs ago | test mode
85184 spring app | example | started 14 secs ago | development mode
$ printf "puts Rails.env; exit" | rails console -e test
Loading test environment (Rails 4.2.0)
Switch to inspect mode.
puts Rails.env; exit
test
$ cat example.env
development
The last line should be test. The problem seems to be in spring, it does not always respect the -e argument properly.
Ok. Lets move this discussion to sprint issue tracker.
From @effkay on March 18, 2015 20:5
Yep ignore my diff. Apparently ran into another issue where spring stuff is missing from bin/rails even if it is in the Gemfile ;)
From @pehrlich on October 20, 2015 20:31
What's the status of this issue? Perhaps you could provide a link to the issue in your sprint issue tracker?
It appears not fixed in 4.2.4
@pehrlich there is no open issue for that on the spring issue tracker yet. Mind to open?
From @pehrlich on October 20, 2015 20:44
My apologies, I'm confused here. According to the contribution docs, the Github rails/rails issue tracker is the correct place to report bugs. Perhaps this one should be re-opened?
It is the correct place if the issue is for Rails but this one if about spring. I'll just move this issue. Wait a second.