Curious behavior of rake

I came across a curious rake behavior, in which my rakefile required a file
that (accidentally) required the rakefile itself.

It makes rake execute tasks twice.

rake, version 0.4.15

--- rakefile---
require 'a'
task :one
    puts :one
end
----a.rb---
require 'rakefile'

···

-----------

rake one
#=>
one
one

I came across a curious rake behavior, in which my rakefile required a file
that (accidentally) required the rakefile itself.

Hmmm ... don't do that :slight_smile:

It makes rake execute tasks twice.

Yep. Rake loads the rakefile via the "load" command, require doesn't know
that the rake file is alread loaded. So loading it and requiring it cause
the file to be evaluated twice. Rake tasks are additive, e.g. you can say:

  task :one do puts "ONCE" end
  task :one do puts "TWICE" end

Since the file is evaluated twice, each task action is added to the task
twice. Therefore running any task executes the actions twice.

If there is stuff in the rakefile that the other file depends on, it is better
to extract it into a normal library file and let both the rakefile and the
other file require that third file.

Hope this helps.

···

On Monday 24 January 2005 05:20 pm, itsme213 wrote:

--
-- Jim Weirich jim@weirichhouse.org http://onestepback.org
-----------------------------------------------------------------
"Beware of bugs in the above code; I have only proved it correct,
not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)

Since the file is evaluated twice, each task action is added to the task
twice. Therefore running any task executes the actions twice.

Hope this helps.

Very clear now, thanks.

If there is stuff in the rakefile that the other file depends on, it is

better

to extract it into a normal library file and let both the rakefile and the
other file require that third file.

I have had trouble previously before because of the number of Dir and/or
FileUtil methods 'included' by rake. Specifically name conflicts since
whatever rake includes goes into Object, and that conflicted with things I
do in my own code. I was wondering if there was a way around that global
'include', since uses of the Dir or FileUtil methods are (should be?)
limited to within tasks. For example, Tasks could catch missing methods that
are in the set of FileUtil.instance_methods, and handle them appropriately.
Feasible? Worth considering?

···

"Jim Weirich" <jim@weirichhouse.org> wrote

I have had trouble previously before because of the number of Dir and/or
FileUtil methods 'included' by rake. Specifically name conflicts since
whatever rake includes goes into Object, and that conflicted with things I
do in my own code.

Generally rake assumes that it owns the "VM". It doesn't expect you to run
application code in the same ruby instance as rake is running. I know
Austin's test task does this, but goes against one of rake's basic
assumptions.

I was wondering if there was a way around that global
'include', since uses of the Dir or FileUtil methods are (should be?)
limited to within tasks. For example, Tasks could catch missing methods
that are in the set of FileUtil.instance_methods, and handle them
appropriately. Feasible? Worth considering?

An interesting idea. Its probably worth investigating. Thanks for the idea.

···

On Monday 24 January 2005 08:55 pm, itsme213 wrote:

--
-- Jim Weirich jim@weirichhouse.org http://onestepback.org
-----------------------------------------------------------------
"Beware of bugs in the above code; I have only proved it correct,
not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)