0

I'm trying to run a Jekyll build programatically, but I'm having issues in that the build crashes when trying to load some gems used by the Jekyll site's plugins.

My code looks like this (line breaks added for readability):

%x(cd #{@config['input_directory']} &&
  bundle install &&
  bundle exec jekyll build --config #{File.join(@config['input_directory'], "_config.yml")} --source #{@config['input_directory']} --destination #{@config['output_directory']} --trace)

When this runs, I get a stack trace that looks like this:

/_plugins/jekyll_lunr_js_search.rb:80:in `require': cannot load such file -- nokogiri (LoadError)
    from /_plugins/jekyll_lunr_js_search.rb:80:in `<top (required)>'
    from /ruby-1.9.3-p448/gems/jekyll-1.3.0/lib/jekyll/site.rb:77:in `require'
    from /ruby-1.9.3-p448/gems/jekyll-1.3.0/lib/jekyll/site.rb:77:in `block (2 levels) in setup'
    from /ruby-1.9.3-p448/gems/jekyll-1.3.0/lib/jekyll/site.rb:76:in `each'
    from /ruby-1.9.3-p448/gems/jekyll-1.3.0/lib/jekyll/site.rb:76:in `block in setup'
    from /ruby-1.9.3-p448/gems/jekyll-1.3.0/lib/jekyll/site.rb:75:in `each'
    from /ruby-1.9.3-p448/gems/jekyll-1.3.0/lib/jekyll/site.rb:75:in `setup'
    from /ruby-1.9.3-p448/gems/jekyll-1.3.0/lib/jekyll/site.rb:29:in `initialize'
    from /ruby-1.9.3-p448/gems/jekyll-1.3.0/lib/jekyll/commands/build.rb:5:in `new'
    from /ruby-1.9.3-p448/gems/jekyll-1.3.0/lib/jekyll/commands/build.rb:5:in `process'
    from /ruby-1.9.3-p448/gems/jekyll-1.3.0/bin/jekyll:77:in `block (2 levels) in <top (required)>'
    from /ruby-1.9.3-p448/gems/commander-4.1.5/lib/commander/command.rb:180:in `call'
    from /ruby-1.9.3-p448/gems/commander-4.1.5/lib/commander/command.rb:180:in `call'
    from /ruby-1.9.3-p448/gems/commander-4.1.5/lib/commander/command.rb:155:in `run'
    from /ruby-1.9.3-p448/gems/commander-4.1.5/lib/commander/runner.rb:402:in `run_active_command'
    from /ruby-1.9.3-p448/gems/commander-4.1.5/lib/commander/runner.rb:78:in `run!'
    from /ruby-1.9.3-p448/gems/commander-4.1.5/lib/commander/delegates.rb:11:in `run!'
    from /ruby-1.9.3-p448/gems/commander-4.1.5/lib/commander/import.rb:10:in `block in <top (required)>'

The Gemfile of the input_directory looks like this, so I'm not sure why it's not able to load nokogiri.

source 'https://rubygems.org'

gem 'directory_watcher', '= 1.4.1' # http://stackoverflow.com/questions/15591000/jekylls-auto-doesnt-work
gem 'kramdown'
gem 'nokogiri'
gem 'yui-compressor'
gem "jekyll"

group :development do
  gem 'capistrano'
  gem 'rvm-capistrano'
end

Any pointers here are welcome. I get the feeling there are better ways of doing this Jekyll build programmatically too, this just seemed to be simplest to me.

Alex Ghiculescu
  • 7,522
  • 3
  • 25
  • 41

1 Answers1

1

Run bundle exec jekyll to ensure that it sets up the gem path correctly for Jekyll. If you're calling the program from an app that uses Bundler itself, you may need to also wrap the execution in a call to Bundler.with_clean_env.

The complete command should look like this:

Bundler.with_clean_env do
  %x(cd #{@config['input_directory']} &&
    bundle install &&
    bundle exec jekyll build --config #{File.join(@config['input_directory'], "_config.yml")} --source #{@config['input_directory']} --destination #{@config['output_directory']} --trace)
end
Tim Moore
  • 8,958
  • 2
  • 23
  • 34
  • I did try that also - annoyingly, I got the same error (I'll update the question to indicate this). – Alex Ghiculescu Nov 19 '13 at 00:04
  • I've updated the answer with another suggestion to use `Bundler.with_clean_env`. – Tim Moore Nov 19 '13 at 10:15
  • One more question. There was no actual output to STDOUT during the build. Is there something I should be doing to have it show up (just for development purposes)? – Alex Ghiculescu Nov 19 '13 at 23:44
  • Using %x captures stdout for the child process and returns it as a string. Since you don't assign it to anything, it gets lost. You could use `system` instead to have the child process use STDOUT directly. See http://stackoverflow.com/a/18623297/29470 for details. – Tim Moore Nov 20 '13 at 11:40