rake
rake copied to clipboard
Shared multitask prerequisite intermittently executed multiple times
Given a Rakefile
multitask :default => [:a, :b, :c, :d, :e]
task :a => ['target']
task :b => ['target']
task :c => ['target']
task :d => ['target']
task :e => ['target']
rule(/^target$/ => [ 'source' ]) do |t|
p "#{t.name} => #{t.source}"
p t.actions
end
file 'source'
the target may get executed multiple times like this
$ ruby --version
ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux-musl]
$ rake --version
rake, version 12.3.1
$ rake --trace --rules
** Invoke default (first_time)
** Invoke a (first_time)
Attempting Rule target => source
** Invoke b (first_time)
Attempting Rule target => source
** Invoke c (first_time)
Attempting Rule target => source
** Invoke d (first_time)
Attempting Rule target => source
(target => source ... EXIST)
** Invoke e (first_time)
Attempting Rule target => source
(target => source ... EXIST)
(target => source ... EXIST)
** Invoke target (first_time)
(target => source ... EXIST)
** Invoke source (first_time)
(target => source ... EXIST)
** Execute source
** Execute target
"target => source"
[#<Proc:0x000055e092b971c0@/repo/Rakefile:10>, #<Proc:0x000055e092b971c0@/repo/Rakefile:10>, #<Proc:0x000055e092b971c0@/repo/Rakefile:10>, #<Proc:0x000055e092b971c0@/repo/Rakefile:10>, #<Proc:0x000055e092b971c0@/repo/Rakefile:10>]
"target => source"
[#<Proc:0x000055e092b971c0@/repo/Rakefile:10>, #<Proc:0x000055e092b971c0@/repo/Rakefile:10>, #<Proc:0x000055e092b971c0@/repo/Rakefile:10>, #<Proc:0x000055e092b971c0@/repo/Rakefile:10>, #<Proc:0x000055e092b971c0@/repo/Rakefile:10>]
"target => source"
[#<Proc:0x000055e092b971c0@/repo/Rakefile:10>, #<Proc:0x000055e092b971c0@/repo/Rakefile:10>, #<Proc:0x000055e092b971c0@/repo/Rakefile:10>, #<Proc:0x000055e092b971c0@/repo/Rakefile:10>, #<Proc:0x000055e092b971c0@/repo/Rakefile:10>]
"target => source"
[#<Proc:0x000055e092b971c0@/repo/Rakefile:10>, #<Proc:0x000055e092b971c0@/repo/Rakefile:10>, #<Proc:0x000055e092b971c0@/repo/Rakefile:10>, #<Proc:0x000055e092b971c0@/repo/Rakefile:10>, #<Proc:0x000055e092b971c0@/repo/Rakefile:10>]
"target => source"
[#<Proc:0x000055e092b971c0@/repo/Rakefile:10>, #<Proc:0x000055e092b971c0@/repo/Rakefile:10>, #<Proc:0x000055e092b971c0@/repo/Rakefile:10>, #<Proc:0x000055e092b971c0@/repo/Rakefile:10>, #<Proc:0x000055e092b971c0@/repo/Rakefile:10>]
** Execute d
** Invoke target
** Execute b
** Invoke target
** Execute a
** Invoke target
** Execute c
** Invoke target
** Execute e
** Execute default
The results differ between runs. It seems to happen more often with debug output.
The same action block gets added multiple times to the same task.
Workaround:
LOOKUP_MONITOR = Monitor.new
module RakeTaskManagerPatch
def [](task_name, scopes=nil)
self.lookup(task_name.to_s, scopes) or LOOKUP_MONITOR.synchronize { super(task_name, scopes) }
end
end
class Rake::Application prepend RakeTaskManagerPatch end