Skip to content
Browse files

fix root path detection under thin/passenger

Fixed how the Sinatra application's path is set. Previously it
was derived exclusively from $0, but this was causing problems
when running Sinatra under Rack because $0 is the web server
(e.g., thin, passenger, etc) and thus Sinatra would incorrectly
set things like the default views path. This fix adds a new
locate_app_file method that uses a number of techniques to guess the
Sinatra application's path.
  • Loading branch information...
1 parent b03d717 commit 68d76aa504a629e112caef13526951696e615ad4 @igal igal committed with rtomayko Nov 4, 2008
Showing with 16 additions and 2 deletions.
  1. +3 −0 ChangeLog
  2. +13 −2 lib/sinatra.rb
View
3 ChangeLog
@@ -2,6 +2,9 @@
* BUG: use_in_file_templates! failes with CR/LF (#45)
+ * BUG: Sinatra detects the app file and root path when run under
+ thin/passenger.
+
= 0.3.2
* BUG: Static and send_file read entire file into String before
View
15 lib/sinatra.rb
@@ -923,7 +923,8 @@ def initialize
# file, before any DSL related functions are invoked.
def self.default_options
return @default_options unless @default_options.nil?
- root = File.expand_path(File.dirname($0))
+ app_file = locate_app_file
+ root = File.expand_path(File.dirname(app_file))
@default_options = {
:run => true,
:port => 4567,
@@ -934,7 +935,7 @@ def self.default_options
:public => root + '/public',
:sessions => false,
:logging => true,
- :app_file => $0,
+ :app_file => app_file,
:raise_errors => false
}
load_default_options_from_command_line!
@@ -957,6 +958,16 @@ def self.load_default_options_from_command_line! #:nodoc:
end.parse!(ARGV.dup.select { |o| o !~ /--name/ })
end
+ # Use heuristics to locate the application file.
+ def self.locate_app_file
+ caller[1..-1].reverse.each do |path|
+ path = path.split(':', 2)[0]
+ next if path =~ /sinatra\.rb$/ || path == '(__DSL__)'
+ return path
+ end
+ $0
+ end
+
# Determine whether the application is in the process of being
# reloaded.
def reloading?

0 comments on commit 68d76aa

Please sign in to comment.
Something went wrong with that request. Please try again.