pygments / pygments.rb

💎 Ruby wrapper for Pygments syntax highlighter
MIT License
572 stars 141 forks source link

MentosError (Failed to get header.) #105

Closed gabrielecirulli closed 10 years ago

gabrielecirulli commented 10 years ago

I checked issue #45 but it doesn't seem to be the same case. I'm running my Rails app in production using Passenger and Nginx and I'm trying to highlight a piece of Ruby code. When I submit the code, I get a 500 error from my app. Looking at the Nginx logs, I see:

App 17862 stdout: Completed 500 Internal Server Error in 13ms
App 17862 stdout: 
App 17862 stdout: MentosError (Failed to get header.):
App 17862 stdout:   config/initializers/markdown_renderer.rb:3:in `block_code'
App 17862 stdout:   app/models/article.rb:16:in `render'
App 17862 stdout:   app/models/article.rb:16:in `cache_makdown'
App 17862 stdout:   app/controllers/articles_controller.rb:15:in `create'

I have checked all the python executables available on my Ubuntu server. They're all Python 2.7.6, which I installed from source a few hours ago.

$ which python
/usr/local/bin/python
$ which python2
/usr/local/bin/python2
$ python --version
Python 2.7.6
$python2 --version
Python 2.7.6

There's also another weird issue, where if I try using pygments from a ruby console, everything works fine:

$ irb
irb(main):001:0> require "pygments"
=> true
irb(main):002:0> Pygments.highlight("1 + 1", lexer: "ruby")
=> "<div class=\"highlight\"><pre><span class=\"mi\">1</span> <span class=\"o\">+</span> <span class=\"mi\">1</span>\n</pre></div>"

And even from the Rails console of the actual application:

$ RAILS_ENV=production bundle exec rails c
Loading production environment (Rails 4.0.0)
irb(main):001:0> Pygments.highlight("1 + 1", lexer: "ruby")
=> "<div class=\"highlight\"><pre><span class=\"mi\">1</span> <span class=\"o\">+</span> <span class=\"mi\">1</span>\n</pre></div>"

I am running Ruby 2.1.0, I have Ubuntu and Python installed from source. Does anyone know what could be the cause?

gabrielecirulli commented 10 years ago

It turns out my application was running without any useful information in ENV["PATH"], so it couldn't find python.

I added an initializer that sets ENV["PATH"] = "/usr/local/bin:/usr/bin:#{ENV["PATH"]}" and it now works fine.