Open rlue opened 7 years ago
Hi, we can’t work on examples that are sexually explicit. It’s not appropriate for users of rake looking for big fixes to be force to read such things
I have removed the offending Saturday Night Live reference from my description of the issue. Thank you for clarifying your standard of propriety; I sincerely apologize for any harm the wording of my issue may have caused.
Now that it has been amended, can you comment on whether the behavior described above is expected/intended or not?
Hi there. I have a same problem. I want to fix it and read a code. I think it is reason below but I don’t understand why Task needs have a actions “Array”? https://github.com/ruby/rake/blob/master/lib/rake/task.rb
def enhance(deps=nil, &block)
@prerequisites |= deps if deps
@actions << block if block_given?
self
end
My plan is a same name task has to be only one. thx.
@wolftatsu
@wolftatsu, I'm not sure we're talking about the same thing. Can you explain more?
@wolftatsu The same problem. load
makes the task's actions duplicate
[40] pry(main)> Rake.application.tasks.first.actions
=> [#<Proc:0x007ff287d796b0@/.../lib/tasks/test_task.rake:11>]
[41] pry(main)> load('./lib/tasks/test_task.rake')
=> true
[42] pry(main)> Rake.application.tasks.first.actions
=> [#<Proc:0x007ff287d796b0@/.../lib/tasks/test_task.rake:11>,
#<Proc:0x007ff287af32b0@/.../lib/tasks/test_task.rake:11>]
[43] pry(main)> load('./lib/tasks/test_task.rake')
=> true
[44] pry(main)> Rake.application.tasks.first.actions
=> [#<Proc:0x007ff287d796b0@/.../lib/tasks/test_task.rake:11>,
#<Proc:0x007ff287af32b0@/.../lib/tasks/test_task.rake:11>,
#<Proc:0x007ff285d65450@/.../lib/tasks/test_task.rake:11>]
I'm not sure if the following method has any other impacts
require 'rake/task'
module Rake
class Task
def enhance(deps=nil, &block)
@prerequisites |= deps if deps
if block_given?
@actions.clear
@actions << block
end
self
end
end
end
We ran into this as well, and it caused test failures because a test that ran Rails.application.load_tasks
and then Rake.application.rake_require 'some/task'
would run twice.
Alternative, should @actions
be defined as a Set
?
@fcce @stanhu This is an intended behavior https://github.com/ruby/rake/blob/c2eeae2fe2b67170472a1441ebf84d3a238c3361/lib/rake/task.rb#L413-L415
You should use require
to load files only once in this case.
============================================================
@rlue
You should use file
instead of task
in your example.
Studying the code for loading imports (and tests for it)
https://github.com/ruby/rake/blob/c2eeae2fe2b67170472a1441ebf84d3a238c3361/lib/rake/application.rb#L775-L789
I would say, that this is an intended behavior.
# Rakefile
task 'dep.rb' do
puts "From Rakefile"
end
task :hello => 'dep.rb'
import 'dep.rb'
# dep.rb
task 'dep.rb' do
puts "From dep.rb"
end
Output:
$ bundle exec rake hello
From Rakefile
From Rakefile
From dep.rb
You enhanced the task named dep.rb
in the file named dep.rb
(by "redefining" it), so after you import
ed this file you want to run that part too.
So, this issue should be closed.
I picked up Andrey Koleshko's Rake Task Management Essentials to start learning rake. The book covers the
import
statement in Chapter 1. After some tinkering, I think I figured out what he was trying to say, but noticed some differences between the behavior he describes in the book and what happens on my machine.I've copied the relevant section below for reference, but I think the upshot is:
If you import a file, and also define a task with the same name as that file, then that task will be run before the file is imported. This feature is provided so that dependency files may be generated on-the-fly.
The problem is that on my machine, the aforementioned pre-import task runs twice:
At first, I thought this was happening because I was explicitly calling the
foo.rb
task, so maybe that was running once because I called it and again in response to theimport
statement — but then I tried running a different task altogether:and I get the same duplicated output as before:
In fact, it doesn't just spit out the same input twice; it actually runs twice, from start to finish:
Is this the expected behavior, or does this constitute a bug in rake?
On a related note, the command-line flag
-T
is supposed to display a list of all the rake tasks that have descriptions, but when runningrake -T
against the aboveRakefile
, theimport
directive causes the corresponding task to run anyway:Is this the expected behavior, or does this constitute a bug in rake?
FWIW I'm on Ruby 2.4.0 / Rake 12.0.0