Can't rake Bundler

783 views
Skip to first unread message

Arve Knudsen

unread,
Oct 25, 2010, 9:50:36 AM10/25/10
to ruby-b...@googlegroups.com
Hi

When trying to rake the latest Bundler from Git under Ruby 1.8.7 on Windows, an error occurs:

PS C:\Users\arvek\VCS-Checkouts\Bundler> rake --trace -T
(in C:/Users/arvek/VCS-Checkouts/Bundler)
rake aborted!
Unable to determine name from existing gemspec. Use :name => 'gemname' in #install_tasks to manually set it.
C:/Users/arvek/VCS-Checkouts/Bundler/lib/bundler/gem_helper.rb:18:in `initialize'
C:/Users/arvek/VCS-Checkouts/Bundler/lib/bundler/gem_helper.rb:9:in `new'
C:/Users/arvek/VCS-Checkouts/Bundler/lib/bundler/gem_helper.rb:9:in `install_tasks'
C:/Users/arvek/VCS-Checkouts/Bundler/Rakefile:4
C:/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2383:in `load'
C:/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2383:in `raw_load_rakefile'
C:/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2017:in `load_rakefile'
C:/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling'
C:/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2016:in `load_rakefile'
C:/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2000:in `run'
C:/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling'
C:/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:1998:in `run'
C:/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.7/bin/rake:31
C:/Ruby187/bin/rake:19:in `load'
C:/Ruby187/bin/rake:19


What do you think goes wrong?

Arve

Arve Knudsen

unread,
Oct 25, 2010, 10:41:21 AM10/25/10
to ruby-b...@googlegroups.com
I've traced it (no pun intended) to a badly implemented Rake.application.rakefile_location, that in my case returns "C:/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2470:in `rakefile_location'" as the Rakefile location (yes, really). I hacked gem_helper to work around the issue (reimplement Rake.application.rakefile_location), and it looks to me at least like the fix is sound. Please review my patch:

diff --git a/lib/bundler/gem_helper.rb b/lib/bundler/gem_helper.rb
index 184967a..4cd5c9a 100644
--- a/lib/bundler/gem_helper.rb
+++ b/lib/bundler/gem_helper.rb
@@ -1,11 +1,32 @@
 $:.unshift File.expand_path('../vendor', __FILE__)
 require 'thor'
 require 'bundler'
+require 'rbconfig'

 module Bundler
   class GemHelper
     def self.install_tasks(opts = nil)
-      dir = File.dirname(Rake.application.rakefile_location)
+      # Determine the rakefile's location, don't use
+      # Rake.application.rakefile_location since it's buggy
+      rakefile_loc = nil
+      begin
+        fail
+      rescue RuntimeError => ex
+        on_win = Config::CONFIG["host_os"] =~ /mswin|mingw/
+        ex.backtrace.each do |frame|
+          if on_win
+            # Typically, on Windows Rake.application.rakefile is lowercase
+            # whereas the real file might not be, luckily case doesn't matter
+            frame = frame.downcase
+          end
+          m = /^((:?.+\/)#{Rake.application.rakefile}):\d+(:?:.+)?$/.match(frame)
+          if not m.nil?
+            rakefile_loc = m[1]
+          end
+        end
+      end
+
+      dir = File.dirname(rakefile_loc)
       self.new(dir, opts && opts[:name]).install
     end

Arve

Andre Arko

unread,
Oct 25, 2010, 12:24:48 PM10/25/10
to ruby-b...@googlegroups.com
On Oct 25, 2010, at 7:41 AM, Arve Knudsen wrote:
> I've traced it (no pun intended) to a badly implemented Rake.application.rakefile_location, that in my case returns "C:/Ruby187/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2470:in `rakefile_location'" as the Rakefile location (yes, really). I hacked gem_helper to work around the issue (reimplement Rake.application.rakefile_location), and it looks to me at least like the fix is sound. Please review my patch:

Thanks for tracking this down. Do you think you could try this alternate patch and let me know if it works?

- dir = File.dirname(Rake.application.rakefile_location)
+ dir = caller.find{|c| /Rakefile:/}[/^(.*?)\/Rakefile:/, 1]

— Andre

Arve Knudsen

unread,
Oct 25, 2010, 1:18:02 PM10/25/10
to ruby-b...@googlegroups.com
I'll try to test it in a couple of hours.

Arve 

Arve Knudsen

unread,
Oct 25, 2010, 4:35:41 PM10/25/10
to ruby-b...@googlegroups.com
On Mon, Oct 25, 2010 at 6:24 PM, Andre Arko <an...@arko.net> wrote:
This doesn't work if the Rakefile is called something else, for instance Rakefile1 (i.e., rake -f Rakefile1). Besides, the code is perhaps too terse don't you think? I find it difficult to determine which cases this code might not handle, but I'm not really a Rubyist either (Python is my specialty). Using caller to get at the stack is a good idea though (new to me).

Arve

Arve Knudsen

unread,
Oct 25, 2010, 4:49:48 PM10/25/10
to ruby-b...@googlegroups.com
If I wanted to write various testcases, for example test with various Rakefile names, how would I do this in Bundler? The only Ruby-related testing I've done is with Cucumber (the reason I'm using Bundler in the first place).

Arve

Arve Knudsen

unread,
Oct 25, 2010, 5:05:40 PM10/25/10
to ruby-b...@googlegroups.com
I have a new more robust patch, which also raises RuntimeError if it fails to deduce the Rakefile location. I think it's quite good at this point, so I'm attaching it, although I should like some automatic testcases for it :)

I've tested with Ruby 1.9 as well as 1.8, no differences detected.

Arve
fix-raking.patch

Andre Arko

unread,
Oct 25, 2010, 5:12:17 PM10/25/10
to ruby-b...@googlegroups.com
On Oct 25, 2010, at 1:35 PM, Arve Knudsen wrote:
> This doesn't work if the Rakefile is called something else, for instance Rakefile1 (i.e., rake -f Rakefile1). Besides, the code is perhaps too terse don't you think? I find it difficult to determine which cases this code might not handle, but I'm not really a Rubyist either (Python is my specialty). Using caller to get at the stack is a good idea though (new to me).


That's a good point. We'll probably have to come up with an approach that combines both options.

> If I wanted to write various testcases, for example test with various Rakefile names, how would I do this in Bundler? The only Ruby-related testing I've done is with Cucumber (the reason I'm using Bundler in the first place).

Bundler has a somewhat... idiosyncratic test setup. The tests for the GemHelper class are located in bundler/spec/other/gem_helper_spec.rb. I would be happy to include testcases for this if you feel like writing them. :)

— Andre

Arve Knudsen

unread,
Oct 25, 2010, 5:30:45 PM10/25/10
to ruby-b...@googlegroups.com
The idea was that I write testcases, yes :) I just need to know how. I'll see if I can figure something out from reading the existing tests.

Thanks!
Arve

Andre Arko

unread,
Oct 25, 2010, 5:43:03 PM10/25/10
to ruby-b...@googlegroups.com
On Oct 25, 2010, at 2:30 PM, Arve Knudsen wrote:
> The idea was that I write testcases, yes :) I just need to know how. I'll see if I can figure something out from reading the existing tests.

Bundler's tests are generally written integration-style, so I imagine you could try setting up a test directory with a rakefile, and then execute `rake -T` and validate the output to determine whether the test passed or failed. If you have more questions, feel free to email me directly or visit #bundler on irc.freenode.net. I'm there at some point on most days.

— Andre

Arve Knudsen

unread,
Nov 22, 2010, 4:32:36 PM11/22/10
to ruby-b...@googlegroups.com
I've gotten started writing tests for Bundler's Rake tasks, but I've hit the problem that rake doesn't work under Bundler tests. What happens is this:
C:/Ruby187/lib/ruby/site_ruby/1.8/rubygems.rb:779:in `report_activate_error': Could not find RubyGem rake (>= 0) (Gem::L
oadError)
        from C:/Ruby187/lib/ruby/site_ruby/1.8/rubygems.rb:214:in `activate'
        from C:/Ruby187/lib/ruby/site_ruby/1.8/rubygems.rb:1082:in `gem'
        from C:/Ruby187/bin/rake:18

It was suggested on IRC that the problem might be that the Bundler tests clear GEM_HOME and possibly other environment variables at the outset, that rake needs to find its gem. How should I store GEM_HOME and evt. other variables at the outset and restore them before running rake?

Thanks!
Arve


— Andre

--
You received this message because you are subscribed to the Google Groups "ruby-bundler" group.
To post to this group, send email to ruby-b...@googlegroups.com.
To unsubscribe from this group, send email to ruby-bundler...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/ruby-bundler?hl=en.


Andre Arko

unread,
Nov 22, 2010, 10:09:36 PM11/22/10
to ruby-b...@googlegroups.com
On Nov 22, 2010, at 1:32 PM, Arve Knudsen wrote:
> I've gotten started writing tests for Bundler's Rake tasks, but I've hit the problem that rake doesn't work under Bundler tests. What happens is this:
> C:/Ruby187/lib/ruby/site_ruby/1.8/rubygems.rb:779:in `report_activate_error': Could not find RubyGem rake (>= 0) (Gem::L
> oadError)
> from C:/Ruby187/lib/ruby/site_ruby/1.8/rubygems.rb:214:in `activate'
> from C:/Ruby187/lib/ruby/site_ruby/1.8/rubygems.rb:1082:in `gem'
> from C:/Ruby187/bin/rake:18
>
> It was suggested on IRC that the problem might be that the Bundler tests clear GEM_HOME and possibly other environment variables at the outset, that rake needs to find its gem. How should I store GEM_HOME and evt. other variables at the outset and restore them before running rake?

Hi Arve,

Bundler doesn't allow system gems to be used inside tests. However, it sets up its own pretend system gems repository that you can use while the tests run. As you can see in spec/support/rubygems_ext.rb, the rake gem is included in that list.

In order to use the rake gem in your tests, I think you can do something like this:

describe "the bundler gem helper rake tasks" do
before(:each) do
install_gemfile <<-G
gem "rake"
G
end

it "should work with rake" do
bundle "gem foo"
Dir.chdir(bundled_app("foo")) do
`rake build`.should == "Gem built"
end
end
end

I haven't tested that, so it's probably not completely right, but that should be the right general idea.

— Andre

Arve Knudsen

unread,
Nov 23, 2010, 5:01:49 AM11/23/10
to ruby-b...@googlegroups.com
Thanks for the info Andre, I'll try it out.

Arve 

Arve Knudsen

unread,
Nov 28, 2010, 6:56:47 AM11/28/10
to ruby-b...@googlegroups.com
Hi Andre

On Tue, Nov 23, 2010 at 4:09 AM, Andre Arko <an...@arko.net> wrote:
I tried your code and it doesn't work, as the rake gem is still not found, even though it's been installed (with install_gemfile).

In trying to figure out the problem, I've distilled it to this case:

describe "the bundler gem helper rake tasks" do
  it "should work with rake" do
    install_gemfile <<-G
    gem "rake"
    G

    sys_exec("#{Gem.ruby} -e 'require \"rubygems\"; gem \"rake\"")
  end
end

Since the rake gem is now installed in the test environment (I presume), it should be found by Ruby right? Or are we supposed to run Ruby in a special manner, so it finds gems in the test environment?

Thanks,
Arve

Andre Arko

unread,
Nov 28, 2010, 3:06:45 PM11/28/10
to ruby-b...@googlegroups.com
On Nov 28, 2010, at 3:56 AM, Arve Knudsen wrote:
> Since the rake gem is now installed in the test environment (I presume), it should be found by Ruby right? Or are we supposed to run Ruby in a special manner, so it finds gems in the test environment?

The latter. The tests provide a helper method called ruby(), defined in the same file as sys_exec(), that sets up the bundled environment and then runs a ruby interpreter to execute whatever ruby code you pass as the arguments. Give that a shot?

— Andre

Arve Knudsen

unread,
Nov 28, 2010, 3:13:07 PM11/28/10
to ruby-b...@googlegroups.com
describe "the bundler gem helper rake tasks" do
  it "should work with rake" do
    install_gemfile <<-G
    gem "rake"
    G

    ruby("require \"rubygems\"; gem \"rake\"")
  end
end
 

results in:

C:/Ruby187/lib/ruby/site_ruby/1.8/rubygems.rb:779:in `report_activate_error': Could not find RubyGem rake (>= 0) (Gem::LoadError)
        from C:/Ruby187/lib/ruby/site_ruby/1.8/rubygems.rb:214:in `activate'
        from C:/Ruby187/lib/ruby/site_ruby/1.8/rubygems.rb:1082:in `gem'
        from -e:1


Arve

Andre Arko

unread,
Nov 28, 2010, 3:15:10 PM11/28/10
to ruby-b...@googlegroups.com
On Nov 28, 2010, at 12:13 PM, Arve Knudsen wrote:
> ruby("require \"rubygems\"; gem \"rake\"")

Bundler disables rubygems and the gem method. You just need to ruby("require 'rake'; puts Rake") or whatever.

— Andre

Arve Knudsen

unread,
Nov 28, 2010, 3:48:56 PM11/28/10
to ruby-b...@googlegroups.com
I found the root of the problem after futzing around a bit. The problem was actually that my (or our) gemfile specification didn't declare the gem source, and install_gemfiles swallowed the error. This updated code works:

describe "the bundler gem helper rake tasks" do
  it "should work with rake" do
    out = install_gemfile(<<-G
    source "file://#{gem_repo1}"
    gem "rake"
    G

    sys_exec("rake -T")
  end
end


Arve

Arve Knudsen

unread,
Nov 28, 2010, 5:09:58 PM11/28/10
to ruby-b...@googlegroups.com
Now on to implementing my actual tests. In this context I get another error running rake:
C:/Users/Arve/VCSCheckouts/Bundler/lib/bundler/shared_helpers.rb:102:in `gem': rake is not part of the bundle. Add it to
 Gemfile. (Gem::LoadError)
        from C:/Users/Arve/VCSCheckouts/Bundler/tmp/gems/system/bin/rake:18


This is my current test code:
  
describe "Bundler::GemHelper tasks" do
  context "install rake tasks" do
    before(:each) do
      # Install the rake gem for our test
      install_gemfile(<<-G
source "file://#{gem_repo1}"
gem "rake"
G
                     )

      # Create gem 'test*
      bundle("gem test")
      @app = bundled_app("test")
      # Enter test gem
      Dir.chdir(@app)
      # Generate Rakefile
      File.open("Rakefile", "wb") do |f|
        f.write(%{\
require 'bundler/gem_helper'
Bundler::GemHelper.install_tasks
})
      end
    end

    it "should work when no Rakefile is specified" do
      bundle("exec rake -T")
      # TODO
      @out.should == ""
    end

    it "should work work when a Rakefile is specified" do
      FileUtils.mv("Rakefile", "makefile")
      sys_exec("rake -f makefile -T")
      @out.should == "nei!"
    end

    it "should work when rake has entered another directory than the one containing the Rakefile" do
        File.open("Rakefile", "wb") do |f|
          f.write(%{\
require 'bundler/gem_helper'
Dir.mkdir("tmp")
Dir.chdir("tmp")
Bundler::GemHelper.install_tasks
})
        end
    end
  end
end


What should I do?

Arve

Arve Knudsen

unread,
Dec 27, 2010, 8:36:34 AM12/27/10
to ruby-b...@googlegroups.com
I see that this problem is still unsolved in the master branch of Bundler. Is there any work going on to solve it? FWIW, I've a branch 'fix-raking' that fixes the problem for me at least: https://github.com/aknuds1/bundler/tree/fix-raking.

Arve 
Reply all
Reply to author
Forward
0 new messages