{"action":"created","issue":{"url":"https://api.github.com/repos/rails/spring/issues/439","labels_url":"https://api.github.com/repos/rails/spring/issues/439/labels{/name}","comments_url":"https://api.github.com/repos/rails/spring/issues/439/comments","events_url":"https://api.github.com/repos/rails/spring/issues/439/events","html_url":"https://github.com/rails/spring/issues/439","id":112457175,"number":439,"title":"Rails.env not set correctly in initializers when using rails console","user":{"login":"rafaelfranca","id":47848,"avatar_url":"https://avatars.githubusercontent.com/u/47848?v=3","gravatar_id":"","url":"https://api.github.com/users/rafaelfranca","html_url":"https://github.com/rafaelfranca","followers_url":"https://api.github.com/users/rafaelfranca/followers","following_url":"https://api.github.com/users/rafaelfranca/following{/other_user}","gists_url":"https://api.github.com/users/rafaelfranca/gists{/gist_id}","starred_url":"https://api.github.com/users/rafaelfranca/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rafaelfranca/subscriptions","organizations_url":"https://api.github.com/users/rafaelfranca/orgs","repos_url":"https://api.github.com/users/rafaelfranca/repos","events_url":"https://api.github.com/users/rafaelfranca/events{/privacy}","received_events_url":"https://api.github.com/users/rafaelfranca/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/rails/spring/labels/railties","name":"railties","color":"ededed"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":5,"created_at":"2015-10-20T20:48:48Z","updated_at":"2015-10-20T20:48:51Z","closed_at":null,"body":"_From @thinkerbot on August 12, 2014 18:30_\n\nI observed that `Rails.env` is not set correctly within initializers when using `rails console -e`.  For example:\r\n\r\n    $ rails -v\r\n    Rails 4.1.4\r\n    $ rails new example\r\n    $ cd example\r\n    $ cat > config/initializers/example.rb <<\"DOC\"\r\n    File.open(\"example.env\", \"w\") {|io| io.puts Rails.env }\r\n    DOC\r\n    $ printf \"puts Rails.env; exit\" | rails console -e test\r\n    Loading test environment (Rails 4.1.4)\r\n    Switch to inspect mode.\r\n    puts Rails.env; exit\r\n    test\r\n    $ cat example.env\r\n    development\r\n\r\nYou 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.\r\n\r\n    $ rm example.env\r\n    $ printf \"puts Rails.env; exit\" | RAILS_ENV=test rails console\r\n    Loading test environment (Rails 4.1.4)\r\n    Switch to inspect mode.\r\n    puts Rails.env; exit\r\n    test\r\n    $ cat example.env\r\n    test\n\n_Copied from original issue: rails/rails#16479_"},"comment":{"url":"https://api.github.com/repos/rails/spring/issues/comments/149697597","html_url":"https://github.com/rails/spring/issues/439#issuecomment-149697597","issue_url":"https://api.github.com/repos/rails/spring/issues/439","id":149697597,"user":{"login":"rafaelfranca","id":47848,"avatar_url":"https://avatars.githubusercontent.com/u/47848?v=3","gravatar_id":"","url":"https://api.github.com/users/rafaelfranca","html_url":"https://github.com/rafaelfranca","followers_url":"https://api.github.com/users/rafaelfranca/followers","following_url":"https://api.github.com/users/rafaelfranca/following{/other_user}","gists_url":"https://api.github.com/users/rafaelfranca/gists{/gist_id}","starred_url":"https://api.github.com/users/rafaelfranca/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rafaelfranca/subscriptions","organizations_url":"https://api.github.com/users/rafaelfranca/orgs","repos_url":"https://api.github.com/users/rafaelfranca/repos","events_url":"https://api.github.com/users/rafaelfranca/events{/privacy}","received_events_url":"https://api.github.com/users/rafaelfranca/received_events","type":"User","site_admin":false},"created_at":"2015-10-20T20:48:51Z","updated_at":"2015-10-20T20:48:51Z","body":"_From @effkay on March 18, 2015 18:57_\n\nI just ran into this issue when upgrading a rails app from 4.0.3 to 4.2.0.\r\n\r\nOnce I changed `bin/rails` to the version proposed by `rake rails:update` I could not reproduce the issue anymore.\r\n\r\n```diff\r\n  #!/usr/bin/env ruby\r\n- begin\r\n+ APP_PATH = File.expand_path('../../config/application', __FILE__)\r\n-   load File.expand_path('../spring', __FILE__)\r\n- rescue LoadError\r\n- end\r\n- APP_PATH = File.expand_path('../../config/application',  __FILE__)\r\n  require_relative '../config/boot'\r\n  require 'rails/commands'\r\n```"}}