Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

rules do not recieve arguments #27

Closed
avishai-ish-shalom opened this Issue · 6 comments

4 participants

@avishai-ish-shalom
require 'rake'

desc "test"
task :test, [:arg1] => ['test.txt', 'test.csv'] do |t, args|
  puts t.name + ": " + args.inspect
end

file "test.txt", [:arg1] do |t, args|
  puts t.name + ": " + args.inspect
end

rule ".csv", [:arg1] do |t, args|
  puts t.name + ": " + args.inspect
end
running this with
rake -v -t test[somearg]
yields:
** Invoke test (first_time)
** Invoke test.txt (first_time)
** Execute test.txt
test.txt: {:arg1=>"somearg"}
** Invoke test.csv (first_time)
** Execute test.csv
test.csv: {}
** Execute test
test: {:arg1=>"somearg"}

Since rule synthesize file tasks, i would expect the generated tasks to behave the same as file task, including argument handling. This behavior is due to the way resolve_args is called in create_rule function (lib/rake/task_manager.rb):
def create_rule(*args, &block)
pattern, _, deps = resolve_args(args)
pattern = Regexp.new(Regexp.quote(pattern) + '$') if String === pattern
@rules << [pattern, deps, block]
end
Arguments are returned to _ and thrown away.

@jimweirich
Owner

I'm trying to think if there is a good reason for not passing args to rules. I will ponder more on this.

@quix

avishai-ish-shalom, were you aware that arguments are still passed to rules? Replace your rule with

rule ".csv", [:arg1] do |t, args|
  puts t.name + "[:arg1]: " + args[:arg1]
end

Output:

test.txt: {:arg1=>"somearg"}
test.csv[:arg1]: somearg
test: {:arg1=>"somearg"}
@avishai-ish-shalom

weird! how come args[:args1] works but inspect fails? i need to dig into this.

@quix

The default inspect does not walk the parents. A deep_inspect method could do so (quix@84df62), but it's unclear whether this should be the default.

rule ".csv", [:arg1] do |t, args|
  puts t.name + ": " + args.deep_inspect
end
@jimweirich
Owner

looks like this is solved.

@jimweirich jimweirich closed this
@Shumakriss

I know this is ancient but... Is this possibly an issue in earlier versions? 0.9.2.2 for example?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.