RACK_ENV: It's not *for* you

Posted: Sun, 13 October 2013 | permalink | 1 Comment

(With apologies to Penny Arcade)

I’m probably the last person on earth to realise this, but the RACK_ENV environment variable (and the -E option to rackup) isn’t intended for consumption by anything other than Rack itself. If you want to indicate what sort of environment your application should run in, you’re going to have to do it via some other means.

Why is this? Because the interpretation that Rack applies to the value of RACK_ENV that you set makes no sense whatsoever outside of Rack. Valid options are “development”, “deployment”, and “none”. If you follow the usual Rails convention of naming your environments “development”, “test”, and “production” (and maybe “staging” if you’re feeling adventurous), then in any environment other than “development”, you’re not going to be telling Rack anything it understands.

As I said, I may be the last person on earth to have worked this out, but I doubt it. There are plenty of bug reports and “WTF?” blog posts and Stack Overflow questions that appear to stem from people misunderstanding the purpose of RACK_ENV. Sadly, the Rack documentation is very quiet on the whole topic, and the only place that mentions how the environment is interpreted is in the comments for Rack::Server.new – and that doesn’t tie that environment to the -E option or RACK_ENV environment variable.

At any rate, the take away is simple: unless you want Rack to pre-configure a bundle of middleware for you, RACK_ENV or rackup -E is not the configuration variable you’re looking for. Use something else to tell your app how it’s supposed to work.

1 Comment

From: Scott Muc
2014-02-12 06:14

Nope, you’re not the last person. It looks like those that have used RACK_ENV to mean a conceptual environment name have just had things work by coincidence.

Thanks for the post. I’m going to make sure that RACK_ENV is one of the valid options in my projects and refer those that disagree to this post. I know Sinatra is guilty of overloading the purpose of this variable.

Post a comment

All comments are held for moderation; markdown formatting accepted.

This is a honeypot form. Do not use this form unless you want to get your IP address blacklisted. Use the second form below for comments.
Name: (required)
E-mail: (required, not published)
Website: (optional)
Name: (required)
E-mail: (required, not published)
Website: (optional)